我有一个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" /> …Run Code Online (Sandbox Code Playgroud)