This WCF tutorial is part-2 in series of WCF Interview Questions. Other parts in this series can be found here.
WCF Interview Questions List – Part 2
- What are the different ways to expose WCF Metadata?
- What is mexHttpBinding in WCF?
- What is a Service Proxy in Windows Communication Foundation?
- What are the different ways to generate proxy in WCF?
- Difference between using ChannelFactory and Proxies in WCF?
- How to create proxy for Non-WCF Services?
- Briefly explain Automatic Activation in WCF?
- What are the different WCF Instance Activation Methods available?
- What are the different ways to handle concurrency in WCF?
- What is WCF throttling?
By default, WCF doesn’t expose metadata. We can expose it by choosing one of the following ways:
1. In configuration file, by enabling metadata exchange as follows:
2. ServiceHost can expose a metadata exchange endpoint to access metadata at runtime.
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
Console.WriteLine(“My Service here……….”); Console.ReadLine();
In order to generate proxy, we need service metadata and mexHttpBinding is the binding that returns service metadata.
If we look into our configuration file, service will have an endpoint with mexHttpBinding as follows:
and service metadata behavior will be configured as follows:
Before deployment of application to production machine, it should be disabled.
In order to support other protocols, related bindings are:
A service proxy or simply proxy in WCF enables application(s) to interact with 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 service contract (signature only, not the implementation). So, when the application interact the service through proxy, it gives the impression that it’s communicating a local object.
We can create proxy for a service by using Visual Studio or SvcUtil.exe.
Back to top
Generating proxy using Visual Studio is simple and straight forward.
- Right click References and choose “Add Service Reference”.
- Provide base address of the service on “Add Service Reference” dialog box and click “Go” button. Service will be listed below.
- Provide namespace and click OK.
Visual studio will generate a proxy automatically.
We can generate proxy using svcutil.exe utility using command line. This utility requires few parameters like HTTP-GET address or the metadata exchange endpoint address and a proxy filename i.e. optional.
svcutil http://localhost/MyService/Service1.svc /out:MyServiceProxy.cs
If we are hosting the service at a different port(other than default for IIS which is 80), we need to provide port number in base address.
svcutil http://localhost:8080/MyService/Service1.svc /out:MyServiceProxy.cs
For parameter details regarding svcutil, please follow the MSDN link
Please follow here for a complete detailed WCF Tutorial on creating Service Proxy
A ChannelFactory creates a kind of Channel used by clients to communicate with service endpoints. If we have control over Server and Client, then 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 server and only have WSDL/URL, then it’s better to generate proxy using Visual Studio or SvcUtil. SvcUtil is better option as compared to Visual Studio because we have more control in case of SvcUtil.
In case of Non-WCF Services, we can create proxy by either using Visual Studio or svcUtil.exe tool by pointing to WSDL of the non-WCF service. In this scenario, we can’t create proxy through ChannelFactory or manually developing proxy class because we don’t have local interfaces i.e. service contract.
Automatic activation means service starts and serves the request when a message request is received, but service doesn’t need to be running in advance.
Both IIS (Internet Information Services) and WAS (Windows Activation Service) supports automatic activation. It means if your service is hosted in IIS or WAS, then it will be activated automatically as a new message
arrives. But there are few scenarios in which service needs to be running in advance, for example, in case of Self-
Note: WAS (Windows Activation Service) is a process activation mechanism introduced in IIS 7.0 that supports other protocols (e.g. TCP, NamedPipes etc.) along with existing HTTP.
If you want to go into details and see the implementation of different available hosting options in WCF, you can follow the below WCF Service Hosting Tutorials.
- WCF Console Hosting
- Hosting in Windows Service
- Hosting in IIS (Internet Information Service)
- WCF WAS (Windows Activation Service) Hosting
WCF supports three different types of Instance Activation methods:
- Per Call: A new instance is created against each incoming request from client and later disposed off as response generated.
- Per Session: an instance for each session.
- Singleton: All incoming requests are served by only one instance.
For details on WCF Instance Management, please refer other article “3 techniques for Instance Management in WCF”.
There are three different ways to handle concurrency in WCF that are:
Single: means at a given time, only a single request can be processed by WCF service instance. Other requests will be waiting until the first one is fully served.
Multiple: means multiple requests can be served by multiple threads of a single WCF service instance.
Reentrant: means 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:
WCF throttling enables us to regulate the maximum number of WCF instances, concurrent calls and concurrent sessions. Basic purpose is to control our WCF service performance by using Service throttling behavior.
In configuration file we can set this behavior as follows:
Above given values are the default ones, but we can update it after looking into the requirements of our application.
Top 10 Interview Questions and Answers Series:
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions