2013-05-06 90 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

相關問題