Nancy: Can I host windows service using Nancy without .net framework?

Created on 21 Aug 2018  路  13Comments  路  Source: NancyFx/Nancy

Can I host windows service using Nancy without .net framework? I want to use .net core.

Prerequisites

  • [ ] I have written a descriptive issue title
  • [ ] I have verified that I am running the latest version of Nancy
  • [ ] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [ ] I have searched open and closed issues to ensure it has not already been reported

Description

Steps to Reproduce

System Configuration

  • Nancy version:
  • Nancy host

    • [ ] Nancy.Hosting.Aspnet

    • [ ] Nancy.Hosting.Self

    • [ ] Nancy.Owin ()

    • [ ] Other:

  • Other Nancy packages and versions:
  • Environment (Operating system, version and so on):
  • .NET Framework version:
  • Additional information:

All 13 comments

Sure, why not 馃槃

How? I am not finding enough resources. Basically I am using .net core 2.1 and .net standard for all the libraries in my project. I want to install it as windows service and host pages at localhost for browsing. Basically trying to achieve below result using Nancy

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.1

I can not use TopShelf as Topshelf current version is not supported by .net core 2.0 applications.

Have you tried going through the guide? Try after you've set it up as a Windows service, it's just a matter of hooking Nancy up in your Startup class. Look at the Kestrel sample in this repo to see how it's done.

I have already seen Kestrel sample but it can not be hosted as windows service. I do not see "Start" and "Stop" methods or service manager class to override my functionalities.

Which guide you are referring to? kindly provide the link to exact requirement.

Thank you. so if I understand currently, once I create windows service using .net core then we need to hook up Nancy. I have added Nancy.Hosting.Self nuget package , but I am getting below warning

Package 'Nancy.Hosting.Self 1.4.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.1'. This package may not be fully compatible with your project.

Then I tried below

Install-Package Nancy -Pre

which resolved the warning, but I am not able to get app.UseNancy() under Configure startup.

Then I tried adding nuget Microsoft.AspNetCore.Owin

This I am able to write code as below

app.UseOwin(x => x.UseNancy());

But my question now is, Is it right way? Am I in right path?

I am new bee to development. Is there any sample which shows hooking up Nancy to .net core 2.1 windows service?

Is there any sample which shows hooking up Nancy to .net core 2.1 windows service?

No, but as I mentioned, you need to combine two resources:

  1. The tutorial you mentioned above to set up a Windows service
  2. The Kestrel sample in this repo.

The first will show you how to host ASP.NET Core in a Windows service, the second will show you how to hook up Nancy in your Startup class.

Nancy.Hosting.Self is .NET Framework only (not .NET Core). In addition to the core Nancy package, you need to install the following packages:

https://github.com/NancyFx/Nancy/blob/dbdbe9428caea06c17d34a507c9216fe35abadc4/samples/Nancy.Demo.Hosting.Kestrel/Nancy.Demo.Hosting.Kestrel.csproj#L16-L18

Adding Nancy to your pipeline is done like this:

https://github.com/NancyFx/Nancy/blob/dbdbe9428caea06c17d34a507c9216fe35abadc4/samples/Nancy.Demo.Hosting.Kestrel/Startup.cs#L26

Thank you. we can close this ticket. Appreciated. I have updated my previous question. Great. Thanks

Awesome! Feel free to keep asking if you encounter problems 馃槃

I would like reopen this ticket as I am stuck in integrating Autofac to the above solution. The reason I need Autofac is my existing class libraries have dependency on Autofac.

Now, How do I integrate Autofac to Nancy ?

Thank you. Appreciated support.

I have added prerelease version

Install-Package Nancy.Bootstrappers.Autofac -Version 2.0.0-clinteastwood

I have managed to build the application without build errors from below code but unable to get the instances. Basically how do I inject?

       protected override void ConfigureApplicationContainer(ILifetimeScope container)
       {

        _services.Configure<Settings>(_configuration.GetSection("MySettings"));
        var deviceId = _configuration["Settings:DeviceId"];

        var containerFactory = new ContainerFactory();

        container.Update(builder =>
        {
            builder.RegisterInstance(containerFactory).As<IContainerFactory>();
            containerBuilder.Register(c => new Engine(containerFactory));
            containerBuilder.Register(i => new HubClient(deviceId, containerFactory)).As<HubClient> ().SingleInstance();
        });
        base.ConfigureApplicationContainer(container);
    }

But now how to I inject it in my modules?

     public static void Main(string[] args)
    {
        var isService = !(Debugger.IsAttached || args.Contains("--console"));
        var builder = CreateWebHostBuilder(args.Where(arg => arg != "--console").ToArray());

        if (isService)
        {
            var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
            var pathToContentRoot = Path.GetDirectoryName(pathToExe);
            builder.UseContentRoot(pathToContentRoot);
        }

        var host = builder.Build();

        if (isService)
        {
             IMyInterface Engine = host.Services.GetService<IMyInterface>();// this line fails.
            host.RunAsCustomService();
        }
        else
        {
            host.Run();
        }
    }

Note: Engine class is dependent on 'DeviceId' and 'containerFactory' parameter

I am very close to solution! Hope you help me reach it!

Was this page helpful?
0 / 5 - 0 ratings