penderi
penderi

Reputation: 9073

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...

Upvotes: 5

Views: 1231

Answers (2)

Davide Icardi
Davide Icardi

Reputation: 12209

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.

Upvotes: 2

penderi
penderi

Reputation: 9073

Ok I spent some time on jabbr.net and had some help from the awesome @dfowl and a helpful if rather curt younger dev (those were the days).

@dfowl: that scenario Is pretty much dead

My take- as our Windows Service/WPF runs under CLR and vNext runs under CLR they are different runtimes.

There is a way to do it, based on an older version of the K runtime and it's er, hairy. File in possible, but never something you'd put in production:

Alxandr's CLR Bootstrap for K runtime

Upvotes: 0

Related Questions