1

I am using reverse poco to create entities from an sql database. The tt script I am using is from the on the plural website on the tutorial on EF. In a windows console app I am using Autofac to dependency injection of the the ddbcontext interface ISCDInnoPRDDataLayerDbContext:

public partial class SCDInnoPRDDataLayerDbContext : Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext, ISCDInnoPRDDataLayerDbContext

and of a class that will takes that interface as an input. public class DLScdHopDataNarRepository :

   DLScdBaseRepository<Read_Only_HopDataNar>,
            IDLScdHopDataNarRepository
        {
            public DLScdHopDataNarRepository(ISCDInnoPRDDataLayerDbContext dataLayerDbContext)
                : base(dataLayerDbContext)
            {
            }

Autofac is throwing an exception when I resolve the DLScdHopDataNarRepository class. The exception is on the constructor parameter to the SCDInnoPRDDataLayerDbContext class.

Exceptions: An exception was thrown while invoking the constructor 'Void .ctor(System.String)' on type 'SCDInnoPRDDataLayerDbContext'. at SCDInnoPRD.DataLayer.Context.SCDInnoPRDDataLayerDbContext..ctor(String connectionString) at lambda_method(Closure , Object[] ) at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() + InnerException {"The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception."}
System.Exception {System.TypeInitializationException}

Below is the autofac registration and the code that resolves the DLScdHopDataNarRepository .

Registration code:

public class SCDInnoPRDModule :Module
    {
        public string ConnectionString { get; private set; }

        public SCDInnoPRDModule(string connectionString)
        {
            ConnectionString = connectionString;
        }
        protected override void Load(ContainerBuilder builder)
        {

             builder.RegisterType<SCDInnoPRDDataLayerDbContext>()
                .As<ISCDInnoPRDDataLayerDbContext>()
                .Keyed<ISCDInnoPRDDataLayerDbContext>("scdb")
                .WithParameter((pi, ctx) => pi.ParameterType == typeof(string)
                                            && pi.Name == "connectionString",
                    (pi, ctx) => getConnectionString(ConnectionString))
                .InstancePerLifetimeScope();

            builder.RegisterType<DLScdHopDataNarRepository>()
            .As<IDLScdHopDataNarRepository>()
            .Keyed<IDLScdHopDataNarRepository>("sdhopres")
            .InstancePerLifetimeScope();

            builder.RegisterType<DLScdNarsService>()
         .As<IDLScdNarsService>()
         .Keyed<IDLScdNarsService>("sddlser")
         .InstancePerLifetimeScope();

        }
        private Func< string,string> getConnectionString = delegate (string connectionString)
        {

            string retVal = connectionString;

            return retVal;
        };

Resolve code:

using (var config= new ContainerConfig()) { // register dependency injectopn var container = config.Configure(); using (var scope = container.BeginLifetimeScope()) try { // exception occurs here var dlscd = container.ResolveNamed("sdhopres");

I have to a point where I can solve this problem. Any suggestion to fix the problem would be appreciated. Any example source code project that use autofac to do di of a dbcontext in a windows console application would be helpful. I can not find any such project on line. I can post the entire source code project on GitHub or a website for download if anyone would like to view the code

srf
  • 21
  • 1
  • 2
  • I think it's yelling about your web.config. see if anything here resolves the issue. https://stackoverflow.com/questions/17585611/the-type-initializer-for-system-data-entity-internal-appconfig-threw-an-except – Tyddlywink Jul 19 '19 at 12:32
  • You can omit this question. Problem was with Autofac at all. The problem was in the app.config file. I had the connectionstrings section before the configsection – srf Jul 19 '19 at 12:53

0 Answers0