Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net vNext self-hosting within existing application

I've hunted for the answer to this one, in SO and beyond but I've not seen any answers thus far.

We are looking at adding some reporting to an existing Windows Services / WPF EXE. Ideally we'd self-host a little vNext application that would expose reporting endpoints our app can use. This was possible with OWIN and ASP.NET 4.

Is this even possible with vNext?

I've tried a few samples etc and the K Runtime seems to, clearly, be a different runtime to the CLR. Build etc is all rather different too... so I guess at the very least it would have to be a completely separate process .... or am I barking up the wrong tree?

In particular it seems we need to invoke the K runtime (k web or elsed a k pack'ed .cmd) which seems coutner intuitive as I'm already within a process I'm running (the main exe/service).

EDIT: I'm wondering if the answer is NoWin , referenced and providing the OWIN container. B ut I'm struggling to see if that's the best approach...

like image 865
penderi Avatar asked Oct 31 '22 11:10

penderi


1 Answers

Here a possible solution: How to Run DNX Applications in a Windows Service and How to Host ASP.NET in a Windows Service thanks to Erez Testiler.

Basically the idea is to add the following references:

  • "Microsoft.AspNet.Hosting": "1.0.0-beta7" – Bootstraps the web server
  • "Microsoft.AspNet.Server.Kestrel": "1.0.0-beta7" – Web server implementation
  • "Microsoft.AspNet.StaticFiles": "1.0.0-beta7" – Hosts static files
  • "Microsoft.AspNet.Mvc": "6.0.0-beta7" – Includes all the MVC packages

And then programmatically configure and start the Server and ASP.NET:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Hosting.Internal;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.Configuration.Memory;
using Microsoft.Framework.DependencyInjection;
using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;

....

private readonly IServiceProvider _serviceProvider;
private IHostingEngine _hostingEngine;
private IDisposable _shutdownServerDisposable;

public Program(IServiceProvider serviceProvider)
{
    _serviceProvider = serviceProvider;
}

protected override void OnStart(string[] args)
{
    var configSource = new MemoryConfigurationSource();
    configSource.Add("server.urls", "http://localhost:5000");

    var config = new ConfigurationBuilder(configSource).Build();
    var builder = new WebHostBuilder(_serviceProvider, config);
    builder.UseServer("Microsoft.AspNet.Server.Kestrel");
    builder.UseServices(services => services.AddMvc());
    builder.UseStartup(appBuilder =>
    {
        appBuilder.UseDefaultFiles();
        appBuilder.UseStaticFiles();
        appBuilder.UseMvc();
    });

    _hostingEngine = builder.Build();
    _shutdownServerDisposable = _hostingEngine.Start();
}

It seems to be a quite good solution to me.

like image 59
Davide Icardi Avatar answered Nov 11 '22 15:11

Davide Icardi