WCF REST调用返回“方法不允许”;例外

本文关键字:方法不允许 例外 不允许 方法 REST 调用 返回 WCF | 更新日期: 2023-09-27 18:12:14

我正在尝试创建一个WCF Restful Service。

这是我的契约:(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"
        };
    }

还有网络。配置文件:

    <?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

代码正在执行,但我仍然得到一个方法不允许(405)异常。

有什么想法吗?

->当前本地使用

-> IIS Express (Visual Studio 2012)

WCF REST调用返回“方法不允许”;例外

您正在使用svcutil为"RESTful WCF服务"创建代理。这行不通。最简单的原因是,Web端点没有为svcutil工具公开元数据,使其知道需要向它发送哪些请求。

问题是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; }
}

这是工作....