C# 启用 WCF 服务以与 JSON 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16144873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable WCF Service to use with JSON
提问by Finisher001
I have created a wcf service. That is working fine when i am using simply in .net by adding as a webservice. But i want to make it able to use for iPhone app as JSON call. For testing i have used it in .net with JSON but its not working.
我创建了一个 wcf 服务。当我通过添加为 web 服务在 .net 中简单使用时,这工作正常。但我想让它能够作为 JSON 调用用于 iPhone 应用程序。为了测试,我在 .net 和 JSON 中使用了它,但它不起作用。
i know this kind of question is asked before, i have looked in for this cant find solution for me.
我知道以前有人问过这种问题,我已经寻找过这个无法为我找到解决方案的问题。
my configuration:
我的配置:
<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>
interface code:
接口代码:
[ServiceContract]
public interface IGolfPyramidService
{
[WebInvoke(UriTemplate = "/Test", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
string Test();
}
Myservice.cs code:
Myservice.cs 代码:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public string Test()
{
return "success";
}
}
i want to make it possible to call the method using url format like : http://example.com/MyService.svc/test
我想让使用 url 格式调用该方法成为可能,例如:http: //example.com/MyService.svc/test
采纳答案by Arshad
if you are beginner then this will guide you create json and xml enabled web service which can be consumed by IOS and android.
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
如果您是初学者,那么这将指导您创建支持 json 和 xml 的 Web 服务,这些服务可以被 IOS 和 android 使用。
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
回答by binard
Why you use a post method to get a simple string value ? Try this example which should normally work.
为什么要使用 post 方法来获取简单的字符串值?试试这个应该正常工作的例子。
Configuration
配置
<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
IMyService.cs
namespace WcfService1
{
[ServiceContract]
public interface IMyService
{
[WebGet(UriTemplate = "Test",
ResponseFormat = WebMessageFormat.Json
)]
[OperationContract]
string Test();
}
}
MyService.svc.cs
我的服务.svc.cs
namespace WcfService1
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public string Test()
{
return "Test";
}
}
}

