即使指定了 WebGet 属性,WCF 代理也使用 Post(仅当从另一个 WCF 服务调用时) - 导致 405 错误 [英] WCF Proxy Using Post Even Though WebGet Attribute is Specified (Only when called from another WCF service) - Causes 405 Error

查看:36
本文介绍了即使指定了 WebGet 属性,WCF 代理也使用 Post(仅当从另一个 WCF 服务调用时) - 导致 405 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Restful WCF 服务,它位于另一台服务器上,配置了 WebGet 属性以响应 HTTP Get 方法.我知道该服务可以正常工作,因为我可以直接通过浏览器调用该服务并手动使用 Fiddler 执行 Get 并收到正确的响应.

I have a Restful WCF service sitting on another server configured with the WebGet attribute to respond to the HTTP Get method. I know the service works correctly because I can call the service directly through the browser and manually do a Get with Fiddler and receive a correct response.

我在本地机器上有一个 Asp.NET 项目,它使用以下代码调用此服务:

I have an Asp.NET project on my local machine that is calling this service with the following code:

代理接口IProductService":

Proxy Interface 'IProductService':

using System.ServiceModel;
using System.ServiceModel.Web;

namespace Hugo.Infrastructure.Services.Products
{
    [ServiceContract]
    [XmlSerializerFormat]
    public interface IProductService
    {
        [OperationContract(Name = "GetProductById")]
        [WebGet(UriTemplate = "Products/Titles/{id}",
            ResponseFormat = WebMessageFormat.Xml,
            RequestFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare)]
        TitleDto GetTitleById(string id);
    }
}

实现产品服务":

using System.ServiceModel;

namespace Hugo.Infrastructure.Services.Products
{
    public class ProductService : ClientBase<IProductService>, IProductService
    {
        public TitleDto GetTitleById(string id)
        {
            return Channel.GetTitleById(id);
        }
    }
}

相关的 Web.config 部分:

Related Web.config section:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
        <baseAddressPrefixFilters>
        </baseAddressPrefixFilters>      
    </serviceHostingEnvironment>
    ...
    <client>
        <endpoint address="http://server/directory/product.svc" bindingConfiguration="ProductServiceBinding" binding="webHttpBinding" behaviorConfiguration="productService" contract="Project.Infrastructure.Services.Products.IProductService" name="ProductServiceRest" />
    </client>
    <behaviors>
        ...
        <endpointBehaviors>
            <behavior name="productService">
                <webHttp />
            </behavior>
            ...
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

当我们从项目中的页面调用该方法时,这工作正常,但是当我们从 WCF 服务中调用它时,它会在这一行出现错误 return Channel.GetTitleById(id);同一个项目.我们收到的错误是 HTTP 405 'Method not allowed' 错误.当我们查看远程服务器上的 IIS 日志时,我们看到 ProductService 代理在从页面启动方法调用时发出 HTTP GET 请求,但在从 WCF 服务调用该方法时发出 HTTP POST 请求.服务上没有配置POST方法,因此405错误.

This works fine when we call the method from a page within the project, however it errors out on this line return Channel.GetTitleById(id); when we call it from within a WCF service from the same project. The error we receive is an HTTP 405 'Method not allowed' error. When we look at the IIS logs on the remote server we see that the ProductService proxy is making an HTTP GET request when the method call is initiated from the page but it is making an HTTP POST request when the method is called from the WCF service. The POST method is not configured on the service, thus the 405 error.

即使页面和服务位于相同的文件夹和命名空间中,我们仍然会收到来自服务的相同错误.如果我们改用经典的 asmx soap 服务,则会进行 GET 调用,该服务将正确执行和响应.如果我们使用 System.Net.WebRequest 对象从 WCF 服务手动获取,则服务调用成功.

Even when the page and the service are in the same folder and namespace we still receive the same error from the service. If we use a classic asmx soap service instead then a GET call is made and the service executes and responds correctly. If we manually do a get from the WCF service using the System.Net.WebRequest object, the service call succeeds.

最重要的是,当在另一个 WCF Rest 服务中使用时,WCF 客户端代理尝试执行 POST 而不是 GET,但在页面或几乎任何其他地方使用时都可以正常工作.

Bottom line, the WCF client proxy tries to do a POST instead of a GET when used from within another WCF Rest service but works correctly when used from a page or pretty much anywhere else.

请帮忙!

推荐答案

这可能有效:http://www.rgoarchitects.com/nblog/2008/09/28/AnotherWCFGotchaCallingAnotherServiceresourceWithinACall.aspx

这篇关于即使指定了 WebGet 属性,WCF 代理也使用 Post(仅当从另一个 WCF 服务调用时) - 导致 405 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