vNext: Console app that uses razor views without hosting -


i creating console application file conversions. these conversions done creating model input file , executing razor models output.

to have working in ide used visual studio 2015 preview , created vnext console application uses mvc. (you razor support out of box then). working need host mvc app though, , cheapest way hosting through weblistener. host mvc app , call through "http://localhost:5003/etc/etc" rendered views construct output.

but console app not supposed listen to/use port. command line tool file conversions. if multiple instances run @ same time fight host pages on same port. (this of coarse prevented choosing port dynamically, not looking for)

so question how working without using port, using of vnext frameworks possible.

in short: how can use cshtml files pass models in console app not use port using vnext razor engine.

here code use:

program.cs

using microsoft.aspnet.hosting; using microsoft.aspnet.http; using microsoft.aspnet.mvc; using microsoft.framework.configurationmodel; using microsoft.framework.dependencyinjection; using microsoft.framework.dependencyinjection.fallback; using system; using system.io; using system.net.http; using system.net.http.headers; using system.threading; using system.threading.tasks; using system.xml.linq;  namespace consoletest {     public class program     {         private readonly iserviceprovider _hostserviceprovider;          public program(iserviceprovider hostserviceprovider)         {             _hostserviceprovider = hostserviceprovider;         }          public async task<string> getwebpageasync()         {             using (var httpclient = new httpclient())             {                 httpclient.baseaddress = new uri("http://localhost:5003/home/svg?idx=1");                 httpclient.defaultrequestheaders.accept.clear();                 httpclient.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("text/xml"));                 return await httpclient.getstringasync("");             }         }          public task<int> main(string[] args)         {             var config = new configuration();             config.addcommandline(args);              var servicecollection = new servicecollection();             servicecollection.add(hostingservices.getdefaultservices(config));             servicecollection.addinstance<ihostingenvironment>(new hostingenvironment() { webroot = "wwwroot" });             var services = servicecollection.buildserviceprovider(_hostserviceprovider);              var context = new hostingcontext()             {                 services = services,                 configuration = config,                 servername = "microsoft.aspnet.server.weblistener",                 applicationname = "consoletest"             };              var engine = services.getservice<ihostingengine>();             if (engine == null)             {                 throw new exception("todo: ihostingengine service not available exception");             }              using (engine.start(context))             {                 var tst = getwebpageasync();                 tst.wait();                 file.writealltext(@"c:\\result.svg", tst.result.trimstart());                  console.writeline("started server..");                 console.writeline("press key stop server");                 console.readline();             }             return task.fromresult(0);         }     } } 

startup.cs

using microsoft.aspnet.builder; using microsoft.framework.dependencyinjection; using microsoft.aspnet.routing; using microsoft.framework.configurationmodel;  namespace consoletest {     public class startup     {         public iconfiguration configuration { get; private set; }          public void configureservices(iservicecollection services)         {             // add mvc services services container             services.addmvc();         }          public void configure(iapplicationbuilder app)         {             //configure webfx             app.usemvc(routes =>             {                 routes.maproute(                     null,                     "{controller}/{action}",                     new { controller = "home", action = "index" });             });         }     } } 

i solved using following code:

program.cs

using system; using system.threading.tasks; using microsoft.aspnet.testhost; using microsoft.aspnet.builder; using microsoft.framework.runtime.infrastructure;  namespace consoletest {     public class program     {         private action<iapplicationbuilder> _app;         private iserviceprovider _services;          public async task<string> testme()         {             var server = testserver.create(_services, _app);             var client = server.createclient();             return await client.getstringasync("http://localhost/home/svg?idx=1");         }          public void main(string[] args)         {             _services = callcontextservicelocator.locator.serviceprovider;             _app = new startup().configure;              var x = testme();             x.wait();             console.writeline(x.result);              console.readline();         }     } } 

startup.cs

using microsoft.aspnet.builder; using microsoft.framework.dependencyinjection; using microsoft.aspnet.routing;  namespace consoletest {     public class startup     {         public void configure(iapplicationbuilder app)         {             app.useservices(services =>             {                 // add mvc services services container                 services.addmvc();             });             //configure webfx             app.usemvc(routes =>             {                 routes.maproute(                     null,                     "{controller}/{action}",                     new { controller = "home", action = "index" });             });         }     } } 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -