简体   繁体   English

HttpServer上的第二个请求返回503服务不可用

[英]Second request on HttpServer returns 503 Service Unavailable

I have the following setup: 我有以下设置:

1) A base class for API testing 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) An extension of the class for each controller I want to test: 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
    }
}

So I guess the http server responds only to the first query. 因此,我想http服务器仅响应第一个查询。 Why is that, how to fix it, and how could have I known (don't see it anywhere in the documentation) 为什么会这样,如何解决它,以及我怎么知道(在文档的任何地方都看不到)

EDIT 编辑

Turns out that the problem comes from instantiating the client HttpMessageInvoker twice. 原来,问题出在两次实例化客户端HttpMessageInvoker If I refactor the BasiApiTest class like the code snippet below, it works fine. 如果我像下面的代码片段一样重构BasiApiTest类,它可以正常工作。 However, I am still interested in my questions. 但是,我仍然对我的问题感兴趣。

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

It turns out that the HttpMessageInvoker class has 2 constructors: 事实证明HttpMessageInvoker类具有2个构造函数:

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

The first one is chained to the second one with true value, so in my case calling new HttpMessageInvoker(server) is equivalent to new HttpMessageInvoker(server, true) and according to specification the boolean parameter is a value that indicates whether this instance is responsible for disposing the handler. 第一个链接到具有true值的第二个链接,因此在我的情况下,调用new HttpMessageInvoker(server)等同于new HttpMessageInvoker(server, true)并且根据规范,布尔参数是一个值,值指示此实例是否负责用于配置处理程序。

What seems unnatural to me is why 在我看来不自然的是为什么

  1. the default behavior is to dispose the dependency provided. 默认行为是处理提供的依赖项。
  2. the default behavior is NOT documented. 默认行为未记录。

However, the first question is a matter of personal opinion, while the second can still be answered for completeness of this topic. 但是,第一个问题是个人观点,而第二个问题仍可以回答,以确保本主题的完整性。

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

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