c# - Using Startup class in ASP.NET5 Console Application -


is possible asp.net 5-beta4 console application (built asp.net console project template in vs2015) use startup class handle registering services , setting configuration details?

i've tried create typical startup class, never seems called when running console application via dnx . run or inside visual studio 2015.

startup.cs pretty much:

public class startup {   public startup(ihostingenvironment env)   {     configuration configuration = new configuration();     configuration.addjsonfile("config.json");     configuration.addjsonfile("config.{env.environmentname.tolower()}.json", optional: true);     configuration.addenvironmentvariables();      this.configuration = configuration;   }    public void configureservices(iservicecollection services)   {     services.configure<settings>(configuration.getsubkey("settings"));      services.addentityframework()             .addsqlserver()             .adddbcontext<applicationcontext>(options => options.usesqlserver(this.configuration["data:defaultconnection:connectionstring"]));   }    public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory)   {     loggerfactory.addconsole(minlevel: loglevel.warning);   } } 

i've tried manually create startup class in main method, doesn't seem right solution , hasn't far allowed me configure services.

i'm assuming there's way me create hostingcontext doesn't start web server keep console application alive. along lines of:

hostingcontext context = new hostingcontext() {   applicationname = "appname" };  using (new hostingengine().start(context)) {   // console code } 

however far way can work if set hostingcontext.serverfactorylocation microsoft.aspnet.server.weblistener, starts web server.

what you're looking right idea, think you'll need moment.

firstly, may have noticed default program class isn't using static methods anymore; because constructor gets dependency injection love on own!

public class program {     public program(iapplicationenvironment env)     {                 }              public void main(string[] args)     {     } } 

unfortunately, there aren't many of services you're used asp.net 5 hosting environment registered; this article , iservicemanifest can see there's few services available:

microsoft.framework.runtime.iassemblyloadercontainer microsoft.framework.runtime.iassemblyloadcontextaccessor microsoft.framework.runtime.iapplicationenvironment microsoft.framework.runtime.ifilemonitor microsoft.framework.runtime.ifilewatcher microsoft.framework.runtime.ilibrarymanager microsoft.framework.runtime.icompileroptionsprovider microsoft.framework.runtime.iapplicationshutdown

this means you'll joy of creating own service provider, too, since can't 1 provided framework.

private readonly iserviceprovider serviceprovider;  public program(iapplicationenvironment env, iservicemanifest servicemanifest) {     var services = new servicecollection();     configureservices(services);     serviceprovider = services.buildserviceprovider(); }  private void configureservices(iservicecollection services) { } 

this takes away lot of magic see in standard asp.net 5 projects, , have service provider wanted available in main.

there's few more "gotchas" in here, might list them out:

  • if ask ihostingenvironment, it'll null. that's because hosting environment comes from, well, asp.net 5 hosting.
  • since don't have 1 of those, you'll left without ihostingenvironment.environmentname - you'll need collect environment variables yourself. which, since you're loading configuration object, shouldn't problem. (it's name "aspnet_env", can add in debug tab of project settings; not set default console applications. you'll want rename that, anyway, since you're not talking aspnet environment anymore.)

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -