gpt4 book ai didi

razor - vNext : Console app that uses razor views without hosting

转载 作者:行者123 更新时间:2023-12-01 02:13:42 24 4
gpt4 key购买 nike

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

为了让它在 IDE 中工作,我使用了 Visual Studio 2015 预览版并创建了一个使用 MVC 的 vnext 控制台应用程序。 (然后,您将获得开箱即用的 Razor 支持)。为了让这一切正常工作,您需要托管 MVC 应用程序,而最便宜的方式是通过 WebListener 托管。所以我托管 MVC 应用程序,然后通过 "http://localhost:5003/etc/etc" 调用它获取构建输出的渲染 View 。

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

所以我的问题是如何在不使用端口但尽可能多地使用 vnext 框架的情况下使其工作。

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

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

程序.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 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 to the services container
services.AddMvc();
}

public void Configure(IApplicationBuilder app)
{
//Configure WebFx
app.UseMvc(routes =>
{
routes.MapRoute(
null,
"{controller}/{action}",
new { controller = "Home", action = "Index" });
});
}
}
}

最佳答案

我使用以下代码解决了它:

程序.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();
}
}
}

启动文件
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 to the services container
services.AddMvc();
});
//Configure WebFx
app.UseMvc(routes =>
{
routes.MapRoute(
null,
"{controller}/{action}",
new { controller = "Home", action = "Index" });
});
}
}
}

关于razor - vNext : Console app that uses razor views without hosting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27013442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com