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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With