C# Nancy-两个模块在不同端口上侦听

C# Nancy-两个模块在不同端口上侦听,c#,.net,nancy,C#,.net,Nancy,我的想法是,我有两个NancyModule类,它们将处理两个不同端口上的流量。例如: FirstModule监听localhost:8081 SecondModule监听localhost:8082 我目前正在使用Nancy.Hosting.Self在localhost:8081和localhost:8082上创建Nancy实例: internal static void Main(string[] args) { var uris = new Uri[] { new

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

  • FirstModule
    监听
    localhost:8081
  • SecondModule
    监听
    localhost:8082
我目前正在使用
Nancy.Hosting.Self
localhost:8081
localhost:8082
上创建Nancy实例:

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();
}
如何使类
FirstModule:NancyModule
仅在端口
8081
上侦听,以及
SecondModule:NancyModule
仅在端口
8082
上侦听

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

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

您可以使用自定义引导程序将其拆分为每个服务器的单独项目,以分离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)));
            }
        }
    }
}

为什么不拥有两个独立的站点,并使用两个独立的进程在不同的端口上自我托管它们呢?但这有什么真正的用途呢?我正在构建一个可配置的“存根”服务器。其中一个将托管对已配置URL的固定响应。另一个将用作restful端点,用于修改第一个服务器上当前加载的响应。我们已经为Java和Node.jsHi实现了这个工具,您没有得到任何异常吗?如果在同一个程序集中或同一目录中的单独程序集文件(dll、exe)中有多个“DefaultNancyBootstrapper”,则会出现异常,提示“找到多个引导程序:”我还在吃这个problem@Locke如果您找到了多个引导程序:,那么只需在
\u server=new NancyHost(new Bootstrapper(),new Uri(“http://localhost:9696"));例如
\u服务器=新NancyHost(新的MyLocalBookTrapper(),新的Uri(“http://localhost:9696"));@Sammi我只需调用“WebApp.Start()”和“UseNancy()”。我想我需要改变启动服务器的方式,但是怎么做呢?我就像这里的这个家伙一样:
var startOptions=new startOptions();startOptions.url.Add(“http://+:9696”);使用(WebApp.Start(startOptions,builder=>{var nancyOptions=new nancyOptions();nancyOptions.Bootstrapper=new MyLocalBootstrapper();builder.UseNancy(nancyOptions);})){Console.ReadLine();}
不起作用,给出此错误:错误CS1061“Bootstrapper”不包含“GetModuleKeyGenerator”的定义,并且找不到接受“Bootstrapper”类型的第一个参数的扩展方法“GetModuleKeyGenerator”(是否缺少using指令或程序集引用?)
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)));
            }
        }
    }
}
using System;

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