Why ASP.NET Core DI knows how to resolve ILogger<T>, but not ILogger?
Date : March 29 2020, 07:55 AM
should help you out If class T contains dependency on ILogger, dependency is resolved: , Logging adds the following services to DI services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>());
services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>)));
public Foo(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger("logger name here");
}
|
asp.net core Trouble adding a startup extension to startup.cs
Tag : chash , By : Grace Jones
Date : March 29 2020, 07:55 AM
To fix the issue you can do You are not using the keyword this in your extension method which doesn't make it an extension method. If you do it like this inside a static class (mandantory as well) it works just fine public static void EnsureMigrationsAndInitialisationRun(this IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var dbContextFactory = serviceScope.ServiceProvider.GetService<IDbContextFactory>();
var allTenantConnectionStrings = serviceScope.ServiceProvider.GetService<ISessionServices>().AllTenantConnectionStrings();
foreach (var tenantConnectionString in allTenantConnectionStrings)
{
var context = dbContextFactory.CreateDbContext(tenantConnectionString);
context.Database.Migrate();
// Add in the initializations here.
}
}
}
app.EnsureMigrationsAndInitialisationRun();
|
How and Who calling the ConfigureServices and Configure method of startup class in .net core
Tag : chash , By : Ohad Barzilay
Date : March 29 2020, 07:55 AM
To fix the issue you can do As everyone know that Main method of Program.cs is the entry point of application. As you can see in the .net core default code created when we create any project. , As an overly simplified explanation, WebHost.CreateDefaultBuilder(args)
|
Azure function 2.x ILogger is not competible with .net core ILogger?
Tag : chash , By : Techspirit
Date : March 29 2020, 07:55 AM
this one helps. I have been trying to refer a .net core library project into my Azure function project to call one of the process defined in a .net core class library. , Update: Have use .net core 3.0 now. <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="librdkafka.redist" Version="1.2.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.28" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="librdkafka.redist" Version="1.2.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
|
Accessing ILogger from non-Controller classes in Class Libary .NET Core 3
Tag : chash , By : AnToni00
Date : September 12 2020, 07:00 AM
Hope this helps Instead of instantiating a depended class inside your controller, delegate this responsibility to .NET Core inbuilt DI container - This will help you to inject the depended class directly to the required class. Below code snippet will help you how to use DI in your existing codebase. public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging();
services.AddTransient<ClassX>();
services.AddTransient<ClassY>();
services.AddTransient<ClassZ>();
services.AddTransient<ClassD>();
...
}
}
public class ControllerB : ControllerBase
{
private readonly ClassD classD;
private readonly ILogger logger;
public ControllerB(ClassD classD, ILogger<ControllerB> logger)
{
this.classD = classD;
this.logger = logger;
}
...
}
public class ClassD
{
private readonly ClassZ classZ;
public ClassD(ClassZ classZ)
{
this.classZ = classZ;
}
} //Do the same thing for ClassZ and ClassY
public class ClassX
{
private readonly ILogger logger;
public ClassX(ILogger<ClassX> logger)
{
this.logger = logger;
}
}
|