Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfiguration' while attempting to activate 'DotNotStandard.DataAccess.Core.ConnectionAddressManager'.))” #7
-
When I tried the generated class to query the data, I actually reported an error, the error is as follows:“One or more errors occurred. (DataPortal.Fetch 失败 (Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfiguration' while attempting to activate 'DotNotStandard.DataAccess.Core.ConnectionAddressManager'.))”,IConfiguration was not implemented properly,how can i resolve this error. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, Sorry for the delay, this notification got lost in amongst other work communications. This error isn't directly related to the generated code, as such. This error message indicates that the host application is not initializing configuration. That's unusual for an ASP.NET host, so are you perhaps using another host? Your message doesn't mention what technology your host application uses. You may need to manually add configuration to whatever HostBuilder is used to initialize the host environment you are using. Here's an example snippet, but note that this creates a new HostBuilder; in general you want to add the request for configuration to an existing HostBuilder. new HostBuilder()
.ConfigureAppConfiguration((hostingContext, config) =>
{
var baseConfigFileInfo = new FileInfo("appsettings.json");
var environmentConfigFileInfo = new FileInfo($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json");
config
.AddJsonFile(baseConfigFileInfo.FullName, optional: true, reloadOnChange: false)
.AddJsonFile(environmentConfigFileInfo.FullName, optional: true, reloadOnChange: false);
config.AddEnvironmentVariables();
config.AddCommandLine(args);
}); This code will probably need a bit of tweaking to suit your exact hosting environment. For example, you might choose to use fewer configuration sources for an application running on a client machine - for example, pulling configuration from environment variables probably doesn't make sense for a .NET MAUI app. There is a key/value pair configuration provider you can use if you want to hard-code the configuration for your data provider (the connection string, in particular), although that would be fairly unusual. This code generally goes into Program.cs for a modern .NET app. A slightly older style of app might put it into Startup.cs. |
Beta Was this translation helpful? Give feedback.
Hi,
Sorry for the delay, this notification got lost in amongst other work communications.
This error isn't directly related to the generated code, as such. This error message indicates that the host application is not initializing configuration. That's unusual for an ASP.NET host, so are you perhaps using another host? Your message doesn't mention what technology your host application uses.
You may need to manually add configuration to whatever HostBuilder is used to initialize the host environment you are using. Here's an example snippet, but note that this creates a new HostBuilder; in general you want to add the request for configuration to an existing HostBuilder.