How WCF request is handled in IIS - wcf

I am working on a project to improve the performance of WCF service. We have WCF service hosted on IIS server.
I went through various articles to understand the End to End flow from WCF client to request processing by WCF service.
Kindly clarify the below questions:
What happens in IIS when the WCF request comes from the client?
How the IIS identifies the request is for particular WCF service (assume that there are multiple WCF services hosted in the IIS)?
How IIS is delegating the request processing to WCF service?
Who is responsible for creating the WCF service instance?
How the ASP.Net worker threads come into WCF service processing request?
What is the role of Application pool settings in WCF service request processing?

The requests to the WCF service that is hosted on IIS are handled by WAS (Windows Activation Service) which decouples the activation architecture from IIS.
WAS is responsible for processing the requests / response for WCF service.
Here is the high level of what goes on when a http request arrives for a WCF Service hosted on WAS
Requests are first received by the http.sys kernel.
A new WAS service is responsible for configuring application pools,
among other things.
The WWW Service is responsible for activating application pools and
requests queues to process incoming requests. In addition, this
service has a new HTTP Listener Adapter that forwards requests to the
HttpHandler to process the request (without the need for
aspnet_isapi.dll).
Keeping above points in mind, here are the best answers that I can come up with.
Assuming your WCF service is listening on HTTP protocol
1. What happens in IIS when the WCF request comes from the client?
Everything happens in WAS not IIS.
When a protocol listener ( HTTP.sys) picks up a client request, WAS determines if a worker process is running or not. If an application pool already has a worker process that is servicing requests, the listener adapter passes the request onto the worker process for processing. If there is no worker process in the application pool, WAS will start a worker process so that the listener adapter can pass the request to it for processing.
The listener adapter is part of WAS. The request then flows into the worker process’ (w3wp.exe activated by WAS) app domain that has HTTP module and HTTP handler (without the need for aspnet_isapi.dll) that process this request.
2. How the IIS identifies the request is for particular WCF service (assume that there are multiple WCF services hosted in the IIS)?
Its not IIS its WAS that creates a worker process instance (w3wp.exe) and loads application’s code along with other components / network protocols in that worker process. The incomming request has an endpoint address that contains the WCF service name. Also the SOAP message contains the method and the service (class) name which is called by the client. The Channel Dispatchers and Endpoint Dispatchers use this information to activate the class instance and call the target method.
Channel and Endpoint Dispatchers
3. How IIS is delegating the request processing to WCF service?
Again its WAS that delegates the request to particular WCF service which it identifies when the request comes in.
4. Who is responsible for creating the WCF service instance?
Dispatchers are responsible for accepting new channels, receiving messages, operation dispatch and invocation, and response processing. The answer for question # 2 describes this.
5. How the ASP.Net worker threads come into WCF service processing request?
In IIS 7 and later, Windows Process Activation Service (WAS) manages application pool configuration and worker processes instead of the WWW Service.It manages application pools and worker processes for both HTTP and non-HTTP requests.
HTTP Listener adapter is responsible for bridging requests between WAS and ASP.Net worker process. For HTTP requests the adapter is provided by www services (w3svc).
6. What is the role of Application pool settings in WCF service request processing?
All of the applications inside an application pool share a common set of run-time characteristics. For example, they all run under the same version of the common language runtime (CLR) and they all share a common process identity. Each application pool corresponds to an instance of a worker process (w3wp.exe). Each managed application running inside of a shared application pool is isolated from other applications by means of a CLR AppDomain.
Once the request arrives at the ServiceHost process, WCF channel dispatchers picks it up and forward to the particular endpoint dispatchers which intern call the method (endpoint) that is responsible to processes the request.
References:
Learning WCF: A Hands-on Guide By Michele Leroux Bustamante
http://devproconnections.com/net-framework/iis-and-was-hosting-architecture
https://msdn.microsoft.com/en-us/library/ms734677(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/ms789006(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/ms734665.aspx
https://www.iis.net/learn/get-started/introduction-to-iis/introduction-to-iis-architecture#WAS

Related

WCF Service Callback Integration with Web Push API for Web Notifications

I need notifications (which comes from a WCF service callback) in my web application even when the browser is not opened.
The notification source / event I will get from a WCF service callback from WCF service hosted in the network.
I came across the Web Push API and Service Workers concepts which gives me the intended behavior (notify when even browser is closed).
Push API can send the notification to service worker and then it can process.
Only service worker can get activated even when browser is not opened for notifications.
I doubt whether I can host WCF service for the service callback even when browser is closed in contrast to service workers.
How to achieve this scenario? notifications which I get at WCF service callback in my webapp -> pass to push api -> service worker -> Notification Display.

WCF in UWP: should I explicitly call the OpenAsync() of the service client(proxy)?

