问题描述
我有一个库,我想在 .NET Core 支持的所有平台(Xamarin、Windows、Mac)上运行它.为此,我需要一个跨平台 DI 来处理特定于平台的定制,例如存储、安全性等.
我看到的所有 Microsoft.Extensions.DependencyInjection
示例都包含 ASP.NET Core 的代码.控制台应用是否存在任何等效/支持的场景?
或者(理想情况下)我希望用于控制台的 DI 框架适用于 ASP.NET Core、Xamarin 和 Win32
错误示例
我使用
我基本上是这样使用的:
安装nuget Microsoft.Extensions.DependencyInjection
然后在 Program.cs 中
使用 Microsoft.Extensions.DependencyInjection;公开课计划{public static void Main(string[] args){var services = new ServiceCollection().AddSingleton(newWhateverType());var serviceProvider = services.BuildServiceProvider();serviceProvider.GetService().DoWork();}}
I have a library that I would like to run on all platforms supported by .NET Core (Xamarin, Windows, Mac). And to do this I need to have a cross platform DI to handle the platform specific customizations such as storage, security, etc.
All of the examples I've seen for Microsoft.Extensions.DependencyInjection
include code for ASP.NET Core. Does any equivalent / supported scenario exist for a console app?
Alternatively (ideally) I would like the DI framework used for the console to work for ASP.NET Core, Xamarin, and Win32
Error Example
I'm using this code as the basis for my .NET Core application. It appears that ServiceCollection should be public, but when I use Xamarin on the Mac the default implementation of IServiceCollection
(ServiceCollection) is missing:
I use it basically like so:
Install nuget Microsoft.Extensions.DependencyInjection
Then in Program.cs
using Microsoft.Extensions.DependencyInjection;
public class Program
{
public static void Main(string[] args)
{
var services = new ServiceCollection()
.AddSingleton<WhateverType>(new WhateverType());
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetService<WhateverType>().DoWork();
}
}
这篇关于如何在 .NET Core 控制台应用程序中使用 Microsoft.Extensions.DependencyInjection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!