2017-12-06 6 views
0

私はASP.NET Core 2.0でルーティングを試しています。 I(単純なユニットテスト用)System.DateTime.UtcNow機能をラップクラスASP.Net Core RouteBuilderとDependency Injection

public interface IDateTimeWrapper 
{ 
    DateTime UtcNow(); 
} 

public sealed class DateTimeWrapper : IDateTimeWrapper 
{ 
    public DateTime UtcNow() 
    { 
     return DateTime.UtcNow; 
    } 
} 

を持っているこれはHttpResponse

public class WhatIsTheTimeHandler 
{ 
    private readonly IDateTimeWrapper _dateTimeWrapper; 

    public WhatIsTheTimeHandler(
     IDateTimeWrapper dateTimeWrapper) 
    { 
     this._dateTimeWrapper = dateTimeWrapper; 
    } 

    public async Task Execute(Microsoft.AspNetCore.Http.HttpContext httpContext) 
    { 
     httpContext.Response.StatusCode = 200;    
     await httpContext.Response.WriteAsync(this._dateTimeWrapper.UtcNow().ToString()); 
    } 
} 
DateTimeWrapperの結果を書き込み、単一方法 Executeを持つクラスによって使用されています

の中で へのリクエストは、WhatIsTheTimeHandler(!!!コメントを参照)で処理されます。

public sealed class Startup 
{ 
    public Startup(IHostingEnvironment env) 
    { 
     var builder = new ConfigurationBuilder(); 

     builder.SetBasePath(env.ContentRootPath); 
     builder.AddJsonFile("appsettings.json", false, true); 
     // we must lowercase the json file path due to linux file name case sensitivity 
     builder.AddJsonFile($"appsettings.{env.EnvironmentName.ToLower()}.json", false, true); 
     builder.AddEnvironmentVariables(); 

     this.Configuration = builder.Build(); 
    } 

    public IConfigurationRoot Configuration { get; set; } 

    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddRouting(); 

     services.AddScoped<IDateTimeWrapper, DateTimeWrapper>(); 
    } 

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(); 

     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
     } 

     var routeBuilder = new Microsoft.AspNetCore.Routing.RouteBuilder(app); 

     // !!! 
     // I want to do something like 
     // routeBuilder.MapGet("whatisthetime", WhatIsTheTimeHandler.Execute); 

     var routes = routeBuilder.Build(); 
     app.UseRouter(routes); 
    } 
} 

WhatIsTheTimeHandler.Executeがインスタンスメソッドであるため、上記の操作はできません。私。エラー:

An object reference is required for the non-static field, method or property 'WhatIsTheTimeHandler.Execute(HttpContext)' Cannot access non-static method in static context).

私はそれstatic作るなら、私は_dateTimeWrapperインスタンスメンバを使用することはできません。

アイデア?

+0

httpsのミドルウェアを作成します://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware?tabs = aspnetcore2xまた、DIを許可します – Nkosi

+1

'WhatIsTheTimeHandler'がなぜ' Controller'ではないのだろうか? –

+0

@CamiloTerevintoは非常に有効なポイントを作っています。 – Nkosi

答えて

1

ASP.NETコアルーティングは、ルートをデリゲートにマップします。 ASP.NET MVCコアルーティングは、ルートをオブジェクト(一般にコントローラと呼ばれます)にマップします。だから後者は、おそらくあなたの特にニーズに適したソリューションです。しかし

、あなたがサービスとしてハンドラを追加する必要がありますあなたの現在のソリューションの作品を作るために:

services.AddScoped<WhatIsTheTimeHandler>(); 

その後、例えば、ルートの中からこれを呼び出す:

​​
関連する問題