自托管vnext应用程序

时间:2014-08-20 11:39:36

标签: self-hosting asp.net-core

我想知道我是否可以重构一个自托管的应用程序(启动并显示它提供的Web服务的URL的控制台应用程序),以便它可以在纯vnext而不是owin上工作。

owin代码如下

namespace Selfhostingtest
{
    class Program
    {
        static void Main(string[] args)
        {
            String strHostName = string.Empty;
            strHostName = Dns.GetHostName();
            Console.WriteLine("Local Machine's Host Name: " + strHostName);
            var options = new StartOptions();
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;
            for (int i = 0; i < addr.Length; i++)
            {
                if (!addr[i].IsIPv6LinkLocal && addr[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    Console.WriteLine("IPv4 Address {0}: {1} ", i, addr[i].ToString());
                    options.Urls.Add(String.Format("http://{0}:5000/", addr[i].ToString()));
                }
            }
            using (WebApp.Start<Startup>(options))
            {
                Console.WriteLine("Razor server is running. Press enter to shut down...");
                Console.ReadLine();
            }
        }
    }
}

为了记录,我不想使用“k web”命令行启动。我想将vnext应用程序完全打包为可执行文件。

应该使用Microsoft.AspNet.Hosting而不是Microsoft.Owin.Hosting(与“k web”命令定义中的类相同。请记住,Owin Startup期望IAppBuilder和vnext期望IBuilder。

1 个答案:

答案 0 :(得分:2)

在ASP.NET vNext中,您无法构建EXE文件,但您绝对可以将应用程序打包为自包含。查看可以在应用程序文件夹中运行的kpm pack命令。它将打包所有依赖项,并生成您可以使用的命令脚本(而不是使用k web等)。最后,如果你看看k web做了什么,它只是一些shell脚本最终运行klr.exe并带有各种参数来指示它应该从什么开始。

项目维基有关kpm工具各种选项的一些基本信息:https://github.com/aspnet/Home/wiki/Package-Manager

以下是kpm pack的命令行帮助,可以让您了解它可以执行的操作。

Usage: kpm pack [arguments] [options]

Arguments:
  [project]  Path to project, default is current directory

Options:
  -o|--out <PATH>                  Where does it go
  --configuration <CONFIGURATION>  The configuration to use for deployment
  --overwrite                      Remove existing files in target folders
  --no-source                      Don't include sources of project dependencies
  --runtime <KRE>                  Names or paths to KRE files to include
  --appfolder <NAME>               Determine the name of the application primary folder
  -?|-h|--help                     Show help information