sebastian.roibu
sebastian.roibu

Reputation: 2859

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 = "[email protected]",
        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.

Upvotes: 4

Views: 13741

Answers (3)

Nilesh Rathod
Nilesh Rathod

Reputation: 149

I think you missed bindings here you have to add webHttpBinding and basichttpbing under the bindings tab in system.serviceModel tag.

    <services>
  <service name="VInfotech.Server.Intranet.IntranetService" behaviorConfiguration="IntranetService.Service1Behavior">
    <!-- Service Endpoints -->
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="Binding1" contract="VInfotech.Server.Intranet.IIntranet">
      <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
  </service>
  <service name="VInfotech.Server.Intranet.MobileServicesController" behaviorConfiguration="ServBehave">
    <endpoint address="RestService" bindingConfiguration="StreamedRequestWebBinding" binding="webHttpBinding" behaviorConfiguration="restPoxBehavior" contract="VInfotech.Server.Intranet.IMobileServices" />
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding name="StreamedRequestWebBinding"
    bypassProxyOnLocal="true"
             useDefaultWebProxy="false"
             hostNameComparisonMode="WeakWildcard"
             sendTimeout="10:15:00"
             openTimeout="10:15:00"
             receiveTimeout="10:15:00"
             maxReceivedMessageSize="9223372036854775807"
             maxBufferPoolSize="9223372036854775807"
             maxBufferSize="2147483647"
             transferMode="StreamedRequest" >
      <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
    </binding>
  </webHttpBinding>
  <basicHttpBinding>
    <binding name="Binding1" closeTimeout="04:01:00" openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1073741824" maxBufferPoolSize="1073741824" maxReceivedMessageSize="1073741824" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
      <readerQuotas maxDepth="1073741824" maxStringContentLength="1073741824" maxArrayLength="1073741824" maxBytesPerRead="1073741824" maxNameTableCharCount="1073741824"/>
    </binding>
    <!-- For Cyber Source bindings-->
    <binding name="ITransactionProcessor" closeTimeout="04:01:00" openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1073741824" maxBufferPoolSize="1073741824" maxReceivedMessageSize="1073741824" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
      <readerQuotas maxDepth="1073741824" maxStringContentLength="1073741824" maxArrayLength="1073741824" maxBytesPerRead="1073741824" maxNameTableCharCount="1073741824"/>
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
        <message clientCredentialType="UserName" algorithmSuite="Default"/>
      </security>
    </binding>
  </basicHttpBinding>
  <!--Cyber Source bindings ends here-->
</bindings>

Upvotes: 2

paramosh
paramosh

Reputation: 2258

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

Upvotes: 4

carlosfigueira
carlosfigueira

Reputation: 87238

You need to add the <webHttp/> to the list of endpoint behaviors. Also, the endpoint needs to use the webHttpBinding. And finally, to respond to GET HTTP requests, you need to use the WebGet attribute (instead of WebInvoke(Method="GET").

  <system.serviceModel> 
    <services> 
      <service name="ContactLibrary.ContactLibrary"> 
        <endpoint address=""
                  binding="webHttpBinding"
                  behaviorConfiguration="JSONEndpointBehavior" 
                  contract="ContactLibrary.IContact"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
        <endpoint address="ws"
                  binding="wsHttpBinding"
                  bindingConfiguration="" 
                  contract="ContactLibrary.IContact" /> 
        <host> 
          <baseAddresses> 
            <add baseAddress="http://localhost/ContactLibrary" /> 
          </baseAddresses> 
        </host> 
      </service> 
    </services> 
    <behaviors> 
      <endpointBehaviors> 
        <behavior name="JSONEndpointBehavior"> 
          <webHttp/> 
        </behavior> 
      </endpointBehaviors> 
      <serviceBehaviors> 
        <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> 
  </system.serviceModel> 

And the service contract:

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

Upvotes: 5

Related Questions