Pages

Showing posts with label Custom Binding. Show all posts
Showing posts with label Custom Binding. Show all posts

Thursday, August 22, 2013

WCF - Custom Binding to support SOAP1.2



WCF Service Binding determines how a Client or rather a Consumer of the WCF service is going to communicate with it. There are many bindings available. Mostly used ones are –

  1. WSHTTPBinding
  2. WebHTTPBinding
  3. Custom Binding

This post talks about Custom Binding and how it can be implemented to support SOAP 1.2 standards. Please note that WSHTTPBinding supports SOAP 1.2 by default. However, some firms find security-related issues with using this binding. The WS-Addressing functionality is enabled for this binding which cannot be changed.
Let us first look at the Web.config settings required to implement CustomBinding –

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <bindings>
      <customBinding>
          <binding name="UPAHubAccessWS12">
            <textMessageEncoding messageVersion="Soap12" />
            <httpsTransport requireClientCertificate="True" />
          </binding>
        </customBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="BehaviorName" name="ServiceName">
        <endpoint address=""  binding="customBinding" bindingConfiguration="BindingName" contract="ServiceInterfaceName" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name=" BehaviorName">
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
</system.serviceModel>

Please make note of the following values specified in the above configuration –
- BehaviorName – Denotes the service behavior configuration to be used for the WCF service. Here, it allows HTTPS request (required for service hosted on HTTPS).

- ServiceName – Denotes the name of your WCF service

- BindingName – Denotes the name you give to your binding configuration

- ServiceInterfaceName – Denotes the name of the Interface class or the contract of your WCF service.


In one of my posts, I had created the client programmatically using C#, the above configuration settings correspond to the same code used but the initialization part is done at the Client’s end.



Wednesday, August 21, 2013

Initializing WCF client programmatically


Usually, developers follow the practice of making use of Web.config to set all the configuration values for a WCF client. But in some cases, that approach is not always applicable.

For example, if we are trying to call a WCF service through some SharePoint page which was deployed using a feature, in this case, the Web.config resides at a different location and hence the code would not be able to fetch this data.

The more desired approached is to initialize it through your code itself.

Below is the code which I had used to initialize a WCF Client for a service which uses Custom Binding and SOAP 1.2

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

public static CustomBinding GetBinding()
{
   CustomBinding binding = new CustomBinding();
   binding.Name = "Name_of_your_binding";
   binding.CloseTimeout = TimeSpan.Parse("00:05:00");
   binding.OpenTimeout = TimeSpan.Parse("00:05:00");
   binding.ReceiveTimeout = TimeSpan.Parse("00:10:00");
   binding.SendTimeout = TimeSpan.Parse("00:10:00");
   binding.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap12, System.Text.Encoding.UTF8));
   HttpsTransportBindingElement hbe = new HttpsTransportBindingElement();
   hbe.RequireClientCertificate = true;
   hbe.AllowCookies = false;
   hbe.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
   hbe.BypassProxyOnLocal = false;
   hbe.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
   hbe.KeepAliveEnabled = true;
   hbe.ManualAddressing = false;
   hbe.MaxBufferPoolSize = Convert.ToInt64(int.MaxValue);
   hbe.MaxBufferSize = int.MaxValue;
   hbe.MaxReceivedMessageSize = Convert.ToInt64(int.MaxValue);
   hbe.ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
   hbe.Realm = "";
   hbe.TransferMode = TransferMode.Buffered;
   hbe.UnsafeConnectionNtlmAuthentication = false;
   hbe.UseDefaultWebProxy = true;
   binding.Elements.Add(hbe);
   return binding;
}


public static EndpointAddress GetEndpointAddress()
{
   EndpointAddress endpoint = new EndpointAddress(<URL_string>);
   return endpoint;
}


public static void Main(string[] args)
{
   CustomBinding binding = GetBinding();
   EndpointAddress endpoint = GetEndpointAddress();
   <Proxy_Client_Name> client = new <Proxy_Client_Name>(binding, endpoint);
}

Above, Proxy_Client_Name denotes the name specified for your Client Class in the WCF Service Proxy file which you might have generated from your WSDL or must have been handed over to you by the WCF service providers.

One of the drawbacks of using this approach is that you cannot change the configuration settings at run-time. Apart from that, I would personally suggest all of you to go ahead with it :) ...