简体   繁体   English

使用WCF WebGetAttribute的UriTemplate属性的奇怪行为

[英]Strange behavior with WCF WebGetAttribute's UriTemplate property

I'm done banging my head on the keyboard for this one. 我已经为这个敲了敲键盘。

I have a WCF client project that can be used to hit the Yahoo GeoPlanet service. 我有一个WCF客户端项目 ,可以用来打Yahoo GeoPlanet服务。 One of their endpoints for free-text querying can be represented like so: 他们的自由文本查询端点之一可以这样表示:

[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);

There is an implementation of this that invokes the WCF client (more or less) like so: (there is retry logic here, but this is what it boils down to) 有一个这样的实现(或多或少)调用WCF客户端:(这里有重试逻辑,但这可以归结为)

public Places Places(string query, string appId, RequestView view = RequestView.Long)
{
    return Channel.Places(HttpUtility.UrlEncode(query), appId, view);
}

The problem is that, even though the query parameter is url encoded, by the time WCF issues the actual HTTP request, values of %2F are converted back into a forward slash ( / ). 问题是,即使query参数是url编码的,到WCF发出实际的HTTP请求时, %2F值也将转换回正斜杠( / )。 As a result, searches like "Saint Augustine Tunapuna/Piarco, Trinidad and Tobago" are (understandably) being rejected with a 400 Bad Request by the Yahoo server. 结果,雅虎服务器拒绝了"Saint Augustine Tunapuna/Piarco, Trinidad and Tobago"类的搜索"Saint Augustine Tunapuna/Piarco, Trinidad and Tobago"并被400错误请求拒绝。

The worst part of it is that this only seems to happen when the client library is used as a non-visual studio project reference. 最糟糕的是,这似乎仅在将客户端库用作非可视Studio项目参考时才会发生。 I have the following test in the project, which always passes: 我在项目中进行了以下测试,该测试始终通过:

[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);
    }
}

When I use this library in another project, it works only when the client .csproj is part of the other project's solution, and is referenced as a project reference. 当我在另一个项目中使用此库时,它仅在客户端.csproj是另一个项目的解决方案的一部分时才起作用,并且被引用为项目参考。 As soon as I include it as a NuGet package or direct dll file reference, it fails. 一旦将其包含为NuGet软件包或直接dll文件引用,它就会失败。

I have stepped down into the code, and it seems like the URL is correctly encoded by the time it is passed to the Channel . 我已经深入到代码中,似乎URL传递到Channel时已正确编码。 However sometime after that, the %2F in the search string is getting converted back into a forward slash. 但是,此后的某个时间,搜索字符串中的%2F将转换回正斜杠。 The only reason I know this is from inspecting the request in fiddler. 我知道的唯一原因是从检查提琴手中的请求。

Perhaps consider using HttpClient ? 也许考虑使用HttpClient It's a LOT simpler. 这要简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM