.NET Nancy 详解(四) Self Host

时间:2023-10-13 13:29:43

Self Host 使得Nancy 能够在任意application 中启动,无论是console 还是windows service。这期我们使用的版本是Nancy v0.4.0。

Demo

首先看一下使用方式:

class Program
{
static void Main(string[] args)
{
var nancyHost = new NancyHost(new Uri("http://localhost:8888/nancy/"));
nancyHost.Start();
Console.WriteLine("Nancy now listening - navigate to http://localhost:8888/nancy/. Press enter to stop");
Console.ReadKey();
nancyHost.Stop();
Console.WriteLine("Stopped. Good bye!");
}
}

这是一个控制台的例子,我们在Main方法中直接创建一个NancyHost的实例,然后调用nancyHost.Start()。warning: 当前版本的实现为早期版本,仅仅是为了演示而使用,请勿deploy在生产环境中。

   public NancyHost(Uri baseUri, INancyBootstrapper bootStrapper)
{
this.baseUri = baseUri;
listener = new HttpListener();
listener.Prefixes.Add(baseUri.ToString()); bootStrapper.Initialise();
engine = bootStrapper.GetEngine();
}

NancyHost 需要两个参数,Uri,以及INancyBootstrapper。Uri这里我们传入的是new Uri("http://localhost:8888/nancy/"。INancyBootstrapper这个参数可以省略,Nancy 启动的时候会反射加载所有实现了INancyBootstrapper的启动器实现。INancyBootstrapper可以用来作依赖注入框架的启动器,实现该接口的类有:

.NET Nancy 详解(四) Self Host

可以看到除了测试用的fake实现,都是一些IOC框架(Ninject,StructureMap,Unity......)。NancyBootstrapperLocator类具体负责寻找实现了该接口的启动器,如果找不到,就会使用默认的TinyIoCContainer作为IOC 容器。

static NancyBootstrapperLocator()
{
// Get the first non-abstract implementation of INancyBootstrapper if one exists in the
// app domain. If none exist then just use the default one.
var bootstrapperInterface = typeof(INancyBootstrapper);
var defaultBootstrapper = typeof(DefaultNancyBootstrapper); var locatedBootstrappers = from assembly in AppDomain.CurrentDomain.GetAssemblies()
where !assembly.IsDynamic
from type in assembly.SafeGetExportedTypes()
where !type.IsAbstract
where bootstrapperInterface.IsAssignableFrom(type)
where type != defaultBootstrapper
select type; var bootstrapperType = locatedBootstrappers.FirstOrDefault() ?? defaultBootstrapper; Bootstrapper = (INancyBootstrapper) Activator.CreateInstance(bootstrapperType);
}

我们可以看到所有默认接口都指定了Default的实现。

private IEnumerable<TypeRegistration> BuildDefaults()
{
return new[]
{
new TypeRegistration(typeof(IRouteResolver), DefaultRouteResolver),
new TypeRegistration(typeof(INancyEngine), DefaultNancyEngine),
new TypeRegistration(typeof(IModuleKeyGenerator), DefaultModuleKeyGenerator),
new TypeRegistration(typeof(IRouteCache), DefaultRouteCache),
new TypeRegistration(typeof(IRouteCacheProvider), DefaultRouteCacheProvider),
new TypeRegistration(typeof(IRoutePatternMatcher), DefaultRoutePatternMatcher),
new TypeRegistration(typeof(IViewLocator), DefaultViewLocator),
new TypeRegistration(typeof(IViewFactory), DefaultViewFactory),
new TypeRegistration(typeof(INancyContextFactory), DefaultContextFactory),
new TypeRegistration(typeof(INancyModuleBuilder), DefaultNancyModuleBuilder),
new TypeRegistration(typeof(IResponseFormatter), DefaultResponseFormatter)
};
}

Nancy 面向接口编程做的比较的不错。接下去,我们继续完成NancyHost的初始化,这里创建一个HttpListener的实例。HttpListener是System.dll中提供的一个类:

  /// <summary>
/// Provides a simple, programmatically controlled HTTP protocol listener. This class cannot be inherited.
/// </summary>
public sealed class HttpListener : IDisposable

有了这个类就方便了很多,我们不需要自己去处理socket。接下去我们启动NancyHost。

    public void Start()
{
shouldContinue = true; listener.Start();
thread = new Thread(Listen);
thread.Start();
}

这里就是这个简易版本的问题所在了,程序只是启动了一个线程去处理所有的http请求。在线程中,将所有的context,request作了一轮适配器一样的转换,最终将NancyResponse转变会HttpListener需要的Response。不过虽然同样是一个死循环的写法,while (shouldContinue)的做法明显是优于while(true)的,无则加勉,大家要有这个意识。

.NET Nancy 详解(四) Self Host

改进Self Host

单线程阻塞当然不妥当,不过这样就基本实现了一个简易的Self Host。接下去,让我们把视野切换到版本1.4.1。

   private void StartListener()
{
if (this.TryStartListener())
{
return;
} if (!this.configuration.UrlReservations.CreateAutomatically)
{
throw new AutomaticUrlReservationCreationFailureException(this.GetPrefixes(), this.GetUser());
} if (!this.TryAddUrlReservations())
{
throw new InvalidOperationException("Unable to configure namespace reservation");
} if (!TryStartListener())
{
throw new InvalidOperationException("Unable to start listener");
}
}
}

同样是HttpListener,这里的启动更加的成熟,增加了不少错误处理的判断。

    public void Start()
{
this.StartListener(); try
{
this.listener.BeginGetContext(this.GotCallback, null);
}
catch (Exception e)
{
this.configuration.UnhandledExceptionCallback.Invoke(e); throw;
}
}

在启动完HttpListener后,这里最大的不同是使用异步回调的方式,不再是单线程阻塞模式。

private void GotCallback(IAsyncResult ar)
{
try
{
var ctx = this.listener.EndGetContext(ar);
this.listener.BeginGetContext(this.GotCallback, null);
this.Process(ctx);
}
catch (Exception e)
{
this.configuration.UnhandledExceptionCallback.Invoke(e); try
{
this.listener.BeginGetContext(this.GotCallback, null);
}
catch
{
this.configuration.UnhandledExceptionCallback.Invoke(e);
}
}
}

采用标准的回调处理,每个请求将会在它自己的独立线程中执行。

private void Process(HttpListenerContext ctx)
{
try
{
var nancyRequest = this.ConvertRequestToNancyRequest(ctx.Request);
using (var nancyContext = this.engine.HandleRequest(nancyRequest))
{
try
{
ConvertNancyResponseToResponse(nancyContext.Response, ctx.Response);
}
catch (Exception e)
{
this.configuration.UnhandledExceptionCallback.Invoke(e);
}
}
}
catch (Exception e)
{
this.configuration.UnhandledExceptionCallback.Invoke(e);
}
}

至此,self host相关内容结束。

当当当当 - つづく