Search code examples
jsonwcfwcf-bindingwcf-rest

WCF and JSON binding


I am trying to create a wcf service that returns json. I have some problems with my config file and i also don't know how to test it.

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="ContactLibraryJSON.ContactLibrary">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="JSONEndpointBehavior"
          contract="ContactLibraryJSON.IContactServiceJSON" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.1.31/ContactLibraryJSON" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="JSONEndpointBehavior">
          <webHttp/>
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

I still get

"Cannot add the behavior extension webhttp to the service behavior names JSONEndpointBehavior because the underlying behavior type does not implement the IServiceBehaviorInterface"

Contact is defined like :

[DataContract(Name="Contact")]
public class Contact
{        
    [DataMember(Name="FirstName")]
    public string firstName=null;
    [DataMember(Name="LastName")]
    public string lastName=null;
    [DataMember(Name="Email")]
    public string email=null;
    [DataMember(Name = "Age")]
    public int age = 0;
    [DataMember(Name = "Street")]
    public string street=null;
    [DataMember(Name = "City")]
    public string city=null;
    [DataMember(Name = "Country")]
    public string country=null;
}

IContactService is defined like :

[ServiceContract]
public interface IContactServiceJSON
{
    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "")]
    Contact GetContact();        
}

The implementation of GetContact:

public Contact GetContact()
{
    return new Contact()
    {
        firstName = "primulNume",
        lastName = "alDoileaNume",
        age = 33,
        city = "Cluj",
        country = "Romania",
        email = "ceva@mail.com",
        street = "Bizusa 8"
    };
}

My service runs on another computer in my lan. Base address is like: http://192.168.1.xxx/ContactLibraryService. ContactLibraryService is hosted by IIS and is converted to an application.


Solution

  • It's not good that you have Service Contract IContact and Data Contact Contact. Rename Service Contract like IContactService.

    <services>
      <service name="ContactLibrary.ContactService">
        <endpoint address="" binding="webHttpBinding" contract="ContactLibrary.IContactService"  behaviorConfiguration="JsonBehavior" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    
    <behaviors>
      <endpointBehaviors>
        <behavior name="JsonBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    

    During Debug time (it looks you have WCF library), service address will be http://localhost:8732/contact