2016-04-06 2 views
2

J'écris un intergiciel personnalisé à utiliser dans une application ASP.NET. Mon middleware dépend de certains services que je peux injecter dans le conteneur Microsoft DI en utilisant la méthode AddServices.Comment tester un middleware personnalisé dans DNX qui nécessitait DI?

Cependant, lors de l'utilisation de xUnit et de la création d'un TestServer, je n'ai aucun endroit où appeler le conteneur Microsoft DI pour injecter mes services dont dépend mon middleware.

Voir l'exemple ci-dessous pour plus d'informations sur la façon dont je crée un TestServer et ajouter mon Middleware sur elle:

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

Où et comment injecter mes services dans le conteneur Microsoft DI?

Répondre

0

Il semble que cela peut être fait relativement facilement:

/// <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(); 
    }); 
}