How to integrate this with OwinStartup?
cosmoKenney opened this issue · comments
I've got my old web forms project setup to now include MVC. And I have automatic startup via OwinStartup:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Owin;
using Owin;
using System.Web.Mvc;
[assembly: OwinStartup( typeof( ProvPubs.Compline.Web.Startup ) )]
namespace ProvPubs.Compline.Web
{
public partial class Startup
{
public void Configuration( IAppBuilder app )
{
var services = new ServiceCollection( );
ConfigureServices( services );
var resolver = new DefaultDependencyResolver( services.BuildServiceProvider( ) );
DependencyResolver.SetResolver( resolver );
ConfigureAuth( app );
}
}
}
My services are registered in a partial (Startup) class.
But since my startup is automatically called by the framework, how would I incorporate your project such that I can use constructor di in aspx pages?
@cosmoKenney You'll want to use WebActivatorEx
and perform service configuration there instead of in OWIN startup, as per the examples in the documentation.
This is because WebActivatorEx.PreStart
runs before OwinStartup
: https://stackoverflow.com/questions/21462777/webactivatorex-vs-owinstartup
The ASP.NET MVC functionality in AspNetDependencyInjection.Mvc
(which implements IDependencyResolver
) doesn't work right now, but I've got a fix in the works I'll publish shortly - in a few hours.
So your code would look like this:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Owin;
using Owin;
using System.Web.Mvc;
using AspNetDependencyInjection;
[assembly: PreApplicationStartMethod ( typeof( global::ProvPubs.Compline.Web.Startup ), methodName: nameof( global::ProvPubs.Compline.Web.Startup.PreStart ) )]
[assembly: OwinStartup( typeof( ProvPubs.Compline.Web.Startup ) )]
namespace ProvPubs.Compline.Web
{
public partial class Startup
{
private static MvcApplicationDependencyInjection _di;
public static void PreStart()
{
_di = MvcApplicationDependencyInjection.ConfigureMvc( ConfigureServices );
}
private static void ConfigureServices( IServiceCollection services )
{
// TODO: Add any dependencies needed here
services
// Useful services built-in to AspNetDependencyInjection:
.AddDefaultHttpContextAccessor() // Adds `IHttpContextAccessor`
.AddWebConfiguration() // Adds `IWebConfiguration`
;
}
public void Configuration( IAppBuilder app )
{
ConfigureAuth( app );
}
}
}
@cosmoKenney Did my reply help? May I close this issue now?
Yes I believe so.