Friday, May 4, 2012

WCF EndPoints

End Points :

All communication with a Windows Communication Foundation (WCF) service occurs through the endpoints of the service.

Each endpoint consists of following  properties:

  • Address
  • Binding
  • Contract
    If no endpoints are specified in code or in configuration, then the runtime adds one default endpoint on each base address for each contract implemented by the service.
Address :
            Addresses is one of important element of  end  points .
            It is URL by which the endpoint can be reached or where service located


There are two ways to specify endpoint addresses for a service in WCF. 
You can specify an absolute address for each endpoint associated with the service 

Also you can provide a base address of service host and relative address in end point. 


Binding   : It indicate how the service could be used.
 Its mainly  specifies which protocol (such as HTTP, TCP, and MSMQ),encoding (such as text, binary, or MTOM),security to be used.



Some common bindings are
  • BasicHttpBinding , 
  • WSHttpBinding , 
  • WS2007HttpBinding
  • WSDualHttpBinding , 
  • WSFederationHttpBinding ,  
  • WS2007FederationHttpBinding ,
  • NetTcpBinding ,
  • NetNamedPipeBinding , 
  • NetMsmqBinding ,
  • NetPeerTcpBinding ,
  • MsmqIntegrationBinding
  




Configuring Timeout Values on a Binding :


There are a number of timeout settings available in WCF bindings.

Correct timeout settings  improve service’s performance but also play a role in the usability and security of service





 
SendTimeout – used to initialize the OperationTimeout, which governs the whole process of sending a message, including receiving a reply message for a request/reply service operation. This timeout also applies when sending reply messages from a callback contract method.

OpenTimeout – used when opening channels when no explicit timeout value is specified

CloseTimeout – used when closing channels when no explicit timeout value is specified

ReceiveTimeout is not used
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding openTimeout="00:10:00"
                 closeTimeout="00:10:00"
                 sendTimeout="00:10:00"
                 receiveTimeout="00:10:00">
        </binding>
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>





Contract  : Contracts are  standard way of describing what the service does 

What operations can be called by a client.

The form of the message.

The type of input parameters or data required to call the operation.

What type of processing or response message the client can expect. 


 Contract should be an Interface or class but for best practice use interface.


BehaviorConfiguration :  Specifies the name of one of the behavior elements found in the behaviors element. The specified behavior governs actions such as whether the service allows impersonation. If its value is the empty name or no behaviorConfiguration is provided then the default set of service behaviors is added to the service.


BindingConfiguration: If the default values of a binding must be modified, this can be done by configuring the appropriate binding element in the bindings element. This attribute should be given the same value as the name attribute of the binding element that is used to change the defaults. If no name is given, or no bindingConfiguration is specified in the binding, then the default binding of the binding type is used in the endpoint.

   
Multiple IIS Binding Support in .NET Framework 4 and later





<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" >
    </serviceHostingEnvironment>
  </system.serviceModel>
 
 




<services> Element :-

 <system.serviceModel> section contains a <service> element for each service.

The <service> element contains a collection of <endpoint> elements that specify the endpoints exposed for each service and optionally a set of service behaviors.


 
<system.serviceModel>
 <
services>     
  <service>
        <host>                       
              <baseAddresses>
             <add  baseAddress="http://localhost:8080/ServiceModelSamples/service.svc"/>
                WCF allow us to give multiple base addresses for each type of protocol
             <add  baseAddress=" net.tcp://localhost:9000/ssamples/TCPservice.svc"/>
                </baseAddresses>
        </host>

The relative address specify, so the endpoint address is the http://localhost:8080/servicemodelsamples/service/Addresservice1.
   <endpoint   address="/AddressService1"   binding="wsHttpBinding"     contract="MyInterface" />
  The relative address is empty (""), so the endpoint address is the same as the base address.
   <endpoint   address=""      binding="wsHttpBinding"    contract="MyInterface2" />

This endpoint address specifies an absolute address with service
 <endpoint  address="http://localhost:8080/Samples/sEmp.svc"     binding="wsHttpBinding"     contract="MyInterface3" />

