Saturday, September 25, 2010

Hosting WCF Service with netTcpBinding on IIS7

Web.Config should have following configuration settinggs to hosting WCF service on IIS7(WAS):

        <binding name="tcpbinding" portSharingEnabled="true">
          <security mode="None"></security>
          <!--<security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"/>
            <message clientCredentialType="Windows"/>


Trouble Shooting on IIS7.*
If following exception thrown while adding service reference-
Error: Cannot obtain Metadata from net.tcp://win-2008.cspl:9002/Multibinding/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at Exchange Error    URI: net.tcp://win-2008.cspl:9002/Multibinding/Service1.svc    Metadata contains a reference that cannot be resolved: 'net.tcp://win-2008.cspl:9002/Multibinding/Service1.svc'.    Could not connect to net.tcp://win-2008.cspl:9002/Multibinding/Service1.svc. The connection attempt lasted for a time span of 00:00:02.0374439. TCP error code 10061: No connection could be made because the target machine actively refused it .     No connection could be made because the target machine actively refused it

Step1: Make sure following services is enable for net.tcp

a.                Net.Tcp Listener Adapter
b.               Net.Tcp Port Sharing Service

     If not then enable and start both services.

Step2: IIS Manager -> Select Hosted service -> Action pane -> Advance setting ->
           See Enable Protocol, by default http protocol there, need to specify net.tcp with comma separated

Step3: Make sure your application pool is running in classic mode

Step4: Right click on Default Website -> Edit bindings
            Make sure net.tcp protocol information there

Step5: Check firewall, Create Inbound and out bound rule for port which we are using.

Friday, September 24, 2010

WCF Service Throttling Behavior

WCF Service Throttling is a way for you to limit (“throttle”) the throughput of your service so that resources (memory, CPU, network, etc.) are kept at healthy levels.

While it is not a direct instance management technique, throttling enables you to restrain client connections and the load they place on your service. Throttling enables you to avoid maxing out your service and the underlying resources it allocates and uses. When throttling is engaged, if the settings you configure are exceeded, WCF will automatically place the pending callers in a queue and serve them out of the queue in order. If the client's call timeout expires while pending in the queue, the client will get a TimeoutException.

Using the behaviorConfiguration tag, you add to your service a custom behavior that sets throttled values.
      <service name = "MyService" behaviorConfiguration = "ThrottledBehavior">
         <behavior name = "ThrottledBehavior">
               maxConcurrentCalls     = "12"
               maxConcurrentSessions  = "34"
               maxConcurrentInstances = "56"

 I have done following test on wcf service for service throttling in different scenarios:
If maxConcurrentCalls =2 then I made 3 calls to service it will execute only two calls at a time remaining call put into the queue execute after first two calls. See following program


static void Main(string[] args)
            System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(test1));
            System.Threading.Thread th1 = new System.Threading.Thread(new System.Threading.ThreadStart(test2));
            System.Threading.Thread th2 = new System.Threading.Thread(new System.Threading.ThreadStart(test3));

        static void test1()
            ServiceReference1.Service1Client o = newConsoleApplication1.ServiceReference1.Service1Client();
           Console.WriteLine("Istance1  " + o.GetData(1));
       static void test2()
            ServiceReference1.Service1Client o = new ConsoleApplication1.ServiceReference1.Service1Client();
            Console.WriteLine("Istance2  " + o.GetData(2));
      static void test3()
            ServiceReference1.Service1Client o = new ConsoleApplication1.ServiceReference1.Service1Client();
            Console.WriteLine("Istance3  " + o.GetData(3));

Result is:

 Each call is taking 5 second to complete.In this sample first two calls execute simaultiniiously and remaining one call executes after 5 seconds.

1. Iif maxConcurrentCalls =3 then I made 3 calls to service it will execute simultaneously
Result is:

3. If maxConcurrentCalls =3 and maxConcurrentSessions="2" then only two calls will be executed rest one call is discarded and any exception will not be raised

Result is:

4. If maxConcurrentInstances="2" then same result found
Result is:

Note: Above result is found with default InstanceContextMode and ConcurrencyMode
That is [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall,ConcurrencyMode=ConcurrencyMode.Single)]