/// <summary> /// Initializes a new instance. /// </summary> /// <param name="next"></param> /// <param name="serviceType"></param> /// <param name="bindings"></param> /// <param name="router"></param> /// <param name="applicationLifetime"></param> /// <param name="logger"></param> /// <param name="serviceProvider"></param> protected AspNetCoreServiceHostMiddleware( RequestDelegate next, Type serviceType, AspNetCoreBindingFactory bindings, AspNetCoreRequestRouter router, MessageVersion messageVersion, IApplicationLifetime applicationLifetime, ILogger <AspNetCoreServiceHostMiddleware> logger, IServiceProvider serviceProvider) : this(next, bindings, router, messageVersion, applicationLifetime, logger, serviceProvider) { host = new AspNetCoreServiceHost(serviceType, AddDefaultServiceEndpoint, httpBaseUri, httpsBaseUri); host.AddDependencyInjectionBehavior(serviceType, serviceProvider); host.Description.Behaviors.Add(new TextOrMtomEncoderInspector()); host.Description.Behaviors.Add(new AspNetCoreRequestRouterBehavior(router)); host.Description.Behaviors.Add(new AspNetCoreServiceMetadataBehavior()); var sdb = host.Description.Behaviors.Find <ServiceDebugBehavior>(); if (sdb == null) { host.Description.Behaviors.Add(sdb = new ServiceDebugBehavior()); } var httpGetBinding = bindings.CreateBinding(serviceProvider, MessageVersion.None, false, HttpMethods.Get); var httpsGetBinding = bindings.CreateBinding(serviceProvider, MessageVersion.None, true, HttpMethods.Get); sdb.HttpHelpPageEnabled = true; sdb.HttpHelpPageBinding = httpGetBinding; sdb.HttpHelpPageUrl = new Uri("", UriKind.Relative); sdb.HttpsHelpPageEnabled = true; sdb.HttpsHelpPageBinding = httpsGetBinding; sdb.HttpsHelpPageUrl = new Uri("", UriKind.Relative); var smb = host.Description.Behaviors.Find <ServiceMetadataBehavior>(); if (smb == null) { host.Description.Behaviors.Add(smb = new ServiceMetadataBehavior()); } smb.HttpGetEnabled = true; smb.HttpGetBinding = httpGetBinding; smb.HttpsGetEnabled = true; smb.HttpsGetBinding = httpsGetBinding; }
/// <summary> /// Initializes a new instance. /// </summary> /// <param name="next"></param> /// <param name="bindings"></param> /// <param name="router"></param> /// <param name="messageVersion"></param> /// <param name="logger"></param> /// <param name="applicationLifetime"></param> AspNetCoreServiceHostMiddleware( RequestDelegate next, AspNetCoreBindingFactory bindings, AspNetCoreRequestRouter router, MessageVersion messageVersion, IApplicationLifetime applicationLifetime, ILogger <AspNetCoreServiceHostMiddleware> logger, IServiceProvider serviceProvider) { this.next = next; this.bindings = bindings ?? throw new ArgumentNullException(nameof(bindings)); this.router = router ?? throw new ArgumentNullException(nameof(router)); // bindings for registered services httpBinding = bindings.CreateBinding(serviceProvider, messageVersion, false); httpsBinding = bindings.CreateBinding(serviceProvider, messageVersion, true); // identifies this registered middleware route routeId = Guid.NewGuid(); // listen on multiple URIs httpBaseUri = new Uri($"http://{routeId:N}/"); httpsBaseUri = new Uri($"https://{routeId:N}/"); // register for cleanup when application is stopped applicationLifetime.ApplicationStopping.Register(() => { try { host.Close(); } catch (Exception e) { logger.LogError(e, "Exception closing ServiceHost."); } }); }