[英]Strange behavior with WCF WebGetAttribute's UriTemplate property
我已经为这个敲了敲键盘。
我有一个WCF客户端项目 ,可以用来打Yahoo GeoPlanet服务。 他们的自由文本查询端点之一可以这样表示:
[OperationContract(Name = "places")]
[WebGet(
UriTemplate = "places.q({query});count=0?format=json&view={view}&appid={appId}",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare
)]
PlacesResponse Places(string query, string appId, RequestView view);
有一个这样的实现(或多或少)调用WCF客户端:(这里有重试逻辑,但这可以归结为)
public Places Places(string query, string appId, RequestView view = RequestView.Long)
{
return Channel.Places(HttpUtility.UrlEncode(query), appId, view);
}
问题是,即使query
参数是url编码的,到WCF发出实际的HTTP请求时, %2F
值也将转换回正斜杠( /
)。 结果,雅虎服务器拒绝了"Saint Augustine Tunapuna/Piarco, Trinidad and Tobago"
类的搜索"Saint Augustine Tunapuna/Piarco, Trinidad and Tobago"
并被400错误请求拒绝。
最糟糕的是,这似乎仅在将客户端库用作非可视Studio项目参考时才会发生。 我在项目中进行了以下测试,该测试始终通过:
[TestMethod]
public void Yahoo_GeoPlanet_GeoPlanetClient_Places_ShouldUrlEncodeQuery_WhenItContainsUnsafeCharacters()
{
using (var geoPlanetClient = new GeoPlanetClient())
{
var places = geoPlanetClient.Places("Saint Augustine Tunapuna/Piarco, Trinidad and Tobago", AppId);
places.ShouldNotBeNull();
places.Items.Count.ShouldBeInRange(1, int.MaxValue);
}
}
当我在另一个项目中使用此库时,它仅在客户端.csproj是另一个项目的解决方案的一部分时才起作用,并且被引用为项目参考。 一旦将其包含为NuGet软件包或直接dll文件引用,它就会失败。
我已经深入到代码中,似乎URL传递到Channel
时已正确编码。 但是,此后的某个时间,搜索字符串中的%2F
将转换回正斜杠。 我知道的唯一原因是从检查提琴手中的请求。
也许考虑使用HttpClient
? 这要简单得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.