14

我有一个带有以下 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

才不是。

4

2 回答 2

5

所以这在我最近更新之前可能并不明显,但是我有两个 RESTful 服务,它们相互通信,但位于不同的域中。Web-Layer 服务是第一个联系点,App-Layer 服务是实际的工作人员。在这种情况下,我能够进一步调试,发现实际异常是从 Web 到 App 层的调用出现 405(不允许的方法)。经过大量挖掘后,我找到了这个链接,解决了我的问题。

当使用ClientBase<>as you 方法在服务之间进行通信时,您基本上需要在调用之间重新建立操作上下文。否则一切都会变成 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;
    }
}
于 2015-05-26T21:07:35.330 回答
4

.NET WCF Web 服务默认设置为发送文本编码的 SOAP 消息。这意味着 HTTP 方法是 POST 并且有必要的标头告诉服务调用什么方法。

我使用您的服务端点创建了一个快速示例,这是从提琴手生成的与该端点对话的请求。

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 中的某些内容设置不正确。发布数据或标题。

于 2015-05-21T19:49:28.153 回答