Skip to main content

Posts

Showing posts with the label C#

WCF Review Exercise 2

1. Download and open the WCF application  here . 2. Enable message logging in the host application 3. In the client application, handle CommunicationException, TimeOutException, and FaultException in the correct order.  4.  Use finally block to check if the client channel has entered a faulted state if it has then the abort the client and create a new instance of the client if the client is required again. 5. Enable reliable session by changing the app.config of the service host project.  6.  In the service host project, create a custom service host class called NewProdSrvHost. 7.   Override OnOpening() method to add an endpoint with basicHttpBinding and to enable metadata so that M etadata is always enabled with one endpoint. 8. Update the service host project to use the custom host class created above. 9. Update client reference and test the whole application.  10 Save and submit as a single zip file. 

Controlling life cycle of a Service instance

1. Download the start file here . 2. To use a single service instance to handle request all clients, add the following annotation above the service implementation. [ ServiceBehavior (InstanceContextMode = InstanceContextMode .Single)] public class StudentService : IStudentService { ... 3. Run the ConsoleHost and ConsoleClient, add a student then use option 3 and 4. Notice the instance HashCode for all three service calls. 4. When the above ConsoleHost and ConsoleClient is running, add another ConsoleClient. 5. Using the new ConsoleClient, add a student then use the option 3 and 4. Switch to the first ConsoleClient and add another student. Then use option 4. 6. Notice that all of the above calls use just a single instance or a Singleton of the service. 7. Now, let's see how the PerCall ServiceInstanceMode behaves. To change it PerCall mode do the following changes. [ ServiceBehavior (InstanceContextMode =  InstanceContextMode .PerCall)] public ...

Creating a custom Service Host Factory

Creating a custom ServiceHostFactory 1. Follow the instructions in this blog to create a custom service host. 2. Follow the instructions in this blog to Host the service in a website 3. Open the NewProdSrvHost.cs file and change the constructor as shown below: public NewProdSrvHost( params Uri [] baseAddresses) : base ( typeof ( ProductService ),baseAddresses) { } 4. Move the NewProdSrvHost to the ProductServiceLibrary Project. 5. Right click productServiceLibrary project > select Add > Class > name: ProdSrvHostFactory. 6. Make the class public and derive it from ServiceHostFactory 7. Override CreateServiceHost method Protected override Sytem.ServiceModel.ServiceHost CreateServiceHost(type { return new NewProdSrvHost(baseAddresses); } 8. Go to Svc file and add Factory attribute <%@ ServiceHost Service="ProductServiceLibrary.ProductService" Factory="ProductServiceLibrary.ProdSrvHostFactory" %> 9. Open the Svc file in ...

Message reliablity

When a message is sent across the network many things can go wrong. The message can be lost, dropped or sent in wrong order. This can happen due to a variety of reason, network failure, network congestion and proxy failure are few reasons among many other reasons.  WCF Reliablity features sessions are used to take care most of the above issues. There are two types of reliability Message reliability and Transport reliability. These reliability features simplifies coding needed. Enabling reliable session Reliable sessions are built on top of SOAP messaging and enables connection maintenance, verification and message ordering. Turning on reliable session allow the application to ignore duplicates, retry dropped connection and reestablish connections, notify service when the client is not available anymore. However, the cost of this reliability is increased traffic. Reliable session is easy to turn on. For some binding the reliable session is on by default whereas for ot...

Example - Chat Application

1. Create a WCF Service library project Open Visual Studio > File  >  New  >  Project Select  WCF Service Library  template under  Visual C# > WCF  templates Change the name to  ChatServiceLibrary  and click  OK Visual studio will add reference to System.ServiceModel and System.Runtime.Serialization and will add some files with sample code.  Delete IService1.cs and Service1.cs and add a new class called ChatService 1.1 Create the Data Contract  Open ChatService.cs file that you created. Add  using System.ServiceModel; Add  using System.Runtime.Serialization; Add the following code above ChatService class    [DataContract]     public class ChatMsg     {         public ChatMsg() { }         public ChatMsg(string n, string m){ this.User = n; this.Msg = m;}            ...

WCF Service Contracts

Create a WCF Service library project Open Visual Studio Select  File  >  New  >  Project Select  WCF Service Library  template under  Visual C# > WCF  templates Change the name to  ProductServiceLibrary  and click  OK Visual studio will add reference to System.ServiceModel and System.Runtime.Serialization and will add some files with sample code.  Delete IService1.cs and Service1.cs Add a new class called ProductService Add the following code.      [DataContract]     public class ProductReview     {         [DataMember]         public string Id { get; set; }         [DataMember]         public string ProductCode { get; set; }         [DataMember]         public string ProductName { get; set; }         [DataMember] ...

WCF Exercise

Scenario Ultimate Lottery and Gaming (ULG) wants to build a WCF application that allows the client application at the ticket vendor to retrieve winning number of a given lottery draw.  Task 1: Create a WCF service that exposes a method called GetWinningNo that takes a string parameter and returns an object of type Draw. Use the following two partially completed classes:      public class Draw     {         public string Id { get; set; }         public int [] WinningNo { get; set; }          public double PoolAmt { get; set; }     }     public class LottoSrv     {         Dictionary<string, Draw> drawList = new Dictionary<string, Draw>();         public LottoSrv()         {             int [] w1 = { 1, 2, 3, 4, 5, 6, 7 };...

Collections

Collection Classes From the last two week's labs you may have already identified some issues related to arrays. We learnt that arrays holds identically typed items. However there are instances when you may requires a more flexible data structure that is capable of growing dynamically without throwing an exception (IndexOutOfRangeException) as in the case of an array. .NET framework's System.Collection classes provides a number of builtIn data structures that are dynamic and typesafe. Some of these classes are generic and others are non-Generic. Generic classes are more typesafe and requires a "type parameter" to be specified. Whereas, the non-generic classes are very loosely typed and should be avoided if possible. Non-Generic Classes System.Collections namespace consists of following non generic classes: ArrayList Queue Stack HashTable SortedList BitArray Issues related to non-generic classes:  Slower executions speed: Can be slower ...