首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有参数的WebGet或UriTemplate失败

没有参数的WebGet或UriTemplate失败
EN

Stack Overflow用户
提问于 2015-05-19 16:30:46
回答 2查看 1.6K关注 0票数 14

我有一个RESTful WCF服务,其API如下:

代码语言:javascript
复制
[WebGet(ResponseFormat = WebMessageFormat.Json)]
MyResponseContract GetFileInfo();

当试图到达端点(使用SOAPUI)时,我看到以下错误消息:

服务器在处理请求时遇到错误。请参阅“服务帮助”页面,以构造对该服务的有效请求。

我已经设置了SOAPUI,可以通过GET方法调用来访问它。当我将它切换到一个没有正文的帖子时,它会失败,并收到以下消息:

方法不允许。

这是完全有道理的:不能用一条柱子击中一击。因此,我将代码更新如下:

代码语言:javascript
复制
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
MyResponseContract GetFileInfo();

现在我用POST方法从SOAPUI调用它,它可以工作。好奇心。因此,我现在将代码更改如下:

代码语言:javascript
复制
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET")]
MyResponseContract GetFileInfo();

我在一些帖子中看到,这在本质上等同于一个WebGet属性。这也不起作用。

因此,我的问题是:即使我不接受参数或使用自定义的WebGet,为什么这不能作为一个UriTemplate工作呢?

我试图使用的Url (它是在IIS中本地托管的)是:

http://localhost/Utilities/API/GetFileInfo

更新给出了下面的评论和答案,我仍然面临着这个问题。一些额外的细节。

我的网络层web.config

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>

我的服务接口

代码语言:javascript
复制
[ServiceContract(Namespace = "API")]
public interface IMyApi
{    
    [WebGet]
    MyResponseContract GetFileInfo();
}

我的web层实现

代码语言:javascript
复制
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyApiWebService : ClientBase<IMyApi>, IMyApi
{
    public MyResponseContract GetFileInfo()
    {
        return Channel.GetFileInfo();
    }
}

我的应用程序层实现

代码语言:javascript
复制
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyApiAppService : IMyApi
{
    public MyResponseContract GetFileInfo()
    {
        return new MyResponseContract();
    }
}

我的网络层Global.asax:

代码语言:javascript
复制
    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(MyApiWebService)));
    }

我的应用程序层Global.asax:

代码语言:javascript
复制
    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(MyApiAppService)));
    }

我不知道我还能提供多少细节。正如你所看到的,考虑到所提供的解决方案,我已经执行了所有建议,但都没有效果。无论是通过在浏览器中放置web层服务url,还是使用SOAPUI,还是试图使用服务客户端使用WebGet单元测试来命中此WebGet方法,我都无法使用WebGet。再次感谢你的帮助。

有趣的是,应用程序层URL可以工作。但是网络层没有。所以:

localhost/Utilities.AppService/API/GetFileInfo

工作,而

localhost/Utilities.WebService/API/GetFileInfo

不会的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-26 21:07:35

因此,在我最近更新之前,这一点可能并不明显,但我有两个RESTful服务,它们彼此通信,但生活在不同的域中。Web层服务是第一个接触点,应用程序层服务是实际的工作执行者。在这种情况下,我能够进一步调试,并发现实际的异常是从Web到App层的调用中的405 (方法不允许)。我找到了此链接,在深入挖掘之后解决了我的问题。

当使用ClientBase<>作为服务之间通信的方法时,基本上需要在调用之间重新建立操作上下文。否则,一切都变成一个职位,因此,只有张贴工作。

我希望这对其他人有所帮助,我非常感谢大家在调试这方面的帮助。

为了演示这是什么样子,下面是我更新的、正在工作的web服务实现的样子:

代码语言:javascript
复制
[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;
    }
}
票数 5
EN

Stack Overflow用户

发布于 2015-05-21 19:49:28

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

我使用您的服务端点创建了一个快速示例,下面是fiddler生成的与该端点对话的请求。

代码语言:javascript
复制
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>

收回对…的回应

代码语言:javascript
复制
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中的某些内容没有正确设置。post数据或标头。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30331246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档