Skip to main content

Posts

Showing posts with the label WCF

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. 

ASP.NET Web API

ASP.NET Web API is a framework based on ASP.NET for building HTTP endpoints. The ASP.NET Web API can be built as a part of ASP.NET Webforms or MVC to function as the services layer of a  web, mobile or client desktop application. ASP.NET Web API is recommended over WCF in many situations except if you are limited to use .NET 3.5 and/or if you need to expose a SOAP based service endpoints. ASP.NET Web API is different from REST since it may not fully comply with a RESTful architecture, however, a  REST based service can be built on top of Web API. Creating a simple ASP.NET Web API 1. Open Visual studio 2. Create a new project Template > Visual C# > Web > ASP.NET Empty Web Application 3. Right click on the project > Manage NuGet Packages... > Search for  Web API   > select  Microsoft ASP.NET Web API  > click  Install > Click Close 4. Add a new class to the project, name it MyApiControlle...

Service Instance and InstanceContextMode attribute

InstanceContextMode can be set to PerCall, Single or PerSession. InstanceContextMode.PerCall Description New service instance for each call. To enable set InstanceContextMode.PerCall Service Instance created when The instance is created when a request is received from the client Service Instance disposed when Disposed after servicing a single call. Benefits Scalable, Service instance disposed after each call. Issues Stateless, lowers performance, Clients not aware of multiple instances created during session. InstanceContextMode.Single Description Single instance is used to serve all calls. To enable set InstanceContextMode.Single Service Instance created when The instance is created when the service host is created Service Instance disposed when Disposed when service host is closed. Benefits Easy to manage...

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...

Using MMC to view certificates

1. Open Microsoft Management Console - press Windows Key + R > type MMC > press enter Microsoft Management Console 2. Click File > Add/Remove Snap-in... 3. Select Certificates from Available Snap-ins > click Add > Select Computer account  > click Next 4. Keep the default and click Finish  5. Select Certificates  from  Available Snap-ins  > click  Add  > Select  My user account  > click  Finish  6. Click OK 7. Press ctrl + S to save the snap-in