首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Autofac和Nancy

Autofac和Nancy
EN

Stack Overflow用户
提问于 2017-03-01 00:00:09
回答 1查看 627关注 0票数 0

以下程序与Nancy一起使用Autofac无法正确启动默认的Nancy服务器。

代码语言:javascript
复制
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本身很好。

使用:

  • Autofac 4.3
  • 南希1.4.3
  • Nancy.Hosting.Self 1.4.1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-01 10:37:24

因为您的代码在Console.ReadLine();上“等待”,并且它在using之外,所以Autofac容器已经被释放。将Console.ReadLine();移动到using中以使其工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42520928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档