2017-06-18 55 views
0

我具有以下設置:第二請求返回503服務不可用

1)API測試

public class BaseApiTest 
{ 
    private static readonly Uri BaseUri = new Uri("http://localhost"); 
    private static HttpServer _server; 

    internal BaseApiTest() 
    { 
     var config = new HttpConfiguration 
     { 
      IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always 
     }; 

     WebApiConfig.Register(config); 
     _server = new HttpServer(config); 
    } 

    ~BaseApiTest() 
    { 
     _server.Dispose(); 
    } 

    protected static HttpResponseMessage GetHttpResponseMessageFrom(HttpMethod method, string relativeUri) 
    { 
     using (var client = new HttpMessageInvoker(_server)) 
     { 
      var absoluteUri = new Uri(BaseUri, relativeUri); 

      var message = new HttpRequestMessage(method, absoluteUri); 

      var response = client.SendAsync(message, CancellationToken.None); 

      return response.Result; 
     } 
    } 

2)類的擴展爲每個控制器我想測試的基類:

[TestClass] 
public class ChemicalApiTest : BaseApiTest 
{ 
    private const string ControllerRoutePrefix = "/api/customers/316"; 

    [TestMethod] 
    public void SomeTest() { 

     // Act 
     var response = 
      GetHttpResponseMessageFrom(HttpMethod.Get, ControllerRoutePrefix + "/suppliers"); 
     Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); // this is OK 

     response = 
      GetHttpResponseMessageFrom(HttpMethod.Get, ControllerRoutePrefix + "/suppliers"); 
     Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); // This fails with 503 
    } 
} 

所以我想http服務器只對第一個查詢響應。這是爲什麼,如何解決它,怎麼可能有我知道(沒有看到它在文檔中的任意位置)

編輯

原來,問題就來了,從實例化客戶端HttpMessageInvoker兩次。如果我像下面的代碼片段那樣重構BasiApiTest類,它可以正常工作。不過,我仍然對我的問題感興趣。

public class BaseApiTest 
{ 
    private static readonly Uri BaseUri = new Uri("http://localhost"); 
    private static HttpMessageInvoker _client; 

    internal BaseApiTest() 
    { 
     var config = new HttpConfiguration 
     { 
      IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always 
     }; 

     WebApiConfig.Register(config); 
     var server = new HttpServer(config); 
     _client = new HttpMessageInvoker(server); 
    } 

    ~BaseApiTest() 
    { 
     _client.Dispose(); 
    } 

    protected static HttpResponseMessage GetHttpResponseMessageFrom(HttpMethod method, string relativeUri) 
    { 
     var absoluteUri = new Uri(BaseUri, relativeUri); 

     var message = new HttpRequestMessage(method, absoluteUri); 

     var response = _client.SendAsync(message, CancellationToken.None); 

     return response.Result; 
    } 
} 
+0

你能告訴我們「GetHttpResponseMessageFrom'和'BaseApiTest'的源代碼嗎? – mjwills

+1

@mjwills都在我的文章。第一個代碼段具有「BaseApiTest」類,其方法爲「GetHttpResponseMessageFrom」 – LIvanov

回答

1

事實證明,所述HttpMessageInvoker類有2層構造:

HttpMessageInvoker(HttpMessageHandler); 
HttpMessageInvoker(HttpMessageHandler, Boolean); 

第一個被鏈接到所述第二個與true值,所以在我的情況下調用new HttpMessageInvoker(server)相當於new HttpMessageInvoker(server, true)並根據以指定布爾參數是一個值,該值指示此實例是否負責處理處理程序。

什麼好像不自然,對我來說是爲什麼

  1. 的默認行爲是處置提供的依賴。
  2. 默認行爲沒有記錄。

但是,第一個問題是個人意見的問題,而第二個問題仍然可以回答這個問題的完整性。

相關問題