【发布时间】:2009-04-29 14:07:09
【问题描述】:
我有一个使用基本 http 绑定的 WCF 服务。 如何通过浏览器调用其操作/方法?
【问题讨论】:
标签: wcf web-services
我有一个使用基本 http 绑定的 WCF 服务。 如何通过浏览器调用其操作/方法?
【问题讨论】:
标签: wcf web-services
您需要像以下示例一样将 WebGetAttribute 添加到您的方法中
[OperationContract]
[WebGet(UriTemplate = "/placesList/{userId}",
ResponseFormat = WebMessageFormat.Xml)]
List<Places> GetAllPlacesForUser(String userId)
{
string xml = "";
// build xml here
return xml;
}
现在在浏览器中,你可以像这样调用方法
http://localhost:8085/GeoPlacesDataService/placesList/10
其中 10 是 userId 参数。
注意:为了添加 WebGetAttribute,您必须引用 System.ServiceModel.Web 命名空间,该命名空间位于单独的程序集中
【讨论】:
我建议为服务设置多个端点。使用 webHttpBinding 添加端点以获取服务的 XML 版本。如果正确执行此操作,您将从服务获得的响应与 basicHttpBinding 端点相同,但没有 SOAP 开销。
除此之外,您不能直接从浏览器调用 SOAP Web 服务,因为它需要表单发布。不过,您可以使用一个工具使用 SOAP 对其进行测试,我推荐Soap UI。它是用 Java 编写的,但我尽量不反对它。 :)
【讨论】:
添加上述代码后,需要在web.config、binding="webHttpBinding"和behaviorConfiguration="webHttp"中修改endpoint属性。
【讨论】: