first_page

One WCF Service for Rest and Binary

икони на светци

The sweet spot of the moment is standing in a WCF design that tries to stay with one service contract per tiny application (concern). The motivation to do this is for ease of web.config maintenance where there will be one service declaration in the collection of services in system.serviceModel.

The desire for one service definition leads (me) to learning how to build a service contract that supports binaryMessageEncoding and text-based “rest” WebGet or WebInvoke. This desire is reinforced by the following observations (seen in Visual Studio):

  • Adding a new WCF Service item for the first time auto-generates behaviors and serviceHostingEnvironment declarations in system.serviceModel. No service declaration is generated—which is makes the configuration more maintainable.
  • Adding a new AJAX-enable WCF Service item generates a service definition because it needs an explicit endpoint definition with an AJAX-friendly binding and behavior configuration.
  • Adding a new AJAX-enable WCF Service item after adding a WCF Service item causes the first WCF service to break—so the idea of trying to get MEX and rest by making a few Visual Studio moves with “smart” defaults (without Glenn Block’s help) is not realistic.### The System Service Model Service Definition

Here is that single service declaration promised earlier:<services> <service name="Songhay.Web.HostRoot.Services.WcfServiceOne" behaviorConfiguration="mexServiceBehavior"> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <endpoint address="" binding="customBinding" bindingConfiguration="customHttpBinding" contract="Songhay.Web.HostRoot.Services.IWcfServiceOne" /> <endpoint address="json" behaviorConfiguration="ajaxBehavior" binding="webHttpBinding" contract="Songhay.Web.HostRoot.Services.IWcfServiceOne" /> </service> </services>The customHttpBinding and ajaxBehavior most distinguish this little design. Respectively, we have:<bindings> <customBinding> <binding name="customHttpBinding"> <binaryMessageEncoding /> <httpTransport /> </binding> </customBinding> </bindings>and<endpointBehaviors> <behavior name="ajaxBehavior"> <enableWebScript /> <webHttp helpEnabled="true"/> </behavior> </endpointBehaviors>Also, notice that the customBinding and webHttpBinding are not sharing the same path such that http://localhost/MyService.svc/ would be for binary transport and http://localhost/MyService.svc/json/ would be for the AJAX behavior.

Selected Resources

rasx()