我创建了一个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>
Run Code Online (Sandbox Code Playgroud)
接口代码:
[ServiceContract]
public interface IGolfPyramidService
{
[WebInvoke(UriTemplate = "/Test", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
string Test();
}
Run Code Online (Sandbox Code Playgroud)
Myservice.cs代码:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public string Test()
{
return "success";
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够使用url格式调用该方法,如:http: //example.com/MyService.svc/test
Ars*_*had 11
如果你是初学者,那么这将指导你创建可以由IOS和android使用的json和xml启用的Web服务.
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
为什么使用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" />
Run Code Online (Sandbox Code Playgroud)
IMyService.cs
namespace WcfService1
{
[ServiceContract]
public interface IMyService
{
[WebGet(UriTemplate = "Test",
ResponseFormat = WebMessageFormat.Json
)]
[OperationContract]
string Test();
}
}
Run Code Online (Sandbox Code Playgroud)
MyService.svc.cs
namespace WcfService1
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public string Test()
{
return "Test";
}
}
}
Run Code Online (Sandbox Code Playgroud)