ukraine
ukraine

Reputation: 172

WCF and problem with JSON

Gentelmens,

I'm working with WCF services and I would like to see result as JSON. But I don't have any ideas how to do it. I have marked my method as

[WebGet(ResponseFormat = WebMessageFormat.Json)]

but this doesn't help me. In "Wcf test client" I see result as xml.

I have tried to add behaviour(WebHttp with Json) to endpoint but I have error:

The endpoint at 'http://localhost:8732/Design_Time_Addresses/PlayingWithWCF2/Service1/' does not have a Binding
with the None MessageVersion.  'System.ServiceModel.Description.WebHttpBehavior' is only intended for use 
with WebHttpBinding or similar bindings.

DataContract:

  [DataContract]
  public class Foo
  {
    [DataMember]
    public string Name { get; set; } 
  }

Interface:

  [ServiceContract]
  public interface IFooService
  {
    [OperationContract]
    List<Foo> GetList();
  }

Service:

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  public class FooSerivce : IFooService
  {
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public List<Foo> GetList()
    {
      return new List<Foo> {new Foo {Name = "Name1"}, new Foo {Name = "Name2"}};
    }
  }

Web.config:

<system.serviceModel>
    <services>
      <service name="PlayingWithWCF2.FooSerivce">
        <endpoint address="" behaviorConfiguration="" binding="wsHttpBinding"
         contract="PlayingWithWCF2.IFooService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/PlayingWithWCF2/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Could you give me several advices how I can solve this problem?

P.S. Sorry if question is stupid but I'm newbie in WCF

Upvotes: 2

Views: 5007

Answers (2)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

To return JSON you must use REST service which is not supported by WCF test client. You have defined your service to support REST but you didn't configure it to expose REST endpoint. You need this configuration:

  <system.serviceModel>
    <services>
      <service name="PlayingWithWCF2.FooSerivce">
        <endpoint address="webHttp" binding="webHttpBinding" contract="PlayingWithWCF2.IFooService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/PlayingWithWCF2/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

Upvotes: 3

Pieter Germishuys
Pieter Germishuys

Reputation: 4886

I believe that your problem lies in your web.config, your binding configuration should be set to webHttp. Have a look at the following question/answer on stackoverflow

Upvotes: 0

Related Questions