c# - Nancy - 两个模块监听不同的端口

标签 c# .net nancy

我的想法是我有两个 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 只监听端口 8081SecondModule : NancyModule 只监听端口 8082?

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

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

最佳答案

您可以将它拆分为每个服务器的单独项目,使用自定义 Bootstrap 来分离 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();
        }
    }
}

关于c# - Nancy - 两个模块监听不同的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13436914/

相关文章:

.net - 为什么必须有一个委托(delegate)来桥接线程及其方法?

c# - 尝试使用 Nancy SuperSimpleViewEngine 渲染局部时获取 [ERR!] 作为输出

nancy - 使用 super 简单 View 引擎时如何设置列表索引

c# - 保持 Dotnet Core Grpc Server 作为控制台应用程序运行?

c# - 没有参数的 CQRS 查询处理程序

c# - LINQ Left Outer Join 结合 Let

c# - 为什么 Stream.ReadByte 返回 int?

c# - .NET 中 NaN 数的错误 Math.Pow() 实现

c# - Nancy:在 AfterRequest 事件中修改模型?

c# - C# 中的复合数据类型