2016-04-06 9 views
2

私は、ASP.NETアプリケーションで使用するカスタムミドルウェアを作成しています。 私のミドルウェアは、AddServicesメソッドを使用してMicrosoft DIコンテナに挿入できるサービスによって異なります。DIが必要なカスタムミドルウェアをDNXでテストする方法は?

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

どこで、どのようにマイクロソフトDIコンテナの中に私のサービスを注入する必要がありますか?

答えて

0

は、それが比較的容易に行うことができるようだ:

/// <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(); 
    }); 
} 
関連する問題