wcf - WCF UriTemplate 匹配基地址
问题描述
如何调用 URL 地址与基地址完全相同的端点?
string localhost = "http://localhost:1387";
ServiceHost restHost = new ServiceHost(typeof(WebService), new Uri(localhost));
restHost.AddServiceEndpoint(typeof(IWebService), new WebHttpBinding(), "").Behaviors.Add(new RestBehavior());
hosts.Add(restHost);
这是服务,我想用http://localhost:1387调用它
[WebInvoke(Method = "GET", UriTemplate = "", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public Stream GetBase()
{
//do action
}
解决方案
在WCF中,如果不设置UriTemplate,WCF会在基地址后面加上方法名作为服务调用的URI。这是我的服务的接口:
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
Result GetUserData(string name);
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle =WebMessageBodyStyle.Wrapped)]
Result PostUserData(UserData user);
[OperationContract]
[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Result PutUserData(UserData user);
[OperationContract]
[WebInvoke(Method = "DELETE", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Result DeleteUserData(UserData user);
}
这是服务启动后的帮助文档,可以看到即使我不设置UriTemplate,WCF仍然使用方法名作为UriTemplate。所以基地址不能和调用服务的地址相同。
推荐阅读
- php - 我无法通过 curl 登录网站
- python - 如何在 Windows 中使用 cmd 安装 SQLAlchemy
- javascript - 我无法使用 react 和 javascript 将订购商品的数量保存在本地存储中
- python - 欧拉角引起的机器人定向问题
- docker - 如何在 Dockerfile 中将文件夹的权限更改为 777?
- python - 用for循环转换多列的数据类型
- angular - 如何使用 Angular 和 firebase 使帐号独一无二
- robots.txt - robots.txt 允许和禁止少数页面,对其他页面意味着什么?
- android - 如何对用户隐藏活动?
- java - Java模拟键盘INT输入(机器人)