南希 - 两个模块在不同的端口上侦听

mra*_*rak 4 .net c# nancy

我的想法是我有两个NancyModule类将处理两个不同端口上的流量.例如:

  • FirstModule 听着 localhost:8081
  • SecondModule 听着 localhost:8082

我目前使用Nancy.Hosting.Self建立在两个南希实例localhost:8081localhost:8082:

internal static void Main(string[] args) {
    var uris = new Uri[] {
        new Uri("localhost:8081"),
        new Uri("localhost:8082"),
    };

    var host = new NancyHost(uris);
    host.Start();
    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

如何让类FirstModule : NancyModule仅在端口上侦听8081SecondModule : NancyModule仅在端口上侦听8082

public class FirstModule : NancyModule {
    public FirstModule(){
        Get["/"] = _ => "Hello from FirstModule!"
    }
}

public class SecondModule : NancyModule {
    public FirstModule(){
        Get["/"] = _ => "Hello from SecondModule!"
    }
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*man 9

您可以使用自定义引导程序将其拆分为每个服务器的单独项目,以分隔NancyModule注册.

此示例是一个三部分解决方案,每个服务器有两个类库,一个控制台应用程序用于启动它们.

第一服务器项目

using System;
using System.Collections.Generic;
using System.Linq;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.Hosting.Self;

namespace Server1
{
    public class Server : NancyModule
    {
        private static NancyHost _server;

        public static void Start()
        {
            _server = new NancyHost(new Bootstrapper(), new Uri("http://localhost:8686"));
            _server.Start();
        }

        public Server()
        {
            Get["/"] = _ => "this is server 1";
        }
    }

    public class Bootstrapper : DefaultNancyBootstrapper
    {
        /// <summary>
        /// Register only NancyModules found in this assembly
        /// </summary>
        protected override IEnumerable<ModuleRegistration> Modules
        {
            get
            {
                return GetType().Assembly.GetTypes().Where(type => type.BaseType == typeof(NancyModule)).Select(type => new ModuleRegistration(type, this.GetModuleKeyGenerator().GetKeyForModuleType(type)));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

第二服务器项目

using System;
using System.Collections.Generic;
using System.Linq;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.Hosting.Self;

namespace Server2
{
    public class Server : NancyModule
    {
        private static NancyHost _server;

        public static void Start()
        {
            _server = new NancyHost(new Bootstrapper(), new Uri("http://localhost:9696"));
            _server.Start();
        }

        public Server()
        {
            Get["/"] = _ => "this is server 2";
        }
    }

    public class Bootstrapper : DefaultNancyBootstrapper
    {
        /// <summary>
        /// Register only NancyModules found in this assembly
        /// </summary>
        protected override IEnumerable<ModuleRegistration> Modules
        {
            get
            {
                return GetType().Assembly.GetTypes().Where(type => type.BaseType == typeof(NancyModule)).Select(type => new ModuleRegistration(type, this.GetModuleKeyGenerator().GetKeyForModuleType(type)));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从单独的控制台应用程序或其他任何内容启动它们

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Server1.Server.Start();
            Server2.Server.Start();
            Console.WriteLine("servers started...");
            Console.Read();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我在这里做了这样的人:http://www.strathweb.com/2013/05/the-future-is-now-owin-and-multi-hosting-asp-net-web-applications/`var startOptions =新的StartOptions(); startOptions.Urls.Add("http:// +:9696"); using(WebApp.Start(startOptions,builder => {var nancyOptions = new NancyOptions(); nancyOptions.Bootstrapper = new MyLocalBootstrapper(); builder.UseNancy(nancyOptions);})){Console.ReadLine(); }` (2认同)