将网络添加到.net核心控制台应用程序

时间:2018-07-30 04:27:43

标签: c# asp.net .net-core console-application kestrel-http-server

我有。NET core console application,在这里我开始一个过程。现在,我想添加web page,可以在其中添加该过程的结果。

这是我的program.cs

class Program
{
    static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true)
            .AddCommandLine(args)
            .Build();

        return WebHost.CreateDefaultBuilder(args)
            .UseKestrel()
            .UseUrls("http://localhost:5000")
            .UseConfiguration(config)
            .CaptureStartupErrors(true)
            .PreferHostingUrls(false)
            .Configure(app =>
            {
                IServiceCollection services = new ServiceCollection();
                Startup startup = new Startup();
                var serviceProvider = startup.ConfigureServices(services);
                IWindsorContainer container = serviceProvider.GetService<IWindsorContainer>();
                var configuration = container.Resolve<IConfigurationRoot>();
                var cron = configuration[Enums.ExecutionTime];
                var _logger = container.Resolve<IIBLogger>();
                _logger.LogInfo("Started application");
                RunIbit(cron);
            })
            .Build();
    }

    public static void RunIbit(string cron)
    {

    }

    public static async void Execute(string cron)
    {

    }
}

这是我的startup.cs

public class Startup
{
    IConfigurationRoot Configuration { get; }

    public static readonly LoggerFactory MyLoggerFactory = new LoggerFactory();

    public Startup()
    {
        var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
        Configuration = builder.Build();
    }

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddLogging();
        services.AddMvcCore();
        var serviceProvider = services.BuildServiceProvider();
        services.AddSingleton<IConfigurationRoot>(Configuration);

        return WindsorRegistrationHelper.CreateServiceProvider(ContainerManager.Container, services);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

但是,当我运行程序时,没有打开网页。它显示了无法访问站点localhosdt:5000。我的程序缺少使用Web界面启动它的功能吗?从我看过的示例来看,它似乎应该可以正常工作,所以我不确定是什么问题。

编辑:我设法在Web浏览器上启动服务器,但是后来我无法运行Process方法,因此它要么执行进程,要么在Web上运行,我该如何做到两者兼而有之?

这是到目前为止的代码:

static void Main(string[] args)
    {
        BuildWebHost(args).Run();
        RunIbit();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true)
            .AddCommandLine(args)
            .Build();

        var host = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .UseConfiguration(config)
                .Configure(app =>
                {

                })
            .Build();
        return host;
        //return WebHost.CreateDefaultBuilder(args)
        //    .UseKestrel()
        //    .UseConfiguration(config)
        //    .Configure(app =>
        //    {
        //        IServiceCollection services = new ServiceCollection();
        //        Startup startup = new Startup();
        //        var serviceProvider = startup.ConfigureServices(services);
        //        IWindsorContainer container = serviceProvider.GetService<IWindsorContainer>();
        //        var configuration = container.Resolve<IConfigurationRoot>();
        //        var cron = configuration[Enums.ExecutionTime];
        //        var _logger = container.Resolve<IIBLogger>();
        //        _logger.LogInfo("Started application");
        //        RunIbit(cron);
        //    })
        //    .Build();
    }

Startup.cs

 public void Configure(IApplicationBuilder app)
    {
        app.Run(context =>
        {
            return context.Response.WriteAsync("Hello world");
        });

    }

我当然知道我不需要return context.Response.WriteAsync("Hello world");这行,但这就是我在网上获取的方式,我不知道如何更改

0 个答案:

没有答案