2013-05-06 50 views
0

我想创建一个WCF Restful Service。WCF REST调用返回“方法不允许”异常

这是我的合同:(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; } 
} 

}

这里是服务的实现:

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 

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

对此有何看法?

- >目前本地使用

- > IIS快递(的Visual Studio 2012)

+0

这个问题的答案可能提供一些见解。 http://stackoverflow.com/a/545754/1181408 – cgotberg 2013-05-06 13:45:55

+0

你是如何调用http操作的?即显示javascript/jquery或WebClient /等... – 2013-05-06 13:51:00

+1

获取此错误的一种方法是使用错误的动词进行调用。例如你已经为GET定义了它,如果你使用POST来调用那个URL,那么WebAPI框架就会反弹Method Not Allowed错误。 – 2013-05-06 13:52:21

回答

0

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

这是工作....这就是我所知道的;)

3

您使用SvcUtil工具来创建一个 “REST风格的WCF服务” 的代理。 This does not work。原因很简单,Web端点不公开用于svcutil工具的元数据,以了解它需要发送给它的请求。长版本位于链接的博客文章中。

+0

感谢您的回复。这是真的,你在说什么,但这不是问题(或者至少是主要问题),因为使用这种配置根本不起作用。但我找到了解决方案;) – RAN 2013-05-06 21:24:34

+0

很高兴你找到了解决方案。你可以将它作为答案发布并接受它,这样其他人将从你经历的经历中学习吗?谢谢! – carlosfigueira 2013-05-06 21:28:53

相关问题