2016-03-17 18 views
1

我有一個包含ASP .NET核心一個窗口服務應用ASP.NET核心作爲Windows服務的wwwroot位置

的project.json有2個commans:

"commands": { 
    "web": "Microsoft.AspNet.Server.Kestrel", 
    "service": "MyAppNamespace" 
} 

入口點類:

public class Program : ServiceBase 
{ 
    IApplication _application; 

    public Program(IServiceProvider serviceProvider) 
    { 

    } 

    public void Main(string[] args) 
    { 
     Run(this); 
    } 

    protected override void OnStart(string[] args) 
    { 
     var configProvider = new MemoryConfigurationProvider(); 
     configProvider.Add("server.urls", "http://*:80"); 

     var config = new ConfigurationBuilder() 
      .Add(configProvider) 
      .AddEnvironmentVariables() 
      .Build(); 

     _application = new WebHostBuilder(config) 
      .UseServer("Microsoft.AspNet.Server.Kestrel") 
      .UseStartup<Startup>() 
      .Build() 
      .Start(); 

     // my stuff bellow 
    } 

    protected override void OnStop() 
    { 
     _application?.Dispose(); 
    } 
} 

發佈項目後,我們有這樣的文件夾結構:

approot 
logs 
wwwroot 

接下來,我從控制檯

dnx.exe -p "C:\MyProject\approot\src\MyProject" service 

開始我的應用程序,但使用的根目錄,從目錄 「C:\ MyProject的\ wwwroot文件」 我們有相同project.json即「C:\ MyProject的\爲approot的\ src \ MyProject的網站‘命令類似這樣的」

如果我通過啓動應用程序’:

dnx.exe -p "C:\MyProject\approot\src\MyProject" web 

那麼所有的作品應該的,但當然,我的切入點被忽略,我不能執行我的東西,無論是運行作爲服務

project.json

{ 
    "version": "1.0.0-*", 

    "dependencies": { 
    "MicroORM": "1.0.7", 
    "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1", 
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final", 
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", 
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final", 
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", 
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Configuration": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final", 
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final", 
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final", 
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final", 
    "Npgsql": "3.0.5", 
    "ObjectPool": "1.0.0" 
    }, 

    "commands": { 
    "web": "Microsoft.AspNet.Server.Kestrel --server.urls=http://*:80", 
    "service": "MikroBill" 
    }, 

    "frameworks": { 
    "dnx46": { 
     "frameworkAssemblies": { 
     "System.Data": "4.0.0.0", 
     "System.ServiceProcess": "4.0.0.0" 
     } 
    } 
    }, 

    "exclude": [ 
    "wwwroot", 
    "node_modules" 
    ], 
    "publishExclude": [ 
    "**.user", 
    "**.vspscc" 
    ], 
    "scripts": { 
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ] 
    } 
} 
+0

請問您可以發佈您的整個project.json文件。 –

回答

2

較短的回答

我們可以添加webroot關鍵是我們MemoryConfigurationProvider

configProvider.Add("webroot", "relative-path-to-web-root"); 

或者,我們可以註冊我們與ConfigurationBuilder hosting.json文件:

config.AddJsonFile("hosting.json", optional: true) 

protected override void OnStart(string[] args) 
{ 
    var configProvider = new MemoryConfigurationProvider(); 
    configProvider.Add("server.urls", "http://localhost:5000"); 
    configProvider.Add("webroot", "./../../../my-root"); 

    var config = new ConfigurationBuilder() 
     .Add(configProvider) 
     .AddJsonFile("hosting.json", optional: true) 
     .Build(); 

    _application = new WebHostBuilder(config) 
     .UseServer("Microsoft.AspNet.Server.Kestrel") 
     .UseStartup<Program>() 
     .Build() 
     .Start(); 
} 

再回應

當我們傳遞一個ConfigurationBuilder進入WebHostBuilder構造,我們不再免費獲得默認設置。這是空的WebHostBuilder構造函數。

public WebHostBuilder() 
    : this(config: new ConfigurationBuilder().Build()) { } 

它可能使用WebHostConfiguration.GetDefault方法,它增加了hosting.json文件給我們。

public static IConfiguration GetDefault(string[] args) 
{ 
    // code omitted for clarity 

    var configBuilder = new ConfigurationBuilder() 
     .AddInMemoryCollection(defaultSettings) 
     .AddJsonFile(WebHostDefaults.HostingJsonFile, optional: true) 
     .AddEnvironmentVariables(prefix: 
      WebHostDefaults.EnvironmentVariablesPrefix); 

    // code omitted for clarity 

    return configBuilder.Build(); 
} 

我們可以在我們的代碼中做同樣的事情。 Here is a sample for good measure.

+1

什麼都沒有改變 – Miles

+0

我還有一個選項。如果我們簡單地將wwwroot複製到項目目錄中,它就可以工作,但我不太喜歡這種方法 – Miles

+0

您能告訴我怎麼做嗎? :) – Miles