【问题标题】:Nancy - two modules listening on different portsNancy - 两个模块监听不同的端口
【发布时间】:2012-11-06 09:04:29
【问题描述】:

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

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

我目前正在使用Nancy.Hosting.Selflocalhost:8081localhost: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!"
    }
}

【问题讨论】:

  • 为什么不拥有两个独立的站点并使用两个独立的进程来自己托管它们在不同的端口上侦听?
  • 这个的真正用途是什么?
  • 我正在构建一个可配置的“存根”服务器。一种是托管对已配置 URL 的预设响应。另一个将作为一个 restful 端点,用于修改当前加载的第一个响应。我们已经为 Java 和 Node.js 实现了这个工具

标签: c# .net nancy


【解决方案1】:

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

第二个服务器项目

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();
        }
    }
}

【讨论】:

  • 您好,您没有收到任何异常吗?如果您在同一个程序集中或同一目录中的单独程序集文件(dll、exe)中有多个“DefaultNancyBootstrapper”,那么您将收到异常消息“Located multiple bootstrappers:”我仍然遇到这个问题
  • @Locke 如果你得到Located multiple bootstrappers:,那么只需在_server = new NancyHost(new Bootstrapper(), new Uri("http://localhost:9696")); 中指定一个更具体的引导程序即可。 _server = new NancyHost(new MyLocalBootstrapper(), new Uri("http://localhost:9696"));
  • @Sammi 我只调用了“WebApp.Start()”和“UseNancy()”。我想我需要改变我启动服务器的方式,但是如何?
  • 我在这里像这个人一样:strathweb.com/2013/05/…var startOptions = new StartOptions (); startOptions.Urls.Add ("http://+:9696"); using (WebApp.Start(startOptions, builder =&gt; { var nancyOptions = new NancyOptions(); nancyOptions.Bootstrapper = new MyLocalBootstrapper(); builder.UseNancy(nancyOptions); })) { Console.ReadLine(); }
  • 不起作用,给出此错误:错误 CS1061“Bootstrapper”不包含“GetModuleKeyGenerator”的定义,并且找不到接受“Bootstrapper”类型的第一个参数的扩展方法“GetModuleKeyGenerator” (您是否缺少 using 指令或程序集引用?)服务器
猜你喜欢
  • 2015-08-15
  • 2019-02-20
  • 1970-01-01
  • 2011-04-08
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多