WCF Service FAQs: Part 2

This WCF Tutorial is part 2 in a series of WCF Service FAQs. The previous part in this series is already published at WCF Service FAQs: Part 1.
 

What are the various ways to expose WCF Metadata?

 
By default, WCF doesn't expose metadata. We can expose it by choosing one of the following ways:
  1. In the configuration file, by enabling metadata exchange as follows:
    1. <system.serviceModel>  
    2.   <services>  
    3.     <servicenameservicename="MyService.Service1"  
    4.     behaviorConfiguration="MyService.Service1">   
    5.       <endpointaddressendpointaddress=""binding="wsHttpBinding"  
    6.       contract="MyService.IService1">  
    7.         <identity>  
    8.           <dnsvaluednsvalue="localhost"/>  
    9.         </identity>  
    10.       </endpoint>  
    11.       <endpointaddressendpointaddress="mex"binding="mexHttpBinding"  
    12.       contract="IMetadataExchange"/>  
    13.     </service>  
    14.   </services>  
    15.   <behaviors>  
    16.     <serviceBehaviors>  
    17.       <behaviornamebehaviorname="MyService.Service1">  
    18.         <serviceMetadatahttpGetEnabledserviceMetadatahttpGetEnabled="true"/>  
    19.         <serviceDebugincludeExceptionDetailInFaultsserviceDebugincludeExceptionDetailInFaults="false"/>  
    20.       </behavior>  
    21.     </serviceBehaviors>  
    22.   </behaviors>  
    23. </system.serviceModel>
  2. ServiceHost can expose a metadata exchange endpoint to access metadata at runtime.
    1. using (ServiceHost host = new ServiceHost(typeof(MyService)))  
    2. {  
    3.     ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();  
    4.     behavior.HttpGetEnabled = true;  
    5.     host.Description.Behaviors.Add(behavior);  
    6.     host.Open();  
    7.     Console.WriteLine("My Service here..........");   
    8.     Console.ReadLine();  
    9.     host.Close();  

What is mexHttpBinding in WCF?

 
To generate a proxy, we need service metadata and mexHttpBinding returns service metadata.
 
If we look into our configuration file, the service will have an endpoint with mexHttpBinding as follows:
  1. <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
and the service metadata behavior will be configured as follows
  1. <serviceMetadata httpGetEnabled="true"/> 
Before deployment of the application to a production machine, it should be disabled.
 
To support other protocols, related bindings are mexHttpBinding, mexHttpsBinding and mexTcpBinding.
 

What is a Service Proxy in WCF?

 
A service proxy or simply proxy in WCF enables application(s) to interact with a WCF Service by sending and receiving messages. It's basically a class that encapsulates service details i.e. service path, service implementation technology, platform and communication protocol etc. It contains all the methods of a service contract (signature only, not the implementation). So, when the application interacts with the service through the proxy, it gives the impression that it's communicating a local object.
 
We can create a proxy for a service by using Visual Studio or the SvcUtil.exe.
 

What are the various ways to generate a proxy in WCF?

 
Generating a proxy using Visual Studio is simple and straight forward.
  1. Right-click References and choose "Add Service Reference".
  2. Provide the base address of the service on "Add Service Reference" dialog box and click the "Go" button.
    The service will be listed below.
  3. Provide the namespace and click OK.
Visual Studio will generate a proxy automatically.
 
We can generate a proxy using the svcutil.exe utility using the command line. This utility requires a few parameters like HTTP-GET address or the metadata exchange endpoint address and a proxy filename i.e. optional. For example:
 
svcutil http://localhost/MyService/Service1.svc /out:MyServiceProxy.cs
 
If we are hosting the service at a different port (other than the default for IIS which is 80), we need to provide a port number in the base address. For example:
 
svcutil http://localhost:8080/MyService/Service1.svc /out:MyServiceProxy.cs
 
For parameter details regarding svcutil, please follow the MSDN link
 
 

What is the difference between use of ChannelFactory and Proxies in WCF?

 
If we have control over the server and client, then the ChannelFactory is a good option because it relies on having local interfaces that actually describes the service i.e. service contract.
 
On the other hand, if we don't have control over the server and only have a WSDL/URL, then it's better to generate a proxy using Visual Studio or SvcUtil.
 
SvcUtil is a better option as compared to Visual Studio because we have more control using SvcUtil.
 

How to create proxy for Non-WCF Services?

 
In case of Non-WCF Services, we can create a proxy by either using Visual Studio or the svcUtil.exe tool by pointing to a WSDL of the non-WCF service. In this scenario, we can't create a proxy through a ChannelFactory or manually developing a proxy class because we don't have local interfaces i.e. a service contract.
 

Briefly explain Automatic Activation in WCF?

 
Automatic activation means the service starts and serves the request when a message request is received, but the service doesn't need to be running in advance.
 
There are a few scenarios in which the service needs to be running in advance. For example, in the case of Self-Hosting.
 

What are the various WCF Instance Activation Methods available?

 
WCF supports three different types of Instance Activation methods:
  1. Per Call
  2. Per Session
  3. Singleton

What are the various ways to handle concurrency in WCF?

 
There are three different ways to handle concurrency in WCF; they are:
  1. Single
  2. Multiple
  3. Reentrant
Single: at a given time, only a single request can be processed by a WCF service instance. Other requests will be waiting until the first one is fully served.
 
Multiple: multiple requests can be served by multiple threads of a single WCF service instance.
 
Reentrant: a single WCF service instance can process one request at a given time but the thread can exit the service to call another service.
 
We can apply these concurrency settings by putting ConcurrencyMode property in ServiceBehavior as follows:
  1. [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple]  
  2. Public class MyService : IMyService  
  3. {  
  4. }

What is WCF throttling?

 
WCF throttling enables us to regulate the maximum number of WCF instances, concurrent calls and concurrent sessions. The basic purpose is to control our WCF service performance by using Service throttling behavior.
 
In the configuration file we can set this behavior as follows
  1. <serviceBehavior>  
  2.   <behavior name="MyServiceBehavior">  
  3.     <serviceThrottling  
  4.     maxConcurrentInstances="2147483647"  
  5.     maxConcurrentCalls="16"  
  6.     maxConcurrentSessions="10"  
  7.   </behavior>  
  8. </serviceBehavior> 
The above given values are the default ones, but we can modify them after evaluating the requirements of our application.


Similar Articles