Hi all,
today I will give you an example of a single WCF application exposing data with the following endpoints at the same time:

  • SOAP 1.1 (basicHttpBinding)
  • SAOP 1.2 (wshttpBinding)
  • REST XML
  • REST JSON

So, the WCF service I propost it is hosted by Visual Studio itself as well as a Windows Service. You can download the solution here.
Once you run the solution (remember to run Visual Studio as admin!), you can test SOAP endpoints by Visual Studio WCF Test Client (that will automatically start) and REST endpoints copying paste into your web browser the following URLs:

So, let’s take a look at the Solution structure:

WCFProject

The solution is composed by:

  • Contracts: it defines the WCF Service contract, I mean the data (dto object) exposed outside the service, the Fault classes describing errors, and the service interface
  • Dal: it could be Entity Framework or something else that is able to retrieve data from a DataBase. In this example this Dll is empty, I didn’t realized a complex example. However it is clear that the DAL will be not exposed outside the service as is, but it will mapped into Data Contracts object.
  • Service Host: in this sample you can install the WCF Service as a Windows Service. The Install.bat and Uninstall.bat will do the job for you!
  • Services: the service implementation itself. You could set it as a Startup Project in fact if we take a look at the project we can see that Visual Studio will host the service and it will starts the WCFTest client as well on startup:

    wcfservice

Here you are the most interesting part: the App.config which is the same for the Service (when hosted by Visual Studio) and the Host program:


  <system.serviceModel>
    <services>
      <service name="Marzio.Pattano.Services.SampleService" behaviorConfiguration="SampleServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8081/MyService" />
          </baseAddresses>
        </host>
        <endpoint address="soapBasic" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding" contract="Marzio.Pattano.Contracts.Service.ISampleService" />
        <endpoint address="soapWs" binding="wsHttpBinding" bindingConfiguration="wsHttpBinding" contract="Marzio.Pattano.Contracts.Service.ISampleService" />
        <endpoint address="json" binding="webHttpBinding" bindingConfiguration="webHttpBindingJson" behaviorConfiguration="jsonBehavior" contract="Marzio.Pattano.Contracts.Service.ISampleService" />
        <endpoint address="xml" binding="webHttpBinding" bindingConfiguration="webHttpBindingXml" behaviorConfiguration="poxBehavior" contract="Marzio.Pattano.Contracts.Service.ISampleService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="jsonBehavior">
          <enableWebScript />
        </behavior>
        <behavior name="poxBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="SampleServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="True" />
          <serviceCredentials useIdentityConfiguration="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBinding">
          <security mode="None" />
        </binding>
      </basicHttpBinding>
      <webHttpBinding>
        <binding name="webHttpBindingJson">
          <security mode="None" />
        </binding>
        <binding name="webHttpBindingXml">
          <security mode="None" />
        </binding>
      </webHttpBinding>
      <wsHttpBinding>
        <binding name="wsHttpBinding">
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

so we basicallye have:

  • the four endpoints (soapBasic, soapWs ,json, xml) plus the endpoint MEX for SOAP.
  • the endpointBehaviors: basically json must have enableWebScript and poxBehavior webHttp
  • the bindings:
    notice that webHttpBinding has been defined twice for Json and Xml, this will solve a WCF issue! So keep both of them!

At least notice the service contract:

    [ServiceContract]
    public interface ISampleService
    {
        [OperationContract]
        [WebGet]
        Employee GetEmployee(int employeeId);
    }

where [WebGet] tells WCF that this is a REST method.

Happy coding!!!

Advertisements