【问题标题】:Custom IServer implementation does not work with IISIntegration自定义 IServer 实现不适用于 IISIntegration
【发布时间】:2016-12-03 14:34:43
【问题描述】:

我有一个带有自定义 IServer 接口实现的示例 asp.net 核心应用程序,如下所示:

namespace AspNetCoreAppWithOwinHttpListenerServer
{
    public class Program
    {
        public static void Main(string[] args)
        {
            IWebHost host = new WebHostBuilder()
                .UseHttpListener()
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }

    public class OwinHttpListenerServer : IServer
    {
        private IDisposable _HttpListenerServer;

        public IFeatureCollection Features { get; } = new FeatureCollection();

        public OwinHttpListenerServer()
        {
            Features.Set<IServerAddressesFeature>(new ServerAddressesFeature());
        }

        public void Start<TContext>(IHttpApplication<TContext> application)
        {
            Func<IDictionary<string, object>, Task> appFunc = async env =>
            {
                FeatureCollection features = new FeatureCollection(new OwinFeatureCollection(env));

                TContext context = application.CreateContext(features);

                try
                {
                    await application.ProcessRequestAsync(context);
                }
                catch (Exception ex)
                {
                    application.DisposeContext(context, ex);
                    throw;
                }

                application.DisposeContext(context, null);

            };

            appFunc = OwinWebSocketAcceptAdapter.AdaptWebSockets(appFunc);

            Dictionary<string, object> props = new Dictionary<string, object>();

            props["host.Addresses"] = Features
                .Get<IServerAddressesFeature>()
                .Addresses
                .Select(add => new Uri(add))
                .Select(add => new Address(add.Scheme, add.Host, add.Port.ToString(), add.LocalPath).Dictionary)
                .ToList();

            OwinServerFactory.Initialize(props);

            _HttpListenerServer = OwinServerFactory.Create(appFunc, props);
        }

        public void Dispose()
        {
            _HttpListenerServer?.Dispose();
        }
    }

    public static class OwinHttpListenerWebHostBuilderExtensions
    {
        public static IWebHostBuilder UseHttpListener(this IWebHostBuilder builder)
        {
            return builder.ConfigureServices(services =>
            {
                services.AddSingleton<IServer, OwinHttpListenerServer>();
            });
        }
    }
}

在没有 IIS 的情况下也可以正常工作,但在 IIS 或 IISExpress 上运行会导致 VSIISExeLauncher.exe “未在给定端口上侦听”。

如何使我的自定义服务器与 IIS 兼容? 提前致谢。

GitHub 仓库:https://github.com/ymoradi/AspNetCoreAppWithOwinHttpListenerServer

【问题讨论】:

    标签: asp.net iis asp.net-core


    【解决方案1】:

    AspNetCoreModule (IIS) 与基于 Http.Sys 的服务器不兼容。见https://github.com/aspnet/AspNetCoreModule/issues/23

    【讨论】:

    • 是的,我知道。但我认为我的实施也有问题。我也不能使用“Nowin”。该服务器完全在 .NET 中实现,不依赖于 .NET / Windows Http Listener 或 http.sys
    • 我无法删除我的问题,我将使用 Nowin 实现打开一个新问题,并且我必须修复我的最后一条评论,Nowin 不依赖于 http.sys、.NET HttpListener本身是基于http.sys的。这是我不知道的事情。 (:谢谢
    【解决方案2】:

    OwinHttpListener 基于 .NET HttpListenerClass,该类是在 http.sys 的帮助下实现的。 http.sys 是内核模式代码,目前在 asp.net core iis 模块中不支持。

    【讨论】:

      猜你喜欢
      • 2014-07-29
      • 2023-03-16
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      相关资源
      最近更新 更多