daiplusplus / AspNetDependencyInjection

`AspNetDependencyInjection` allows "Classic" ASP.NET Web Forms applications to use `Microsoft.Extensions.DependencyInjection` to compose application services using dependency injection.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'Diagnostics' (aspx page) does not contain a constructor that takes 0 arguments

mac-bhaird opened this issue · comments

commented

Hi,

We're solving for the same problem as you (old webforms application to move towards .net core)!

We've followed the guide and am receiving the error:

App_Web_diagnostics.aspx.cdcab7d2.ouhviywy.0.cs(156): error CS1729: 'App2.Diagnostics' (aspx page) does not contain a constructor that takes 0 arguments.

We've also followed the troubleshooting steps including wiping out the temp asp.net files, bin folder, obj folder, etc.

Here's my code

Startup:

using AspNetDependencyInjection;
using App2.Web;
using Microsoft.Extensions.DependencyInjection;
using WebActivatorEx;

[assembly: PreApplicationStartMethod(typeof(DependencyInjectionStartup), nameof(DependencyInjectionStartup.PreStart))]
[assembly: PostApplicationStartMethod(typeof(DependencyInjectionStartup), methodName: nameof(DependencyInjectionStartup.PostStart))]
[assembly: ApplicationShutdownMethod(typeof(DependencyInjectionStartup), methodName: nameof(DependencyInjectionStartup.ApplicationShutdown))]
namespace App2.Web
{
    public class DependencyInjectionStartup
    {
        private static ApplicationDependencyInjection ApplicationDependencyInjection;
       
        internal static void PreStart()
        {
            System.Diagnostics.Debug.WriteLine(nameof(DependencyInjectionStartup) + "." + nameof(PreStart) + "() called.");

            ApplicationDependencyInjection = new ApplicationDependencyInjectionBuilder()
                .ConfigureServices(ConfigureServices)
                .Build();
        }

        private static void ConfigureServices(IServiceCollection services)
        {
            // TODO: Add any dependencies needed here
            services
                .AddDefaultHttpContextAccessor()
                .AddWebConfiguration();

            services.AddScopedWithFactory<MyDbContext, MyDbContextFactory>();
   

        }

        internal static void PostStart()
        {
            System.Diagnostics.Debug.WriteLine(nameof(DependencyInjectionStartup) + "." + nameof(PostStart) + "() called.");
        }

        internal static void ApplicationShutdown()
        {
            System.Diagnostics.Debug.WriteLine(nameof(DependencyInjectionStartup) + "." + nameof(ApplicationShutdown) + "() called.");
        }
    }
}

Factory:

using System;
using AspNetDependencyInjection;
using Microsoft.EntityFrameworkCore;

namespace App2.Web
{
    public class MyDbContextFactory : IServiceFactory<MyDbContext>
    {
        private readonly string _connectionString;

        public MyDbContextFactory(IWebConfiguration webConfig)
        {
            if (webConfig == null)
            {
                throw new ArgumentNullException(nameof(webConfig));
            }

            this._connectionString = webConfig.RequireConnectionString("mainConnectionString"); // `RequireConnectionString` is an extension method.
        }

        public MyDbContext CreateInstance()
        {
            var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
            optionsBuilder.UseSqlServer(_connectionString);

            return new MyDbContext(optionsBuilder.Options);
        }
    }
}

Diagnostics (aspx):

      public Diagnostics(MyDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            var employees = _dbContext.Employees.ToList();
}

We're probably missing something simple, thoughts?

commented

Are you seeing the error only in Visual Studio's Error List window when you have an .aspx file open? If so, then it's actually a completely benign and ignorable bug in Visual Studio's WebForms codebase (confirmed in VS2015 and VS2017 - I haven't tried it in VS2019 yet).

You can verify this by checking that you're able to build your *.csproj project (either in VS or using MSBuild) and that aspnet_compiler.exe runs successfully then you're all-set and you can ignore the message.

If you're seeing that same error elsewhere then that's a problem that I'd like to address!

commented

BTW, in your MyDbContextFactory, you can probably move the .UseSqlServer call into the MyDbContextFactory constructor and only keep the DbContextOptionsBuilder<DbContextOptionsBuilder> as a readonly field.

commented

You're right, it was the message in the error panel when the aspx file is open!

Thanks!