Skip to content

Registering Services

Ken Tucker edited this page Dec 6, 2024 · 1 revision

How to register your services

The ConfigureServices method is where we are registering the Objects for dependency injection

    private IServiceProvider Build()
    {
        if (_built)
            throw new InvalidOperationException("Build can only be called once.");
        _built = true;

        _defaultBuilder.ConfigureServices((context, services) =>
        {
            services.AddSingleton<IService, ServiceImpl>();
            services.AddTransient<ICar, Ford>();
            services.AddTransient<ICar, Toyota>();
            services.AddTransient<ICar, Chevy>();
            services.AddKeyedTransient<IPet, Dog>("Dog");
            services.AddKeyedTransient<IPet, Cat>("Cat");
        });

        _services = _defaultBuilder.Build().Services;
        CommonServiceLocator.ServiceLocator.SetLocatorProvider(() => new vb2ae.ServiceLocator.MSDependencyInjection.MSDependencyInjectionServiceLocator(_services));
        return _services;
    }

The line of code below registers vb2ae.ServiceLocator.MSDependencyInjection with the CommonServiceLocator

CommonServiceLocator.ServiceLocator.SetLocatorProvider(() => new vb2ae.ServiceLocator.MSDependencyInjection.MSDependencyInjectionServiceLocator(_services));
        return _services;

Clone this wiki locally