我有一个带有以下 API 的 RESTful WCF Web 服务:
[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 ,为什么这不起作用?
我尝试使用的 URL(它在 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)));
}
我的应用层 Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(MyApiAppService)));
}
我不确定我能提供多少细节。如您所见,鉴于提供的解决方案,我已经实施了所有建议但无济于事。无论我是尝试WebGet
通过将 Web 层服务 url 放置在浏览器中,还是使用 SOAPUI 来实现此方法,还是尝试使用服务客户端通过 C# 单元测试来实现它,我都无法使用WebGet
. 再次感谢您的所有帮助。
有趣的是,应用层 URL 有效。但是 web 层没有。所以:
localhost/Utilities.AppService/API/GetFileInfo
有效,而
localhost/Utilities.WebService/API/GetFileInfo
才不是。