WCF代理使用Post即使指定了WebGet属性(仅在从另一个WCF服务调用时) - 导致405错误

时间:2010-07-20 22:03:41

标签: rest wcf wcf-client

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

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

代理接口'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);
    }
}

实施'ProductService':

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部分:

<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'方法不允许'错误。当我们查看远程服务器上的IIS日志时,我们看到当从页面启动方法调用时,ProductService代理正在发出HTTP GET请求,但是当从WCF服务调用该方法时,它正在发出HTTP POST请求。未在服务上配置POST方法,因此出现405错误。

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

从底线开始,WCF客户端代理在从另一个WCF Rest服务中使用时尝试执行POST而不是GET,但在从页面或其他任何地方使用时都能正常工作。

请帮忙!

相关问题