我想在我的 .net web api 项目中使用这个 nuget 包:https : //github.com/Elfocrash/Cosmonaut/tree/885a98151be242516ec57cd94da1747b72588788
这个想法是从 4.6.1 中的 .net web api 使用那个 nuget 包,但是这个包针对 netstandard 2。所以我无法安装 nuget 包。
最后,我下载了源代码并在.net 4.6.1 中创建了一个新的类库,它编译完美。
然后我添加了一个从 .net web api 到这个库的引用。
但是我遇到了 serviceCollection 的问题,因为它不存在。我如何使用它 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);
}
当前上下文中不存在名称“serviceCollection”
我确切地知道这意味着什么,但我不确定如何在这个项目上实施 DI
您使用的库使用Microsoft.Extensions.DependencyInjection
NuGet 包,其中包含 ASP.NET Core 使用的依赖注入框架。虽然它不依赖于 ASP.NET Core(甚至不依赖于 .NET Core),但鉴于您使用的是 ASP.NET Web API 2,我建议您保持简单并围绕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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句