启用WCF服务以使用JSON

时间:2013-04-22 10:30:41

标签: c# .net json wcf

我创建了一个wcf服务。当我在.net中使用添加为web服务时,这工作正常。但我想让它能够用作iPhone应用程序作为JSON调用。为了测试,我已经在.net中使用它与JSON,但它无法正常工作。

我知道之前有过这样的问题,我一直在找这个找不到适合我的解决方案。

我的配置:

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="servicebehavior">
      <serviceMetadata httpsGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="endpointBehavior">
      <enableWebScript />
      <webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json" />
    </behavior>
  </endpointBehaviors>

</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
  <service name="MyService" behaviorConfiguration="servicebehavior">
    <endpoint address=""
              behaviorConfiguration="endpointBehavior"
              binding="webHttpBinding"
              contract="IMyService" />
  </service>
</services>

界面代码:

[ServiceContract]
public interface IGolfPyramidService
{



    [WebInvoke(UriTemplate = "/Test", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    [OperationContract]
    string Test();

}

Myservice.cs代码:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{        
    public string Test()
    {
        return "success";
    }
}

我希望能够使用url格式调用方法,如: http://example.com/MyService.svc/test

2 个答案:

答案 0 :(得分:11)

如果您是初学者,那么这将指导您创建可由IOS和Android使用的json和xml启用的Web服务。
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

答案 1 :(得分:7)

为什么使用post方法获取简单的字符串值?试试这个应该正常工作的例子。

配置

  <system.serviceModel>
<services>
  <service behaviorConfiguration="RestServiceBehavior" name="WcfService1.MyService">
    <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJSONP" contract="WcfService1.IMyService" />
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingWithJSONP" />
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="RestServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

IMyService.cs

namespace WcfService1
{
[ServiceContract]
public interface IMyService
{
    [WebGet(UriTemplate = "Test",
        ResponseFormat = WebMessageFormat.Json
    )]
    [OperationContract]
    string Test();
}
}

MyService.svc.cs

namespace WcfService1
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
    public string Test()
    {
        return "Test";
    }
}
}