seesharper / LightInject.Annotation

LightInject.Annotation supports annotation of properties and constructor parameters through an extension LightInject.
3 stars 1 forks source link

.net core #3

Open danielmedina opened 7 years ago

danielmedina commented 7 years ago

I'm trying to use it with .net core, but isn't working.

First changed ConfigureServices in Startup.cs like LightInject.Microsoft.DependencyInjection readme:

 public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            var mongoClientCatalog = new MongoClient(Configuration.GetConnectionString("MongoDBConnectionCatalog"));
            IMongoDatabase mongoDatabaseCatalog = mongoClientCatalog.GetDatabase(Configuration.GetConnectionString("MongoDatabaseCatalog"));

            var mongoClientCheckout = new MongoClient(Configuration.GetConnectionString("MongoDBConnectionCheckout"));
            IMongoDatabase mongoDatabaseCheckout = mongoClientCheckout.GetDatabase(Configuration.GetConnectionString("MongoDatabaseCheckout"));

            var container = new ServiceContainer();
            container.EnableAnnotatedPropertyInjection();

            container.RegisterInstance<IMongoDatabase>(mongoDatabaseCatalog, "catalog");
            container.RegisterInstance<IMongoDatabase>(mongoDatabaseCheckout, "checkout");

            container.Register<IEventRepository, EventRepository>(new PerRequestLifeTime());
            container.Register<ITheatreRepository, TheatreRepository>(new PerRequestLifeTime());
            container.Register<ICityRepository, CityRepository>(new PerRequestLifeTime());
            container.Register<ISessionRepository, SessionRepository>(new PerRequestLifeTime());
            container.Register<ISessionService, SessionService>(new PerRequestLifeTime());

            return container.CreateServiceProvider(services);
        }

At the last line of this block, everything works fine. My named services too.

In my application, I'm trying to use my named service like this:

        [Inject("catalog")]
        public IMongoDatabase Database { get; set; }

But it don't work. With others dependecies, like all my others repositories, don't work too. Like this:

 [Inject]
        public ITheatreRepository TheatreRepository { set; get; }

I'm trying to use LightInject.Microsoft.DependencyInjection with my .net core api, but i can't find anything to help me. I don't know if my problem is in LightInject.Microsoft.DependencyInjection or in LightInject.Annotation.

seesharper commented 7 years ago

Try replacing

 services.AddMvc();

with

services.AddMvc().AddControllersAsServices();