2

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

ukraine
  • 172
  • 4
  • 13

2 Answers2

3

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>
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thanks for answer, but I have question. How I can test my REST service without WCF test client. Should I add WCF service application and use it for testing? – ukraine Aug 18 '11 at 09:13
  • You can use test application, you can use web browser to test GET requests and you can use Fiddler or any more suitable tool to test GET, POST and other requests. – Ladislav Mrnka Aug 18 '11 at 09:22
0

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

Community
  • 1
  • 1
Pieter Germishuys
  • 4,828
  • 1
  • 26
  • 34