簡體   English   中英

使用WCF WebGetAttribute的UriTemplate屬性的奇怪行為

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM