vNext:使用razor视图而不托管的控制台应用程序

vNext:使用razor视图而不托管的控制台应用程序,razor,console,asp.net-core,visual-studio-2015,Razor,Console,Asp.net Core,Visual Studio 2015,我正在创建执行一些文件转换的控制台应用程序。这些转换很容易完成,从输入文件创建模型,然后为输出执行razor模型 为了在IDE中实现这一点,我使用Visual Studio 2015 preview并创建了一个使用MVC的vnext控制台应用程序。(你可以从盒子里拿出剃须刀支架)。为了让这一切正常工作,你需要托管MVC应用程序,最便宜的方法是通过WebListener托管。因此,我主持了MVC应用程序,然后通过调用它。”http://localhost:5003/etc/etc“获取构成输出的渲

我正在创建执行一些文件转换的控制台应用程序。这些转换很容易完成,从输入文件创建模型,然后为输出执行razor模型

为了在IDE中实现这一点,我使用Visual Studio 2015 preview并创建了一个使用MVC的vnext控制台应用程序。(你可以从盒子里拿出剃须刀支架)。为了让这一切正常工作,你需要托管MVC应用程序,最便宜的方法是通过WebListener托管。因此,我主持了MVC应用程序,然后通过
调用它。”http://localhost:5003/etc/etc“
获取构成输出的渲染视图

但控制台应用程序不应侦听/使用端口。它只是一个用于文件转换的命令行工具。如果多个实例将同时运行,它们将努力在同一端口上托管页面。(这可以通过动态选择端口来防止,但这不是我想要的)

所以我的问题是,如果不使用端口,而是使用尽可能多的vnext框架,您将如何实现这一点

简言之:我如何在不使用vnext razor引擎端口的控制台应用程序中使用传递模型的cshtml文件

以下是我目前使用的一些代码:

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 the server..");
                Console.WriteLine("Press any key to stop the server");
                Console.ReadLine();
            }
            return Task.FromResult(0);
        }
    }
}
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();
        }
    }
}

我用以下代码解决了这个问题:

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 the server..");
                Console.WriteLine("Press any key to stop the server");
                Console.ReadLine();
            }
            return Task.FromResult(0);
        }
    }
}
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();
        }
    }
}

内存托管中没有涉及网络…你可以看看MVC repo的功能测试,看看它是如何在那里完成的…嘿,谢谢,放弃朝那个方向看,因为我不认为TestServer在mem中。我会试一试的!让它工作:-)结果证明是容易的!酷…我可以问一下您想要实现的场景是什么吗?。创建一个控制台应用程序,将包含信息的xml文件转换为svg、png、dae、css和json文件。它将在内部使用Razor引擎来创建svg、dae、css和json。png文件将使用phantomjs创建。您的
TestServer
看起来像什么?
Microsoft.AspNet.TestHost.TestServer
在包“
Microsoft.AspNet.TestHost
”中,也就是说……如果没有源代码,我们会在哪里??)