Bre*_*ett 4 rest wcf wcf-client
我有一个Restful WCF服务坐在另一台配置了WebGet属性的服务器上,以响应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);
}
}
Run Code Online (Sandbox Code Playgroud)
实施'ProductService':
using System.ServiceModel;
namespace Hugo.Infrastructure.Services.Products
{
public class ProductService : ClientBase<IProductService>, IProductService
{
public TitleDto GetTitleById(string id)
{
return Channel.GetTitleById(id);
}
}
}
Run Code Online (Sandbox Code Playgroud)
相关的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>
Run Code Online (Sandbox Code Playgroud)
当我们从项目中的页面调用方法时,这很好,但是return Channel.GetTitleById(id);当我们从同一个项目的WCF服务中调用它时,它在这一行上出错.我们收到的错误是HTTP 405'方法不允许'错误.当我们查看远程服务器上的IIS日志时,我们看到当从页面启动方法调用时,ProductService代理正在发出HTTP GET请求,但是当从WCF服务调用该方法时,它正在发出HTTP POST请求.未在服务上配置POST方法,因此出现405错误.
即使页面和服务位于同一文件夹和命名空间中,我们仍然会从服务中收到相同的错误.如果我们使用经典的asmx soap服务,那么就会进行GET调用并且服务执行并正确响应.如果我们使用System.Net.WebRequest对象手动从WCF服务获取get,则服务调用成功.
最后,当从另一个WCF Rest服务中使用时,WCF客户端代理尝试执行POST而不是GET,但是当从页面或几乎任何其他地方使用时,它都能正常工作.
请帮忙!
小智 6
这可能有效:http: //www.rgoarchitects.com/nblog/2008/09/28/AnotherWCFGotchaCallingAnotherServiceresourceWithinACall.aspx
| 归档时间: |
|
| 查看次数: |
2599 次 |
| 最近记录: |