none
SimpleInjector.DiagnosticVerificationException RRS feed

  • Question

  • First of all sorry for so much code but i belive it will be easier to find an issue here. I am using SimpleInjector and getting error inside Program class on container.Verify(); line, it says as below. I was trying to investigate throug docs website but still cannot figoure out how to fix that.

    SimpleInjector.DiagnosticVerificationException: 'The configuration is invalid. The following diagnostic warnings were reported: -[Disposable Transient Component] FrmLogin is registered as transient, but implements IDisposable. See the Error property for detailed information about the warnings. Please see https://simpleinjector.org/diagnostics how to fix problems and how to suppress individual warnings.'

    Full code:

    static class Program
        {
            private static Container container;
    
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            private static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Bootstrap();
                Application.Run(container.GetInstance<FrmLogin>());
            }
    
            private static void Bootstrap()
            {
                // Create the container as usual.
                container = new Container();   
                container.Register<IRepositoryDal<User>, UserRepositoryDal>();
                container.Register<IRepositoryDal<Order>, OrderRepositoryDal>();
                container.Register<IDbManager>(() => new DbManager("sqlserver"));
                container.Register<IGenericBal<User>, UserBal>();
                container.Register<IGenericBal<Order>, OrderBal>();
                container.Register<FrmLogin>();
                container.Verify();
            }
        }
    
        public partial class FrmLogin : Form
        {
            private readonly IGenericBal<User> _userBal;
            private readonly IGenericBal<Order> _orderBal;
    
            public FrmLogin(IGenericBal<User> userBal, IGenericBal<Order> orderBal)
            {
                InitializeComponent();
    
                _userBal = userBal;
                _orderBal = orderBal;
    }
    }
    
        public interface IGenericBal<out T> where T : IEntity
        {
            IEnumerable<T> SearchByName(string name);
        }
    
        public class UserBal : IGenericBal<User>
        {
            private readonly IRepositoryDal<User> _userRepositoryDal;
    
            public UserBal(IRepositoryDal<User> userRepositoryDal)
            {
                _userRepositoryDal = userRepositoryDal ?? throw new ArgumentNullException(nameof(userRepositoryDal));
            }
    
            public IEnumerable<User> SearchByName(string name)
            {
                   return _userRepositoryDal.SearchByName(name);
            }
        }
    
        public interface IEntity
        {
            int Id { get; set; }
            int DoSomething(string one, int two);
        }
    
        public interface IRepositoryDal<T> where T : IEntity
        {
            IEnumerable<T> SearchByName(string username);
            T SearchById(string id);
            void Update(T entity);
            void Remove(T entity);
            void Add(T entity);
        }
    
        public class UserRepositoryDal: IRepositoryDal<User>
        {
            private readonly IDbManager _dbManager;
    
            public UserRepositoryDal(IDbManager dbManager)
            {
                //read from either singleton or configuration file !!
                _dbManager = dbManager;
            }
            public IEnumerable<User> SearchByName(string username)
            {
    
    
                    var parameters = new List<IDbDataParameter>
                    {
                        _dbManager.CreateParameter("@Name", 50, username, DbType.String),
                    };
    
                username = "JUSTyou";
                var userDataTable = _dbManager.GetDataTable("SELECT * FROM T_Marke WHERE Name=@Name", CommandType.Text, parameters.ToArray());
    
                foreach (DataRow dr in userDataTable.Rows)
                {
                    var user = new User
                    {
                        Id = int.Parse(dr["Id"].ToString()),
                        Firstname = dr["Name"].ToString(),
                    };
    
                    yield return user;
                }
    
            }
            public User SearchById(string id)
            {
                var parameters = new List<IDbDataParameter>
                {
                    _dbManager.CreateParameter("@Id", 50, id, DbType.Int32),
                };
    
                var userDataTable = _dbManager.GetDataTable("storedpr2", CommandType.StoredProcedure, parameters.ToArray());
    
                return new User
                {
                    Id = int.Parse(userDataTable.Rows[0]["Id"].ToString()),
                    Firstname = userDataTable.Rows[0]["Firstname"].ToString(),
                    Lastname = userDataTable.Rows[0]["LastName"].ToString(),
                    Email = userDataTable.Rows[0]["Email"].ToString()
                };
            }
    
            public void Update(User entity)
            {
                throw new System.NotImplementedException();
            }
    
            public void Remove(User entity)
            {
                throw new System.NotImplementedException();
            }
    
            public void Add(User entity)
            {
                throw new System.NotImplementedException();
            }
        }
    
        public class User : IEntity
        {
            public int Id { get; set; }
            public int DoSomething(string one, int two)
            {
                throw new NotImplementedException();
            }
    
            public string Firstname { get; set; }
            public string Lastname { get; set; }
            public string Email { get; set; }
            public string Password { get; set; }
        }

     I tried to also make like this:

    var container = new Container();
    // Select the scoped lifestyle that is appropriate for the application
    // you are building. For instance:
    container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
    
    // DisposableService implements IDisposable
    container.Register<FrmLogin>(Lifestyle.Scoped);

    but then i get following error:

    "SimpleInjector.DiagnosticVerificationException:
     'The configuration is invalid. The following diagnostic warnings were 
    reported: -[Lifestyle Mismatch] FrmLogin (Async Scoped) depends on 
    IGenericBal<User> implemented by UserBal (Transient). -[Lifestyle 
    Mismatch] FrmLogin (Async Scoped) depends on IGenericBal<Order> 
    implemented by OrderBal (Transient). See the Error property for detailed
     information about the warnings. Please see simpleinjector.org/diagnostics how to fix problems and how to suppress individual warnings.' "

    • Moved by CoolDadTx Friday, June 15, 2018 1:48 PM Third party product
    Friday, June 15, 2018 8:14 AM

All replies