没有参数或UriTemplate的WebGet失败

时间:2015-05-19 16:30:47

标签: c# wcf rest webget

我有一个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.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();
    }
}

我的应用层实施

[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,或者尝试使用服务客户端使用C#单元测试来命中它来尝试触及此WebGet方法,我都无法使用{{ 1}}。再次感谢您的帮助。

有趣的是,App层的URL工作正常。但是网络层没有。所以:

WebGet

有效,而

localhost/Utilities.AppService/API/GetFileInfo

没有。

2 个答案:

答案 0 :(得分:5)

所以在我最近更新之前这可能并不明显,但我有两个RESTful服务,它们彼此通信但是生活在不同的域中。 Web层服务是第一个联系点,App层服务是实际的工作实践者。在这种情况下,我能够进一步调试,发现从Web到App层的调用实际异常是405(Method Not Allowed)。经过多次挖掘后我发现this link解决了我的问题。

当使用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;
    }
}

答案 1 :(得分:4)

默认情况下,.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中的某些内容设置不正确。邮寄数据或标题。

相关问题