Rest 即使指定了WebGet属性(仅当从另一个WCF服务调用时),使用Post的WCF代理也会导致405错误

Rest 即使指定了WebGet属性(仅当从另一个WCF服务调用时),使用Post的WCF代理也会导致405错误,rest,wcf,wcf-client,Rest,Wcf,Wcf Client,我有一个restfulwcf服务,它位于另一台服务器上,配置了WebGet属性以响应httpget方法。我知道该服务工作正常,因为我可以通过浏览器直接调用该服务,并手动使用Fiddler执行Get操作,然后收到正确的响应 我在本地计算机上有一个Asp.NET项目,该项目正在使用以下代码调用此服务: 代理接口“IPProductService”: using System.ServiceModel; using System.ServiceModel.Web; namespace Hugo.In

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

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

代理接口“IPProductService”:

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);
        }
    }
}
实现“ProductService”:

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);
        }
    }
}
使用System.ServiceModel;
命名空间Hugo.Infrastructure.Services.Products
{
公共类ProductService:ClientBase、IPProductService
{
GetTitleById(字符串id)的公共标题
{
返回通道GetTitleById(id);
}
}
}
相关Web.config部分:

<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>

...
...
...
当我们从项目中的一个页面调用该方法时,它工作得很好,但是它在这一行
return Channel.GetTitleById(id)上出错当我们从同一项目的WCF服务中调用它时。我们收到的错误是HTTP 405“不允许使用方法”错误。当我们查看远程服务器上的IIS日志时,我们看到ProductService代理在从页面启动方法调用时发出HTTP GET请求,但在从WCF服务调用方法时发出HTTP POST请求。未在服务上配置POST方法,因此出现405错误

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

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

救命啊

这可能有效:

这可能有效:

@Brett:Microsoft technologies大约需要10年才能稳定下来。不知道为什么,可能是程序员的素质或框架的复杂性。这是微软对迎合设计师驱动的拖放式开发人员的渴望,加上鼓励的不良做法,最终总是导致集群的臃肿,执行不力的技术,直到不可避免的重写——留下了多年的遗留技术债务。@Brett:Microsoft technologies需要10年左右的时间才能稳定下来。不知道为什么,可能是程序员的素质或框架的复杂性。这是微软对迎合设计师驱动的拖放式开发人员的渴望,加上鼓励的不良做法,最终总是导致集群的臃肿,执行不力的技术,直到不可避免的重写,留下了多年的遗留技术债务。