南希-两个模块在不同的端口上侦听 [英] Nancy - two modules listening on different ports

查看:48
本文介绍了南希-两个模块在不同的端口上侦听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个想法是我有两个NancyModule类,它们将处理两个不同端口上的通信.例如:

The idea is that I have two NancyModule classes that will be handling traffic on two different ports. For instance:

  • FirstModulelocalhost:8081
  • 上收听
  • SecondModulelocalhost:8082
  • 上收听
  • FirstModule listening on localhost:8081
  • SecondModule listening on localhost:8082

我目前正在使用Nancy.Hosting.Selflocalhost:8081localhost:8082上创建Nancy实例:

I'm currently using Nancy.Hosting.Self to create Nancy instances on both localhost:8081 and localhost: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();
}

如何使类FirstModule : NancyModule仅在端口8081上侦听,而SecondModule : NancyModule仅在端口8082上侦听?

How do I make class FirstModule : NancyModule listen only on port 8081 and SecondModule : NancyModule listen only on port 8082?

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

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

推荐答案

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

you could split it up into separate projects for each server with a custom bootstrapper to separate the NancyModule registration.

此示例是一个由三部分组成的解决方案,其中每个服务器都有两个类库,而一个控制台应用程序可以启动它们.

This example is a three part solution with two class libraries for each server and one console application to launch them.

第一个服务器项目

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

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

Launch them both from a separate Console application or whatever

using System;

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

这篇关于南希-两个模块在不同的端口上侦听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