5

アイデアは、NancyModule2 つの異なるポートでトラフィックを処理する 2 つのクラスがあるということです。例えば:

  • FirstModule聞いているlocalhost:8081
  • SecondModule聞いているlocalhost:8082

現在、 と の両方でNancy.Hosting.SelfNancy インスタンスを作成するために使用しています。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();
}

FirstModule : NancyModuleクラスを port8081でのみSecondModule : NancyModuleリッスンし、 port でのみリッスンするにはどうすればよい8082ですか?

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

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

1 に答える 1

9

NancyModule 登録を分離するカスタム ブートストラッパーを使用して、サーバーごとに個別のプロジェクトに分割できます。

この例は、サーバーごとに 2 つのクラス ライブラリと、それらを起動する 1 つのコンソール アプリケーションを使用する 3 つの部分からなるソリューションです。

最初のサーバー プロジェクト

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

2 番目のサーバー プロジェクト

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();
        }
    }
}
于 2012-11-20T16:14:12.497 に答える