Q:

How to implement dependency injection in asp.net web api 2

I wanted to use this nuget package in my .net web api project:
https://github.com/Elfocrash/Cosmonaut/tree/885a98151be242516ec57cd94da1747b72588788
The idea was to consume that nuget package from a  .net web api in 4.6.1 however this package targets netstandard 2. So I couldnt install the nuget package.
At the end, I downloaded the source code and created a new class library in .net 4.6.1 and it compiles perfect.
Then I added a reference from .net web api to this library.
However I am having issues with the serviceCollection, as this is not there. 
How do I use it serviceCollection?
// [Authorize]
    public class TenantController : ApiController
    {
        private  CosmosStoreSettings cosmosSettings;

        public TenantController()
        {

            cosmosSettings = new CosmosStoreSettings(ConfigurationManager.AppSettings["database"].ToString(), 
                ConfigurationManager.AppSettings["endpoint"].ToString(), 
                ConfigurationManager.AppSettings["authKey"].ToString());

            serviceCollection.AddCosmosStore<Book>(cosmosSettings);

        }

The name 'serviceCollection' does not exist in the current context

I know exactly what it means, but I am not sure how to implement DI on this project 

A:

The library you are using uses the Microsoft.Extensions.DependencyInjection NuGet package, which contains the Dependency Injection framework that ASP.NET Core uses. While it is not dependent on ASP.NET Core (not even on .NET Core), given that you are using ASP.NET Web API 2, I would suggest you to keep it simple and just create a factory around ComosStore<T>:
public static class CosmosStoreFactory 
{
    private static CosmosStoreSettings _settings = new CosmosStoreSettings("<<databaseName>>", "<<cosmosUri>>", "<<authkey>>");

    public static ICosmosStore<T> CreateForEntity<T>() where T: class
    {
         return new CosmosStore<T>(_settings);
    }
}

