WCF Service Binding and Multiple Endpoints

  • webHttpBinding is the REST-style binding, where you basically just hit a URL and get back a truckload of XML or JSON from the web service
  • basicHttpBinding and wsHttpBinding are two SOAP-based bindings which is quite different from REST. SOAP has the advantage of having WSDL and XSD to describe the service, its methods, and the data being passed around in great detail (REST doesn’t have anything like that – yet). On the other hand, you can’t just browse to a wsHttpBinding endpoint with your browser and look at XML – you have to use a SOAP client, e.g. the WcfTestClient or your own app.

So your first decision must be: REST vs. SOAP (or you can expose both types of endpoints from your service – that’s possible, too).

Then, between basicHttpBinding and wsHttpBinding, there differences are as follows:

  • basicHttpBinding is the very basic binding – SOAP 1.1, not much in terms of security, not much else in terms of features – but compatible to just about any SOAP client out there –> great for interoperability, weak on features and security
  • wsHttpBinding is the full-blown binding, which supports a ton of WS-* features and standards – it has lots more security features, you can use sessionful connections, you can use reliable messaging, you can use transactional control – just a lot more stuff, but wsHttpBinding is also a lot *heavier” and adds a lot of overhead to your messages as they travel across the network

Example to expose two binding:

<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true">
      <security mode="Transport" />
    </binding>
  </webHttpBinding>
  <wsHttpBinding>
    <binding name="wsHttpBindingConfig">
      <security mode="Transport">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<behaviors>
  <endpointBehaviors>
    <behavior name="EndpBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <endpointBehaviors>
    <behavior name="wsEndpBehavior">
    </behavior>
  </endpointBehaviors>

  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<services>
  <service behaviorConfiguration="ServiceBehavior" name="Services.MyService">
    <endpoint name="soap" address="" behaviorConfiguration="WsEndpBehavior" binding="wsHttpBinding"
              bindingConfiguration="wsHttpsBindingConfig" contract="Services.IMyService" />
    <endpoint name="rest" address="/rest" behaviorConfiguration="EndpBehavior" binding="webHttpBinding
              bindingConfiguration="webHttpBindingWithJsonP" contract="Services.IMyService" />
  </service> 
</services>

The service interface definition:

I want to make this service can be consume through server-side (C#.Net) and client-side (jQuery & Cross Domain).

Cross Domain javascript will only work with “WebGet”.

[ServiceContract]
public interface IMyService
{
  [WebGet(RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
  [OperationContract]
  bool MyMethod(string msg);
}

Consume WCF Service

C#.Net with Proxy

WSHttpBinding binding = new WSHttpBinding(SecurityMode.Transport);

EndpointAddress endpoint = new EndpointAddress("https://dev.service.com/WCFService/MyService.svc");

ChannelFactory<IMyService> factory = new ChannelFactory<IMyService>(binding, endpoint);

IMyService proxy = factory.CreateChannel();

bool value = proxy.MyMethod("test");

((IClientChannel)proxy).Close();

factory.Close();

C#.Net with Service Reference

When you reference the service on VS, it will gerneate Client Node in your web.config.

<client>
  <endpoint address="https://dev.service.com/WCFService/MyService.svc"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMyService"
            contract="ServiceReference.IMyService" name="WSHttpBinding_IMyService" />
</client>

ServiceReference.MyServiceClient arsSvc = new ServiceReference.MyServiceClient(); 
arsSvc.MyMethod("test");

jQuery + JSONP

$.ajax({ 
         url: "https://dev.service.com/WCFService/MyService.svc/rest/MyMethod",
         type: "GET",
         data: { msg: "test" },
         contentType: "application/json; charset=utf-8",
         dataType: "jsonp",
         crossDomain: true,
         success: function (data) { alert(data.MyMethodResult); },
         error: function (jqXHR, textStatus, errorThrown) {
           console.log("Fail " + textStatus + " - " + errorThrown);
         }
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s