5

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

Eilon
  • 25,582
  • 3
  • 84
  • 102
penderi
  • 8,673
  • 5
  • 45
  • 62
  • I'm talking out of ignorance here, but why shoehorn a web app into what is clearly a windows app here? if no web server is required to handle requests and each is deployed per machine, why bother with this conundrum – user3036342 Jan 14 '15 at 09:53
  • Hard to know what to say lol. It's rather convenient to host an API allowing monitoring or reporting. It's something I've used in the past and it's certainly not new or very crazy. A few tools/apps do this - with a central app collecting, analysing stats/logs, say and a separate app with a dedicated app to provide feeds/reporting. I'll grant you it's beyond the usual deployment scenarios and due to the mix of runtimes (CLR and now K) it's understandable why this now isn't an option.... – penderi Jan 14 '15 at 13:09

2 Answers2

2

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.

Davide Icardi
  • 11,919
  • 8
  • 56
  • 77
0

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

penderi
  • 8,673
  • 5
  • 45
  • 62