我有一个restfulwcf服务,它位于另一个服务器上,配置了WebGet属性来响应httpget方法。我知道该服务工作正常,因为我可以通过浏览器直接调用该服务,并手动使用Fiddler执行Get操作,并收到正确的响应。
我有一个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);
}
}
}
<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“Method not allowed”错误。当我们查看远程服务器上的IIS日志时,我们看到ProductService代理在从页面启动方法调用时发出httpget请求,但在从WCF服务调用方法时发出httppost请求。POST方法没有在服务上配置,因此405错误。
即使页面和服务位于同一文件夹和命名空间中,我们仍然从服务接收到相同的错误。如果我们使用经典的asmxsoap服务,则会发出GET调用,服务会正确执行和响应。如果我们使用System.Net.WebRequest请求对象,则服务调用成功。
总之,当从另一个WCF Rest服务中使用时,WCF客户机代理尝试执行POST而不是GET,但当从页面或几乎任何其他地方使用时,都可以正常工作。