Asp.net 如何在需要DI的DNX中测试定制中间件?

Asp.net 如何在需要DI的DNX中测试定制中间件?,asp.net,dependency-injection,dnx,Asp.net,Dependency Injection,Dnx,我正在编写一些用于ASP.NET应用程序的自定义中间件。 我的中间件依赖于一些服务,我可以使用方法AddServices将这些服务注入到Microsoft DI容器中 但是,当使用xUnit并创建TestServer时,我没有地方调用Microsoft DI容器来注入中间件所依赖的服务 有关如何创建TestServer并在其上添加中间件的信息,请参见下面的示例: /// <summary> /// Create a server with the ASP.NET Core L

我正在编写一些用于ASP.NET应用程序的自定义中间件。 我的中间件依赖于一些服务,我可以使用方法
AddServices
将这些服务注入到Microsoft DI容器中

但是,当使用xUnit并创建
TestServer
时,我没有地方调用Microsoft DI容器来注入中间件所依赖的服务

有关如何创建TestServer并在其上添加中间件的信息,请参见下面的示例:

/// <summary>
///     Create a server with the ASP.NET Core Logging Middleware registered without any configuration.
///     The server will throw an exception of type <typeparamref name="T"/> on every request.
/// </summary>
/// <typeparam name="T">The type of exception to throw.</typeparam>
/// <returns>A <see cref="TestServer"/> that can be used to unit test the middleware.</returns>
private TestServer CreateServerWithAspNetCoreLogging<T>()
    where T : Exception, new()
{
    return TestServer.Create(app =>
    {
        app.UseAspNetCoreLogging();

        SetupTestServerToThrowOnEveryRequest<T>(app);
    });
}
//
///使用注册的ASP.NET核心日志记录中间件创建服务器,而不进行任何配置。
///服务器将在每个请求上抛出类型为的异常。
/// 
///要引发的异常的类型。
///可用于对中间件进行单元测试的。
专用测试服务器CreateServerWithAspNetCoreLogging()
其中T:Exception,new()
{
返回TestServer.Create(应用=>
{
app.useApsNetCoreLogging();
SetupTestServerToThrowOnEveryRequest(应用程序);
});
}

应该在何处以及如何将我的服务注入Microsoft DI容器?

似乎可以相对轻松地完成:

/// <summary>
///     Create a server with the ASP.NET Core Logging Middleware registered without any configuration.
///     The server will throw an exception of type <typeparamref name="T"/> on every request.
/// </summary>
/// <typeparam name="T">The type of exception to throw.</typeparam>
/// <returns>A <see cref="TestServer"/> that can be used to unit test the middleware.</returns>
private TestServer CreateServerWithAspNetCoreLogging<T>()
    where T : Exception, new()
{
    return TestServer.Create(null, app =>
    {
        app.UseAspNetCoreLogging<string>();

        SetupTestServerToThrowOnEveryRequest<T>(app);
    }, services =>
    {
        services.AddAspNetCoreLogging();
    });
}
//
///使用注册的ASP.NET核心日志记录中间件创建服务器,而不进行任何配置。
///服务器将在每个请求上抛出类型为的异常。
/// 
///要引发的异常的类型。
///可用于对中间件进行单元测试的。
专用测试服务器CreateServerWithAspNetCoreLogging()
其中T:Exception,new()
{
返回TestServer.Create(null,app=>
{
app.useApsNetCoreLogging();
SetupTestServerToThrowOnEveryRequest(应用程序);
},服务=>
{
services.AddAspNetCoreLogging();
});
}