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
behaviorsandserviceHostingEnvironmentdeclarations insystem.serviceModel. Noservicedeclaration is generated—which is makes the configuration more maintainable. - Adding a new AJAX-enable WCF Service item generates a
servicedefinition 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
- “Exposing a WCF Service With Multiple Bindings and Endpoints”—Keith Elder: “In order to configure our service we are going to use the Microsoft Service Configuration Editor. This editor allows us to configure our WCF service and can be launched right from Visual Studio. To launch the editor, right click on the
web.configfile. An option should be in the menu that says “Edit WCF Configuration”. If it doesn’t don’t worry, sometimes, Visual Studio doesn’t [pick up] this option. A trick is to go to the tools menu in Visual Studio and select the editor from there. After the editor is launched it will then show up in the menu when you right click theweb.configfile.” - “Serialize WCF message in a binary way, and not as a SOAP Message” (“Out of the box, the
basicHttpandwsHttpbindings use text encoding—but you can change that if you want to. ThenetTcpbinding (which is the clear preferred choice behind corporate firewalls) will use binary by default.”) - “Misunderstood: Add Service Reference” (describes
configuration91.svcinfoand the other service reference files) - “Consuming WCF REST Services Using jQuery AJAX Calls”
- “WCF Web HTTP Service Help Page”
- “WCF Binding: BasicHttpBinding vs WsHttpBinding”
- “Difference between BasicHttpBinding and WsHttpBinding”
- “Simplified Configuration for WCF Services”
- “How to: Create a Service That Returns Arbitrary Data Using The WCF Web HTTP Programming Model”