This endpoint address specifies an absolute address and a different transport
<endpoint address="net.tcp://localhost:9000/ssamples/service.svc"      binding="netTcpBinding"   contract="MyInterface3" >

           </service>
   </services>
</system.serviceModel>


Name element in End Point

<endpoint  address="http://localhost:8080/Samples/sEmp.svc"    Name =”bindingName”     binding="wsHttpBinding"     contract="MyInterface3" />

On client side when create service object then pass binding name
sEmp proxy1 =  new sEmp (bindingName);

 

Defining Endpoint Addresses in Code

Uri baseAddress = new Uri("http://localhost:8080/MyServiceBase");
URI specified for the endpoint address can be a fully-qualified path or a path that is relative to the service's base address
string address = "http://localhost: 8080/ MyServiceBase/MyService";

using (ServiceHost serviceHost = new ServiceHost(typeof(HelloService), baseAddress))
{
    serviceHost.AddServiceEndpoint(typeof(IHello), new BasicHttpBinding(), address);
     or  Add a relative address 
      serviceHost.AddServiceEndpoint(typeof(IHello), new BasicHttpBinding(), "MyService");
 
    serviceHost.Open();
      //do something
    serviceHost.Close();
}


 


End Points Behavior Configuration: 


<system.serviceModel>
<behaviors>
               <endpointBehaviors>
                                <behavior name="Behavior1">
               <serviceMetadata httpGetEnabled="True" />
               <callbackDebug  includeExceptionDetailInFaults="true" />
                       </behavior> 
              </endpointBehaviors>
    </behaviors>
 <services>     
  <service>
        <endpoint   address="http://localhost:8080/Samples/sEmp.svc"  behaviorConfiguration=" Behavior1"                                       binding="wsHttpBinding"     contract="MyInterface3" />

           </service>
   </services>
</system.serviceModel>

The behaviorConfiguration attribute is used to specify which <behavior> of the <endpointBehaviors> the endpoint should use. Its value is matched with the name attribute of the <behavior> element
The behaviorConfiguration element is also optional and Each behavior element is identified by its unique name attribute





End Points Binding  Configuration: 
 
<system.serviceModel>
In framework 4.0 set multipleSiteBindingsEnabled=true for multiple binding option
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 

<behaviors>
               <endpointBehaviors>
                                <behavior name="Behavior1">
               <serviceMetadata httpGetEnabled="True" />
               <callbackDebug  includeExceptionDetailInFaults="true" />
                       </behavior> 
                                <behavior name="Behavior2">
               <serviceMetadata httpGetEnabled="True" />
               <callbackDebug  includeExceptionDetailInFaults="true" />
                       </behavior
              </endpointBehaviors>
    </behaviors>

<bindings>
      <wsHttpBinding>
        <binding 
name="wsMyServiceBindingSample1"           maxReceivedMessageSize="2147437"
openTimeout="00:10:20"
 receiveTimeout="00:30:00"
sendTimeout="00:40:00"
closeTimeout="00:10:00" 
maxBufferPoolSize="34483647"/ >
          <binding             name="wsMyServiceBindingSample2"             maxReceivedMessageSize="2147437"
                                    openTimeout="00:10:20"                              receiveTimeout="00:30:00"
                                  sendTimeout="00:40:00    closeTimeout="00:30:00"      maxBufferPoolSize="34483647" />
      </wsHttpBinding>

    <basicHttpBinding>
               <binding   name=”basicMyBindingConfig1”         closeTimeout="00:10:00"  maxReceiveBufferSize="100" />
               <binding   name=”basicMyBindingConfig2"         maxBufferSize="200"          maxReceiveBufferSize="200" />
                Default   basic http binding
                <binding     maxBufferSize="100"    maxReceiveBufferSize="100" />
   </basicHttpBinding>

    </bindings>

