wcf服务返回“方法不允许”错误

时间:2012-12-27 07:48:32

标签: wcf rest

我正在尝试将对象参数作为json格式传递给wcf restful service。

像这样的服务conratc代码;

 
[WebInvoke(
    Method = "POST",
    BodyStyle = WebMessageBodyStyle.Wrapped,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    UriTemplate="PR")]
[OperationContract]
TWorkRequestPostResult PostRequest(TWorkRequestPostArgs args);

我的web.config文件就像这样;

 
<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule"/>
    </modules>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>  

当我尝试使用“http:// localhost / serviceurl / PR”url调用服务时,Service返回“Method not allowed”错误消息。

1 个答案:

答案 0 :(得分:20)

您是否从浏览器调用该服务?如果是这样,浏览器使用HTTP GET请求服务,而服务方法映射到HTTP POST Method = "POST",从而导致错误"Method not allowed"

要修复,如果对REST有意义或尝试从支持POST的工具调用服务方法,请更改为Method = "GET",例如FiddlerWcfTestClient