以下程序与Nancy一起使用Autofac无法正确启动默认的Nancy服务器。
using Autofac;
using Nancy.Hosting.Self;
using System;
namespace NancyExample
{
class Program
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.Register(c => new NancyHost(new Uri("http://localhost:8080"))).SingleInstance();
using (var container = builder.Build())
{
NancyHost host = container.Resolve<NancyHost>();
// this fails with:
// Exception thrown: 'System.Net.HttpListenerException' in System.dll
// Exception thrown: 'System.Net.HttpListenerException' in System.dll
// Exception thrown: 'System.InvalidOperationException' in System.dll
// Exception thrown: 'System.InvalidOperationException' in System.dll
// this works:
// NancyHost host = new NancyHost(new Uri("http://localhost:8080"));
host.Start();
}
Console.ReadLine();
}
}
}当通过Autofac解析NancyHost时,似乎在.NET的HttpListener中出现了一个很深的错误。关于这个例外,似乎没有很好的细节。访问http://localhost:8080会导致没有连接。
实例化NancyHost本身很好。
使用:
发布于 2017-03-01 10:37:24
因为您的代码在Console.ReadLine();上“等待”,并且它在using之外,所以Autofac容器已经被释放。将Console.ReadLine();移动到using中以使其工作。
https://stackoverflow.com/questions/42520928
复制相似问题