Last active
December 31, 2020 11:18
-
-
Save oliverhanappi/3720641004576c90407eb3803490d1ce to your computer and use it in GitHub Desktop.
Running ASP.NET Core within IIS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AspNetCoreOwinMiddleware<TAspNetCoreStartup> : OwinMiddleware, IServer | |
where TAspNetCoreStartup : class | |
{ | |
private readonly IWebHost _webHost; | |
private Func<IOwinContext, Task> _appFunc; | |
IFeatureCollection IServer.Features { get; } = new FeatureCollection(); | |
public AspNetCoreOwinMiddleware(OwinMiddleware next, IAppBuilder app) | |
: base(next) | |
{ | |
var appProperties = new AppProperties(app.Properties); | |
if (appProperties.OnAppDisposing != default(CancellationToken)) | |
appProperties.OnAppDisposing.Register(Dispose); | |
_webHost = new WebHostBuilder() | |
.ConfigureServices(s => s.AddSingleton<IServer>(this)) | |
.UseStartup<TAspNetCoreStartup>() | |
.Build(); | |
_webHost.Start(); | |
} | |
void IServer.Start<TContext>(IHttpApplication<TContext> application) | |
{ | |
_appFunc = async owinContext => | |
{ | |
var features = new FeatureCollection(new OwinFeatureCollection(owinContext.Environment)); | |
var context = application.CreateContext(features); | |
try | |
{ | |
await application.ProcessRequestAsync(context); | |
application.DisposeContext(context, null); | |
} | |
catch (Exception ex) | |
{ | |
application.DisposeContext(context, ex); | |
throw; | |
} | |
}; | |
} | |
public override Task Invoke(IOwinContext context) | |
{ | |
if (_appFunc == null) | |
throw new InvalidOperationException("ASP.NET Core Web Host not started."); | |
return _appFunc(context); | |
} | |
public void Dispose() | |
{ | |
_webHost.Dispose(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AspNetCoreStartup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
} | |
public void Configure(IApplicationBuilder app) | |
{ | |
app.UseMvc(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: OwinStartup(typeof(OwinStartup))] | |
public class OwinStartup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
// Paths in ASP.NET Core will not start with /api but with / | |
app.Map("/api", subApp => subApp.Use<AspNetCoreOwinMiddleware<AspNetCoreStartup>>(app)); | |
} | |
} |
Do you have any full fledged working copy of this implementation
I tried something similar and had a problem with content negotiation and getting 406 status codes. Thought I would share my fix for this as it appears it is missing.
// enumerating the env owin.ResponseHeaders causes Content-Type to default to text/html which messes up content negotiation
// set it to empty so AspNetCore can decide the final Content-Type
features.Get<IHttpResponseFeature>().Headers["Content-Type"] = "";
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Required NuGet Packages: