我有以下API一個RESTful WCF的Web服務:WebGet不帶參數或UriTemplate失敗
[WebGet(ResponseFormat = WebMessageFormat.Json)]
MyResponseContract GetFileInfo();
當試圖打端點(使用SOAPUI)我看到了以下錯誤消息:
服務器在處理請求時遇到錯誤。請參閱 服務幫助頁面,以便爲服務構建有效的請求。
我有SOAPUI設置爲使用GET方法調用它。當我將它切換到沒有正文的POST時,它會失敗並顯示以下消息:
方法不允許。
這是非常有意義的:無法通過POST命中GET。所以我更新了我的代碼,如下所示:
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
MyResponseContract GetFileInfo();
現在我使用POST方法從SOAPUI調用它,並且它可以工作。好奇。所以我現在改變我的代碼如下:
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET")]
MyResponseContract GetFileInfo();
我見過幾個職位,這基本上等同於一個WebGet
屬性。這也是行不通的。
所以我的問題是:爲什麼不作爲WebGet
工作,即使我不接受參數或使用自定義UriTemplate?
我試圖用打它(它在IIS本地託管)的網址是:
http://localhost/Utilities/API/GetFileInfo
更新 給出下面的意見和給定的答案,我仍然面臨着這個問題。一些額外的細節。
我的網絡層的web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="10000000" />
</webHttpEndpoint>
</standardEndpoints>
<behaviors>
<endpointBehaviors>
<behavior name="exampleBehavior">
<callbackDebug includeExceptionDetailInFaults="true" />
<enableWebScript />
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding" maxReceivedMessageSize="10000000" />
</webHttpBinding>
</bindings>
<client>
<endpoint address="http://LOCALHOST/Utilities.AppService/API"
binding="webHttpBinding" bindingConfiguration="WebHttpBinding"
contract="Utilities.Common.API.IMyApi"
behaviorConfiguration="exampleBehavior" />
</client>
</system.serviceModel>
</configuration>
我的應用程序層的web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="10000000" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</configuration>
我的服務接口
[ServiceContract(Namespace = "API")]
public interface IMyApi
{
[WebGet]
MyResponseContract GetFileInfo();
}
我的網絡層實現
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyApiWebService : ClientBase<IMyApi>, IMyApi
{
public MyResponseContract GetFileInfo()
{
return Channel.GetFileInfo();
}
}
我的應用層實現
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyApiAppService : IMyApi
{
public MyResponseContract GetFileInfo()
{
return new MyResponseContract();
}
}
我的網絡層的Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(MyApiWebService)));
}
我的應用程序層的全局。asax:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(MyApiAppService)));
}
我不確定我可以提供多少更多的細節。正如你所看到的,鑑於提供的解決方案,我已經實施了一切建議無濟於事。無論我試圖通過在瀏覽器中放置Web圖層服務url還是使用SOAPUI來嘗試實現WebGet
方法,或者嘗試使用服務客戶端通過C#單元測試進行擊打,我都無法使用WebGet
。再次感謝您的幫助。
有趣的是,應用層URL的工作原理。但是網頁層沒有。所以:
localhost/Utilities.AppService/API/GetFileInfo
作品,而
localhost/Utilities.WebService/API/GetFileInfo
沒有。
你是如何訪問它 - 就像一個URI樣?而要清楚的是最後一個也沒有工作? –
更新了要求的詳細信息。 –
你使用的是WebHttpBinding嗎? –