2017-02-17 57 views
0

我试图运行IdentityServer4作为控制台应用程序。但我不确定我是否正确。我创建控制台应用程序(.NET核心)代替ASP.NET核心的Web应用程序(.NET核心)IdentityServer 4作为控制台应用程序

我的WebHostBuilder设置是相当简单:

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     var host = new WebHostBuilder() 
      .UseKestrel() 
      .UseStartup<Startup>() 
      .Build(); 

     host.Run(); 
    } 
} 

另外,启动类是非常小的:

public class Startup 
{ 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddIdentityServer() 
       .AddTemporarySigningCredential() 
       .AddInMemoryIdentityResources(Config.GetIdentityResources()) 
       .AddInMemoryApiResources(Config.GetApiResources()) 
       .AddInMemoryClients(Config.GetClients()) 
       .AddTestUsers(Config.GetUsers()); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(); 
     app.UseIdentityServer(); 
    } 
} 

我只是担心,如果我错过了一些作品可能以后会导致问题。我是否正确?

回答

2

这里描述你可以发布一个独立的应用程序的创建使用.NET Core版本1.1Identity Server的V4可执行的步骤。

创建控制台应用程序(.NET核心)项目。在Main方法执行以下代码:

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     Console.Title = "IdentityServer"; 

     var host = new WebHostBuilder() 
      .UseKestrel() 
      .UseStartup<Startup>() 
      .Build(); 

     host.Run(); 
    } 
} 

Startup类实现下面的代码:

public class Startup 
{ 
    public void ConfigureServices(IServiceCollection services) 
    { 
     // configure identity server with in-memory stores, keys, clients and scopes 
     services.AddIdentityServer() 
      .AddTemporarySigningCredential() 
      .AddInMemoryApiResources(Config.GetApiResources()) 
      .AddInMemoryClients(Config.GetClients()) 
      .AddTestUsers(Config.GetUsers()); 
    } 

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

     app.UseIdentityServer(); 
    } 
} 

要生成可执行文件,您必须没有一个"type": "platform"project.json。比如我project.json看起来是这样的:

{ 
    "dependencies": { 
    "IdentityServer4": "1.1.1", 
    "Microsoft.AspNetCore.Diagnostics": "1.1.0", 
    "Microsoft.AspNetCore.Hosting": "1.1.0", 
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0", 
    "Microsoft.Extensions.Logging.Console": "1.1.0", 
    "Microsoft.NETCore.App": { 
     "version": "1.1.0" 
    } 
    }, 

    "frameworks": { 
    "netcoreapp1.1": { 
    } 
    }, 

    "runtimes": { 
    "win7-x64": {}, 
    "win8-x64": {}, 
    "win10-x64": {} 
    } 
} 

比你可以生成可执行文件DOTNET发布命令如下图所示。

dotnet publish -c release -r win7-x64
dotnet publish -c release -r win8-x64
dotnet publish -c release -r win10-x64

免责声明:这是最小的运行版本,也许你会需要扩展配置,取决于你所需要的。

0

每一个.NET的核心应用是一个控制台应用程序。因此,您使用的模板没有区别。

但是我觉得在你的主 - 你缺少UseContentRoot通话。

+0

是的,这是真的,我的观点是运行_IdentityServer_作为_ * .exe_。我可以_ASP.Net Core_模板做到这一点?哦,并感谢您使用'UseContentRoot'提示。这种设置的含义是什么? –

+0

当你定位完整的框架时,你会得到一个exe文件 - netcore将总是生成一个你可以用dotnet运行的dll。 – leastprivilege