【问题标题】:WCF REST call returns "Method not allowed" exceptionWCF REST 调用返回“方法不允许”异常
【发布时间】:2013-05-06 13:35:16
【问题描述】:

我正在尝试创建 WCF Restful 服务。

这是我的合同:(ActivateUser 类扩展了 BaseResult 类)。

namespace SmartShopServerContract {
[ServiceContract]
public interface IService {
    [OperationContract]
    [WebGet(RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json, UriTemplate="au/{eMail}/{password}/{idHandy}")]
    ActivateUserResult ActivateUser(string eMail, string password, string idHandy);
}

// Basisklasse der Resultate --> alle Resultate haben einen definierten Status!
[DataContract]
public abstract class BaseResult {

    private string status;

    public BaseResult(string status) {
        this.status = status;   
    }

    [DataMember]
    public string Status {
        get { return this.status; } 
    }
}

// Result für ActivateUser
[DataContract]
public class ActivateUserResult : BaseResult {
    public ActivateUserResult(string status)
        : base(status) {
    }

    [DataMember]
    public string CryptedPassword { get; set; }
}

}

这里是Service的实现:

namespace SmartShopServerService {
public class ServiceSmartShop : IService {

    public ActivateUserResult ActivateUser(string eMail, string password, string idHandy) {
        return new ActivateUserResult("OK") {
            CryptedPassword="testatsa"
        };
    }

还有 Web.config 文件:

    <?xml version="1.0"?>
<configuration>
  <system.serviceModel>
     <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json">
        </standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>
    <services>
      <service name="SmartShopServerService.ServiceSmartShop" behaviorConfiguration="RESTBehavior">
        <endpoint address="/" binding="webHttpBinding" contract="SmartShopServerContract.IService" behaviorConfiguration="SmartShopBehavior"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RESTBehavior">
          <serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="SmartShopBehavior">
          <webHttp automaticFormatSelectionEnabled="false"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.5" sku=".NETFramework,Version=v4.5"/>
  </startup>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="WebDAVModule"/>
    </modules>
  </system.webServer>
</configuration>

我正在使用“VS2012 本机工具命令提示符”进行测试,如下所示:

svcutil.exe http://localhost:51162/SmartShopService.svc/au/data1/data2/data3

代码正在执行,但我仍然收到 Method not allowed (405) 异常。

对此有什么想法吗?

--> 当前本地使用

--> IIS Express (Visual Studio 2012)

【问题讨论】:

  • 这个问题和答案可能会提供一些见解。 stackoverflow.com/a/545754/1181408
  • 你是如何调用http操作的?即显示 javascript/jquery 或 WebClient/etc...
  • 获得此错误的一种方法是使用错误的动词进行调用。例如您已经为 GET 定义了它,如果您使用 POST 来调用该 URL,那么 WebAPI 框架将返回 Method Not Allowed 错误。
  • 我已经在使用 [WebGet()] 注释。 (见上面的代码)。正在使用 vs2012 本机工具命令提示符(svcutil.exe)-> 上面的代码调用 http 操作;)。使用 svcutil.exe localhost:51162/SmartShopService.svc/au/data1/data2/data3 命令,我可以使用 GET 调用方法。
  • 不清楚你在做什么。 svcutil 用于生成客户端代理代码以调用 Web 动词——但您只提供了实现该操作的服务器端代码。在不知道该服务器代码是如何被调用的情况下,我们无法判断出了什么问题。您说“我可以使用 GET 调用该方法”,这是否意味着它有效?

标签: c# wcf rest exception


【解决方案1】:

您正在使用 svcutil 为“RESTful WCF 服务”创建代理。 This does not work。快速的原因是 Web 端点不公开元数据,以便 svcutil 工具知道它需要向它发送什么请求。长版本在链接的博客文章中。

【讨论】:

  • 感谢您的回复。你说的是真的,但这不是问题(或至少是主要问题),因为使用这种配置它根本不起作用。但我找到了解决方案;)
  • 很高兴您找到了解决方案。您能否将其发布为答案并接受它,以便其他人从您的经历中学习?谢谢!
【解决方案2】:

问题出在 ActivateUser 类的基类 (BaseResult) --> BaseResult

似乎不可能扩展 DataContract 类并期望它工作。

现在我使用的是接口而不是基类

public interface IResult {
    string Status{get;set;}
}


[DataContract]
public class ActivateUserResult : IResult {

    [DataMember]
    public string CryptedPassword { get; set; }

    [DataMember]
    public string Status { get; set; }
}

这是有效的......这就是我所知道的;)

【讨论】:

    猜你喜欢
    • 2018-07-21
    • 2011-01-13
    • 2010-09-07
    • 2016-09-20
    • 2012-04-21
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多