[英]WebGet with No Parameters or UriTemplate Fails
我有一個RESTful WCF Web服務,其中包含以下API:
[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
屬性。 這也行不通。
所以我的問題是:即使我不接受參數或使用自定義UriTemplate,為什么它不能作為WebGet
工作?
我試圖點擊它(它在IIS本地托管)的Url是:
http://localhost/Utilities/API/GetFileInfo
更新鑒於下面的評論和給出的答案,我仍然面臨這個問題。 一些額外的細節。
我的web層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>
我的app-layer 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();
}
}
我的app層實現
[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)));
}
我的應用層Global.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
才不是。
所以在我最近更新之前這可能不是很明顯,但我有兩個RESTful服務,它們彼此通信但是生活在不同的域中。 Web層服務是第一個聯系點,App層服務是實際的工作實踐者。 在這種情況下,我能夠進一步調試,發現從Web到App層的調用實際異常是405(Method Not Allowed)。 我經過多次挖掘后發現這個鏈接解決了我的問題。
當使用ClientBase<>
作為服務之間的通信方法時,您基本上需要重新建立調用之間的操作上下文。 否則一切都變成了POST,因此只有POST工作。
我希望這有助於其他人,我非常感謝大家幫忙調試這個。
為了演示這看起來是什么,以下是我更新的,有效的Web服務實現:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyApiWebService : ClientBase<IMyApi>, IMyApi
{
public MyResponseContract GetFileInfo()
{
MyResponseContract output = null;
using(var context = new OperationContext(Channel as IContextChannel))
{
output = Channel.GetFileInfo();
}
return output;
}
}
默認情況下,.NET WCF Web服務設置為發送文本編碼的SOAP消息。 這意味着HTTP方法是POST,並且需要標頭來告訴服務要調用哪種方法。
我使用您的服務端點創建了一個快速示例,這里是fiddler生成的與該端點通信的請求。
POST http://localhost/Utilities/API/GetFileInfo/Service1.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/IService1/GetFileInfo"
Host: localhost:8888
Content-Length: 136
Expect: 100-continue
Connection: Keep-Alive
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetFileInfo xmlns="http://tempuri.org/"/></s:Body></s:Envelope>
回復一下
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 398
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcV29ya1xFSVAgV29ya1xRdWVzdGlvbnNcV0NGR2V0VGVzdFxTZXJ2aWNlMS5zdmM=?=
X-Powered-By: ASP.NET
Date: Thu, 21 May 2015 19:47:49 GMT
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetFileInfoResponse xmlns="http://tempuri.org/"><GetFileInfoResult xmlns:a="http://schemas.datacontract.org/2004/07/WCFGetTest" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:BoolValue>true</a:BoolValue><a:StringValue>MyResponseContract </a:StringValue></GetFileInfoResult></GetFileInfoResponse></s:Body></s:Envelope>
對於你,我認為你的SoapUI中的某些東西設置不正確。 郵寄數據或標題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.