<services>     
  <service>
                 <endpoint   address="http://localhost:8080/Samples/sEmp.svc"   behaviorConfiguration=" Behavior1"
                                        bindingConfiguration =" basicMyBindingConfig1"     contract="MyInterface1" />

         <endpoint   address="http://localhost:8080/Samples/sEmpService2.svc"   behaviorConfiguration=" Behavior2"
                                 binding=" basicHttpBinding  bindingConfiguration=" basicMyBindingConfig2"     contract="MyInterface1" />
        This end point take default binding configuration because we dedn’t specify binding configuration
        <endpoint   address="http://localhost:8080/Samples/sEmpService2.svc"   behaviorConfiguration=" Behavior2"
                              binding=" basicHttpBinding "       contract="MyInterface1" />
         <endpoint   address="http://localhost:8080/Samples/sEmp.svc"   behaviorConfiguration=" Behavior1"
                  binding="wsHttpBinding"   bindingConfiguration =" wsMyServiceBindingSample1"    contract="MyInterface1" />
         <endpoint   address="http://localhost:8080/Samples/sEmp.svc"   behaviorConfiguration=" Behavior1"
                  binding="wsHttpBinding"   bindingConfiguration =" wsMyServiceBindingSample2"    contract="MyInterface1" />
           </service>
   </services>
</system.serviceModel>





Example 2 : 

 
<system.serviceModel>
    <bindings>
 
      <basicHttpBinding>
<binding name=”basicConfig1messageEncoding=”Text”>    <security mode=”Transport”/>       </binding>
  <binding name=”basicConfig2messageEncoding=”Mtom”>   <security mode=”Transport”/>    </binding>
      </basicHttpBinding>
 
 
      <wsHttpBinding>
        <binding name=”wsConfigBinding”  transactionFlow=”true”>
              <security mode=”TransportWithMessageCredential”>
              <message clientCredentialType=”Username”/>
             <reliableSession enabled=”trueordered=”true”/>
        </binding>
      </wsHttpBinding>
 
 
      <netTcpBinding>
        <binding name=”tcpConfigBinding”  transactionFlow=”true”     maxBufferSize="129873" >
          <security mode=”None”/>
          <reliableSession enabled=”true” />
        </binding>
      </netTcpBinding>
 
    </bindings>
 
    <services>
          <service name=”personalChatService”>
        <endpoint  address=”TEXT”       binding=”basicHttpBinding”    bindingConfiguration=”basicConfig1”          contract=”INTChart” />
        <endpoint  address=”MTOM”   binding=”basicHttpBinding”    bindingConfiguration=”basicConfig2”          contract=” INTChart” />
        <endpoint address=”secure”   binding=”wsHttpBinding”        bindingConfiguration=” wsConfigBinding”   contract=” INTChart” />
        <endpoint address=””               binding=”netTcpBinding”        bindingConfiguration=” tcpConfigBinding”  contract=” INTChart” />
            </service>
    </services> 
  </system.serviceModel>



 



Service Behavior Configuration  :

<system.serviceModel>
   <behaviors>
               <endpointBehaviors>
                            <behavior name=" EndpointBehavior1">                
               <callbackDebug  includeExceptionDetailInFaults="true" />
              </behavior> 
              </endpointBehaviors>
    </behaviors>
<serviceBehaviors>
   <behavior   name="Servicebehavior1">
    <serviceMetadata httpGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
   <behavior   name="Servicebehavior2">
    <serviceMetadata httpGetEnabled="false"/>
    </behavior>
  </serviceBehaviors>
 <services> 
                  <service  behaviorConfiguration=" Servicebehavior1"  name="ServicebehaviorOne">
                 <endpoint   address=http://localhost:8080/Samples/sEmp.svc    behaviorConfiguration=" EndpointBehavior1"
                                        binding="wsHttpBinding"     contract="MyInterface3" />
    </service>
<service  behaviorConfiguration=" Servicebehavior2" name="ServicebehaviorTwo">
                 <endpoint   address=http://localhost:8080/Samples/sEmp.svc    behaviorConfiguration=" EndpointBehavior1"
                                        binding="wsHttpBinding"     contract="MyInterface3" />
           </service>
   </services>
</system.serviceModel>

NOTE:  <remove> and <clear> tags are used to clear or remove behavior. These features can be used at different level of configuration hierarchy where if user want to clear or remove behavior configuration at child level.

No comments :

Post a Comment