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):

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

       </netTcpBinding>
    </bindings>



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 http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata 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.
<system.serviceModel>
   <services>
      <service name = "MyService" behaviorConfiguration = "ThrottledBehavior">
         ...
      </service>
   </services>
   <behaviors>
      <serviceBehaviors>
         <behavior name = "ThrottledBehavior">
            <serviceThrottling
               maxConcurrentCalls     = "12"
               maxConcurrentSessions  = "34"
               maxConcurrentInstances = "56"
            />
         </behavior>
      </serviceBehaviors>
   </behaviors>
</system.serviceModel>




 I have done following test on wcf service for service throttling in different scenarios:
1.        
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

<serviceThrottling
               maxConcurrentCalls="2"
               maxConcurrentSessions="34"
               maxConcurrentInstances="56"
            />


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

            Console.ReadLine();
        }
        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
<serviceThrottling
               maxConcurrentCalls="3"
               maxConcurrentSessions="34"
               maxConcurrentInstances="56"
            />
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

<serviceThrottling
               maxConcurrentCalls="3"
               maxConcurrentSessions="2"
               maxConcurrentInstances="56"
            />
Result is:

 
4. If maxConcurrentInstances="2" then same result found
<serviceThrottling
               maxConcurrentCalls="3"
               maxConcurrentSessions="12"
               maxConcurrentInstances="2"
            />
Result is:



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