I'm developing a UWP client project which need to consume some services of a WCF server. I uses the "add service reference" tool of Visual Studio to auto generate service clients(proxies). The binding type is NetTcpBinding. Below is some code snippet which create the service client:
NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.Security.Mode = SecurityMode.None;
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
tcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;
MainServiceClient = new MainServiceClient(tcpBinding, new EndpointAddress("net.tcp://localhost:8773/MyWCF/MainService/tcp"));
The question is do I need to call OpenAsync() method of MainServiceClient? It seems the service client can be auto opened when it is first called. But I read from this article that auto-opened service client would have some performance penalty. The article was written in 2007. I just wonder if this mechanism have changed today, especially in UWP project. Can anyone share more light on this topic? Thanks!
To explain this case, you should know three ways to do WCF instance management. WCF has provided three ways by which you can control WCF service instances:Per call, Per session, Single instance.
When we configure a WCF service as per call, new service instances are created for every method call you make via a WCF proxy client.
Very often we need to maintain state between method calls or for a particular session. For those kinds of scenarios, we will need to configure the service per session. In per session, only one instance of a WCF service object is created for a session interaction.
Often we would like to create one global WCF instance for all WCF clients. To create a single instance of a WCF service, we need to configure the WCF service as Single instance mode.
And there are three ways by which you can handle concurrency for each service instance in WCF :single, multiple, and reentrant.
Single: A single request has access to the WCF service object at a given moment of time. So only one request will be processed at any given moment of time. The other requests have to wait until the request processed by the WCF service is completed.
Multiple: In this scenario, multiple requests can be handled by the WCF service object at any given moment of time. In other words, requests are processed at the same time by spawning multiple threads on the WCF server object. So you have great throughput here but you need to ensure concurrency issues related to WCF server objects.
Reentrant: A single request thread has access to the WCF service object, but the thread can exit the WCF service to call another WCF service or can also call a WCF client through callback and reenter without deadlock.
In "Instance mode = Per Session and Concurrency = Single" combination, one WCF service instance is created for every WCF client session because the WCF instance mode is set to per session. All the method are executed in a sequential manner one by one. In other words, only one thread is available for all method calls for a particular service instance.
For the above scenario, you should always open WCF client proxy explicitly before you are making any calls. Because it will maintain service state between method calls and obtain high performance.
For more detail you could refer to "WCF Concurrency (Single, Multiple, and Reentrant) and Throttling" and "Three ways to do WCF instance management".

Request/response messaging pattern - Azure Service Bus

All ,
I have a doubt on the Request response pattern... Assume the following is my scenario
1.I have a service running on Windows Azure. This service can be called by users to execute a command.
2.I have a client applications that is running on my intranet. This client application will execute the command . The computer in which the client application is running is connected to internet , but does not have a static IP i.e machine cannot be accessed directly via the internet
3.I am planning to use Azure Service Bus through which my service on Windows Azure can communicate with the client application to execute....
In this scenario, can i use Request/response messaging i.e can the service post a message and expect a response from the client
OR
Should i use command queue for each client , the Service will push the command to be executed on a queue , the client will poll the queue and execute a command
Any help is appreciated
Since you are using WCF (based on the tag), you should consider using Service Bus Relay calling the WCF service asynchronously.
I assume you want to use Relaybinding here, using WCF.
Your web service (which is behind NAT, firewall devices, etc) is only opening outbound connections in that case. The service is listening on a registered endpoint in the cloud (that is accessible for him, because of credentials and protocol). All incoming service calls are sent over that port/socket. The response will then be sent back over the outgoing port again.
If the IP Address of your service changes, it wil register itself again (by listening on the same registered endpoint) and you can reach that service transparantly.
Another way you can achieve request/response in an asynchronous fashion, is through queues. This does not require any open connection between your client and your service and can happen fully asynchronous. This can be achieved by sending a message to the request queue for your specific service (with a Correlation Id). And when that service has processed that message, it can send the response to the response queue of your application, using sessions. A good example of this pattern can be found on Alan Smith's blog: http://www.cloudcasts.net/devguide/Default.aspx?id=13051

How to build Wcf service that runs/processes request after client that published task is closed?

I am trying to build a WCF based web service hosted on IIS 7.0. WCF Service is hosted on IIS and is designed to accept requests from multiple WCF clients(console applications). Each WCF client will give/publish a task to service hosted on WCF. After publishing task client terminates or shuts down.
After a while Client will query WCF Service for status of the task it published, service will return the status either completed or terminated etc... for the query request.
I'm new to the paradigm of WCF and webservices. Can someone help me with WCF concepts that can help me achieve this. samples are appreciated.
Thanks,
--Prasad
If I understand you correctly, then you're looking for a WCF Service that keeps running after the client closes the connection and then make a new connection to that same service at a later time.
From the service's point of view, that new connection is a new, different client - so you're sharing the same service with multiple clients.
You can achieve this with the InstanceContextMode.Single service behavior:
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class EvalService : IEvalService { ...
See The lifetime of an instance of a WCF service?.

Can I listen to a WCF event from a web client?

Can I listen to a WCF event from a web client? Is this possible? I am not talking about call backs, I want the WCF service to raise and event and the web client to be able to listen. Is there a good example of this in C#?
There are no events in WCF. If you want mimic event you still have to call some operation exposed on all clients = you must call WCF service or callback exposed on client.
What do you mean by web client? Do you mean javascript code running in web browser? In such case no you can't achieve that with WCF. You can only use AJAX calls from borowser and continuously poll the service for possible "event".
If you mean ASP.NET application then the answer is theoretically yes, practiacally it will be pretty hard. The reason is that in ASP.NET you handle only current HTTP request by some handler - for example Page. The lifetime of the handler is only for serving the single request. Due to that using duplex service doesn't make to much sense because for receiving callbacks by duplex service your client proxy must live. If you open the proxy in Page it will die after serving the request. If you open the proxy in separate thread you must somehow corelate incomming callbacks to actual client but the client still have to poll the web server to be notified about callbacks. Similar situation will be with exposing the service on ASP.NET application.
Difference between asynchronnous and duplex calls is big. In asynchronnous pattern single request always have single response. Resonse is not sent without request. In duplex pattern you can make single request and receive thousands callback from server.