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