❔ .net core 6 IConfiguration Injection

Hi there - i'm going nuts try to solve this simple problem. I have a .net Core 6 WebApplication builder based app, and all i'm trying to do is get to IConfiguration in my service classes. From what i've read, .net 6 should automatically add its IConfiguration service to the build services, and therefore my AddService<MyClass> constructor whcih has public MyClass(IConfiguration configuration) in it throws "The service implementation object was not initialized or is not available." and it's not hitting breakpoints. Remove IConfiguration from teh constructor, it works it's like either a) IConfiguration is not injected into the services for the app builder, or b) MyClass is not subject to being DI'd by asp.net. argh
18 Replies
superbobface
superbobfaceOP2y ago
if I inspect app just before calling Run() "{[{Name = IConfiguration FullName = Microsoft.Extensions.Configuration.IConfiguration}, {Method = {System.Object <CreateServiceAccessor>b__1(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope)}}]}" this is in the realizedServics list
phaseshift
phaseshift2y ago
Did you somehow use a different namespace for IConfig param in MyClass?
superbobface
superbobfaceOP2y ago
well i dont think so let me double check Microsoft.Extensions.Configuration.IConfiguration thats what VS thinks it is hovering over the parameter to the service class constructor
phaseshift
phaseshift2y ago
That's weird. Do you have a Startup class?
superbobface
superbobfaceOP2y ago
no, just Program.cs magic net6 stuff that freaks me out 🙂
var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddConsole();

builder.WebHost.ConfigureKestrel((context, options) =>
{
options.AllowSynchronousIO = true;
});

// Add WSDL support
builder.Services.AddServiceModelServices().AddServiceModelMetadata();
builder.Services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();
builder.Services.AddOptions();
var app = builder.Build();

// Configure an explicit none credential type for WSHttpBinding as it defaults to Windows which requires extra configuration in ASP.NET
var myWSHttpBinding = new WSHttpBinding(SecurityMode.Transport);
myWSHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

app.UseServiceModel(builder =>
{
builder.AddService<EchoService>((serviceOptions) => { })
.AddService<IntefleCSService>((serviceOptions) => { serviceOptions.DebugBehavior.IncludeExceptionDetailInFaults = true; })
// Add a BasicHttpBinding at a specific endpoint
.AddServiceEndpoint<EchoService, IEchoService>(new BasicHttpBinding(), "/EchoService/")
// Add a WSHttpBinding with Transport Security for TLS
//.AddServiceEndpoint<EchoService, IEchoService>(myWSHttpBinding, "/EchoService/WSHttps")
// Add a BasicHttpBinding at a specific endpoint
.AddServiceEndpoint<IntefleCSService, IIntefleCSService>(new BasicHttpBinding(), "/IntefleCSService/");
// Add a WSHttpBinding with Transport Security for TLS
//.AddServiceEndpoint<IntefleCSService, IIntefleCSService>(myWSHttpBinding, "/IntefleCSService/WSHttps");
});

var serviceMetadataBehavior = app.Services.GetRequiredService<CoreWCF.Description.ServiceMetadataBehavior>();
serviceMetadataBehavior.HttpGetEnabled = true;

app.Run();
var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddConsole();

builder.WebHost.ConfigureKestrel((context, options) =>
{
options.AllowSynchronousIO = true;
});

// Add WSDL support
builder.Services.AddServiceModelServices().AddServiceModelMetadata();
builder.Services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();
builder.Services.AddOptions();
var app = builder.Build();

// Configure an explicit none credential type for WSHttpBinding as it defaults to Windows which requires extra configuration in ASP.NET
var myWSHttpBinding = new WSHttpBinding(SecurityMode.Transport);
myWSHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

app.UseServiceModel(builder =>
{
builder.AddService<EchoService>((serviceOptions) => { })
.AddService<IntefleCSService>((serviceOptions) => { serviceOptions.DebugBehavior.IncludeExceptionDetailInFaults = true; })
// Add a BasicHttpBinding at a specific endpoint
.AddServiceEndpoint<EchoService, IEchoService>(new BasicHttpBinding(), "/EchoService/")
// Add a WSHttpBinding with Transport Security for TLS
//.AddServiceEndpoint<EchoService, IEchoService>(myWSHttpBinding, "/EchoService/WSHttps")
// Add a BasicHttpBinding at a specific endpoint
.AddServiceEndpoint<IntefleCSService, IIntefleCSService>(new BasicHttpBinding(), "/IntefleCSService/");
// Add a WSHttpBinding with Transport Security for TLS
//.AddServiceEndpoint<IntefleCSService, IIntefleCSService>(myWSHttpBinding, "/IntefleCSService/WSHttps");
});

var serviceMetadataBehavior = app.Services.GetRequiredService<CoreWCF.Description.ServiceMetadataBehavior>();
serviceMetadataBehavior.HttpGetEnabled = true;

app.Run();
its this IntefleCSService that can't be created public IntefleCSService(IConfiguration configuration) { _config = configuration; }
phaseshift
phaseshift2y ago
Idk, thought it should be free. Maybe something peculiar with WCF core
Mayor McCheese
Not that this solves your problem, but in many circles it's considered bad practice to scatter iconfiguration across your app.
superbobface
superbobfaceOP2y ago
fair, instead pass options into the service contstructor? Or reference the orgiinal app class?
Mayor McCheese
Options is good That doesn't solve your problem What packages do you have
superbobface
superbobfaceOP2y ago
CoreWCF.Http, CoreWCF.Primites and System.Data.SqlClietn Microsoft.AspNETCore.App and Microsoft.NetCore.App
Mayor McCheese
Have you tried createdefaultbuilder? I don't recall all the differences I don't do much with aspnet, they both handle config iirc but I could wrong
superbobface
superbobfaceOP2y ago
starst with this var builder = WebApplication.CreateBuilder(args);
Mayor McCheese
Ugh I'll need to look not on my phone 😦 Do you have a repo?
superbobface
superbobfaceOP2y ago
lol dont worry, I've gotta head out shortly, nah its privvy
Mayor McCheese
Kk
superbobface
superbobfaceOP2y ago
thanks for looking tho
Mayor McCheese
I'll look on a bit
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server