مهارتهای تزریق وابستگیها در برنامههای NET Core. - قسمت اول - تزریق وابستگیها در برنامههای کنسول
نویسنده: وحید نصیری
تاریخ: ۱۳۹۷/۱۰/۰۸ ۱۲:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using Microsoft.Extensions.Logging;
namespace CoreIocServices
{
public interface ITestService
{
void Run();
}
public class TestService : ITestService
{
private readonly ILogger<TestService> _logger;
public TestService(ILogger<TestService> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public void Run()
{
_logger.LogWarning("A Warning from the TestService!");
}
}
} <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
</ItemGroup>
</Project> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CoreIocServices\CoreIocServices.csproj" />
</ItemGroup>
</Project> using CoreIocServices;
using Microsoft.Extensions.DependencyInjection;
namespace CoreIocSample01
{
class Program
{
static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();
var testService = serviceProvider.GetService<ITestService>();
testService.Run();
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ITestService, TestService>();
}
}
} Exception has occurred: CLR/System.InvalidOperationException An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Extensions.DependencyInjection.dll: 'Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger`1[CoreIocServices.TestService]' while attempting to activate 'CoreIocServices.TestService'.'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CoreIocServices\CoreIocServices.csproj" />
</ItemGroup>
</Project> private static void ConfigureServices(IServiceCollection services)
{
services.AddLogging(configure => configure.AddConsole().AddDebug());
services.AddTransient<ITestService, TestService>();
} CoreIocServices.TestService:Warning: A Warning from the TestService!
namespace Test
{
public static class ConfigureServices
{
private static readonly Lazy<IServiceProvider> _serviceProviderBuilder =
new Lazy<IServiceProvider>(getServiceProvider, LazyThreadSafetyMode.ExecutionAndPublication);
/// <summary>
/// A lazy loaded thread-safe singleton
/// </summary>
public static IServiceProvider Instance { get; } = _serviceProviderBuilder.Value;
private static IServiceProvider getServiceProvider()
{
var services = new ServiceCollection();
// TODO: add other services here ... services.AddSingleton ....
return services.BuildServiceProvider();
}
}
}