简体   繁体   English

如何在ASP.NET Web API项目而不是.NET Core上使用DI

[英]How do I use DI on asp.net web api project not .net core

The following documentation shows how to configure cosmonaut for a .net core project. 以下文档显示了如何为.net核心项目配置cosmonaut。

https://github.com/Elfocrash/Cosmonaut https://github.com/Elfocrash/Cosmonaut

Registering the CosmosStores in ServiceCollection for DI support

 var cosmosSettings = new CosmosStoreSettings("<<databaseName>>", "<<cosmosUri>>", "<<authkey>>");

serviceCollection.AddCosmosStore<Book>(cosmosSettings);

//or just by using the Action extension

serviceCollection.AddCosmosStore<Book>("<<databaseName>>", "<<cosmosUri>>", "<<authkey>>", settings =>
{
    settings.ConnectionPolicy = connectionPolicy;
    settings.DefaultCollectionThroughput = 5000;
    settings.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.Number, -1),
        new RangeIndex(DataType.String, -1));
});

How do I do for an old webpi project? 我如何处理旧的webpi项目?

Web Api 2 doesn't come with out of box dependency injection, you can use 3rd party dependency injection packages like Autofac and Ninject etc, or you can create Cosmonaut's singleton class for use too if you don't want to use dependency injection at all. Web Api 2并没有开箱即用的依赖项注入,您可以使用Autofac和Ninject等第三方依赖项注入包,或者如果您根本不想使用依赖项注入,也可以创建Cosmonaut的singleton类以供使用。 。

Note: As per their docs , Cosmonaut instance should be used as singleton instances per entity. 注意:根据他们的文档 ,Cosmonaut实例应用作每个实体的单例实例。

UPDATE 更新

Implementation of a generic Singleton class where T is the type of entity you are asking for the instance of, 通用Singleton类的实现,其中T是您要查询其实例的实体的类型,

public sealed class CosmosStoreSingleton<T>
{
    private static ICosmosStore<T> instance = null;

    public static ICosmosStore<T> Instance
    {
        get
        {
            if (instance==null)
            {
                var cosmosSettings = new CosmosStoreSettings("<<databaseName>>", "<<cosmosUri>>", "<<authkey>>");
                instance = new CosmosStore<T>(cosmosSettings);
            }

            return instance;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何验证 ASP.NET Core 中的 DI 容器? - How do I validate the DI container in ASP.NET Core? 如何设置 ASP.Net 3.0 Core Web API 项目以使用 AutoFac 和 NLog? - How do you setup an ASP.Net 3.0 Core Web API project to use AutoFac and NLog? 如何在 ASP.NET Core Web API 项目中设置 cookie - How to set a cookie in a ASP.NET Core Web API project 如何在asp.net核心中间件中做DI? - How to do DI in asp.net core middleware? 如何在 ASP.NET Core 6 中为 IConfiguration 实现 DI? - How do you implement DI for IConfiguration in ASP.NET Core 6? 如何在ASP.NET Core动态Web API中使用HttpGet? - How to use HttpGet in ASP.NET Core dynamic web api? 如何在单独的项目中使用测试设置ASP.NET API以使用一个IoC / DI容器 - How to set ASP.NET API with test in separate project to use one IoC/DI container 如何在 ASP.NET Core 中排除项目依赖项? - How do I exclude project dependencies in ASP.NET Core? 如何在我上传文件的Asp.Net核心web api端点上进行集成测试? - How to do an integration test on my Asp.Net core web api endpoint where I upload a file? 如何构建一个 ASP.Net 核心 web api Enpoint 处理带有等号“=”符号的查询参数 - How do I build an ASP.Net core web api Enpoint that handles query parameters with an equal '=' sign
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM