【发布时间】:2016-04-17 17:59:13
【问题描述】:
我有一个简单的 WCF 服务,它将 json 作为输入/输出参数。 它可以作为基于 SOAP 的服务正常工作,并且我得到了预期的结果。
我已将 RESTFul 端点添加到服务中。 我正在使用“基于 Google”的 PostMan 来测试该服务。
我可以成功调用服务上的“Get”方法并返回 JSON 结果:
[OperationContract(IsOneWay = false)]
[WebGet(UriTemplate = "/Test/{calcID}",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
CalcResults GetTestResults(string calcID);
当我尝试访问基于“Put”的方法时,我得到以下状态: 405 方法不允许
“Put”方法的签名:
[OperationContract(IsOneWay = false)]
[WebInvoke(Method = "Post",
UriTemplate = "/GetCalculation",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
CalcResults GetCalculation(CalcInput inputValues);
这是我在 PostMan 输出窗口中看到的输出:
<body>
<div id="content">
<p class="heading1">Service</p>
<p>Method not allowed.</p>
</div>
</body>
这是我的 RESTFul 接口端点的配置设置:
<system.serviceModel>
<services>
<service name="CalcServiceLibrary.CalcService">
<endpoint address="regular" binding="wsHttpBinding" name="wsHTTPEndPoints"
contract="CalcServiceLibrary.ICalcService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="json" binding="webHttpBinding" name="wsWebHTTPEndPoint"
contract="CalcServiceLibrary.ICalcServiceRESTful"
behaviorConfiguration="RESTfulBehavior" bindingConfiguration="crossDomain">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RESTfulBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
我确定我的配置定义中有些地方不太正确。 我研究了几篇帖子,但未能提出解决方案。
新提示:: 我已将以下简单端点添加到我的服务中:
[OperationContract]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public string TestPost(string name)
{
return "Hello " + name + "!";
}
我正在使用 Postman 进行测试。这是我的邮递员屏幕的屏幕截图:
这感觉一定是某种配置问题。 有人可以指出我做错了什么的正确方向吗?
【问题讨论】: