NancyFx / Nancy

Lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono
http://nancyfx.org
MIT License
7.15k stars 1.47k forks source link

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

Closed deepakkumpala closed 6 years ago

deepakkumpala commented 6 years ago

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

Prerequisites

Description

Steps to Reproduce

System Configuration

khellang commented 6 years ago

Sure, why not 😄

deepakkumpala commented 6 years ago

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.

khellang commented 6 years ago

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.

deepakkumpala commented 6 years ago

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.

khellang commented 6 years ago

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

deepakkumpala commented 6 years ago

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?

khellang commented 6 years ago

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

deepakkumpala commented 6 years ago

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

khellang commented 6 years ago

Awesome! Feel free to keep asking if you encounter problems 😄

deepakkumpala commented 6 years ago

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 ?

khellang commented 6 years ago

You should be able to use https://github.com/NancyFx/Nancy.Bootstrappers.Autofac

deepakkumpala commented 6 years ago

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!

deepakkumpala commented 6 years ago

I have created a question in stack overflow https://stackoverflow.com/questions/51988982/how-do-i-integrate-nancy-with-autofac-and-asp-net-core-windows-service