NancyFx / Nancy

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

WPF with Nancy tutorial? #2972

Closed conekeo closed 4 years ago

conekeo commented 4 years ago

Hi,

Could someone redirect me to a good WPF with Nancy v2 tutorial? Or how do I implement that?

Thanks!

cocowalla commented 4 years ago

WPF as in "Windows Presentation Foundation", the desktop framework? I don't see how that's related to Nancy?

conekeo commented 4 years ago

Yes, Windows Presentation Foundation. I just want to create an WPF app an integrate Nancy in it. But I don't know where to start. I just want to handle a simple

  Get("/", args => "Hello World");
jonorossi commented 4 years ago

Check out Nancy's self hosting documentation, I've been using it in a console application for years: https://github.com/NancyFx/Nancy/wiki/Self-Hosting-Nancy

conekeo commented 4 years ago

I saw the console application, but I'm looking for simple WPF implementation.

phillip-haydon commented 4 years ago

It’s the same code. You would just set it up and store it to a static variable in your application initialization code since it doesn’t exist the same way a console app does.

conekeo commented 4 years ago

@phillip-haydon Could you elaborate more on how to implement it? Here what I got so far

MainWindow.xaml.cs

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            using (var host = new NancyHost(new Uri("http://localhost:80")))
            {
                host.Start();
                Console.WriteLine("NancyFX Stand alone test application.");           
            }

            HelloModule hello = new HelloModule();

        }
    }

HelloModule.cs

 public class HelloModule : NancyModule
    {
        public HelloModule()
        {
            Get("/", parameters => "Hello World");
        }
    }
Zaid-Ajaj commented 4 years ago

@conekeo You never initialize the Nancy modules by yourself, that is the work of the Bootstrapper (see Wiki) where it scans the current assembly looking for the modules and registering them (along with their dependencies).

A NancyHost takes a bootstrapper as argument, so you should implement one first:

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
         // your customization goes here
    }
}
conekeo commented 4 years ago

@Zaid-Ajaj Thanks! All right it's working!

Here my WPF working sample:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            NancyHost host = new NancyHost(new Bootstrapper(), new Uri("http://localhost:80"));

            host.Start();
            Console.WriteLine("NancyFX Stand alone test application.");     
        }
    }

    public class Bootstrapper : DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            // your customization goes here
        }
    }

 public class HelloModule : NancyModule
    {
        public HelloModule()
        {
            Get("/", parameters => "Hello World");

        }
    }