Deepti
Deepti

Reputation: 65

Getting error can not access disposed object for builder.build() on visual studio update

I am getting error on debug session start on my dot net core API project; since I updated visual studio to latest version 17.1.1. Following is the exception detail, it is showing on console. I tried by deleting temp, bin, obj folders but nothing worked. Has somebody faced such an issue or know how to fix?

Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object. Object name: 'ConfigurationManager'. at Microsoft.Extensions.Configuration.ReferenceCountedProviderManager.AddProvider(IConfigurationProvider provider) at Microsoft.Extensions.Configuration.ConfigurationManager.AddSource(IConfigurationSource source) at Microsoft.Extensions.Configuration.ConfigurationManager.Microsoft.Extensions.Configuration.IConfigurationBuilder.Add(IConfigurationSource source) at Microsoft.AspNetCore.Builder.WebApplicationBuilder.<>c__DisplayClass25_0.b__2(HostBuilderContext context, IServiceCollection services) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build() at Program.$(String[] args) in Program.cs:line 40

Upvotes: 5

Views: 2461

Answers (4)

BIGNIK
BIGNIK

Reputation: 1

It happened to me when I used at .net 8.0:

IConfiguration? configuration;
using (var serviceProvider = services.BuildServiceProvider())
{
    configuration = serviceProvider.GetService<IConfiguration>();
}

Fixed this using this code:

IConfiguration configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
#if DEBUG
    .AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true)
#endif
    .AddEnvironmentVariables()
    .Build();

Upvotes: 0

Ben Croughs
Ben Croughs

Reputation: 2663

we also had this issue since march 8. is was introduced with the release of 6.0.3, see a github post about the issue : https://github.com/dotnet/aspnetcore/issues/40614

for now what we did is revert to the 6.0.2 version (this is a temporary work around, i will hope to figure out what was wrong asap)

for docker images:

FROM mcr.microsoft.com/dotnet/aspnet:6.0.2 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0.200 AS build
WORKDIR /src

if you are using it in yml also probably

use dotnetversion

DotNetVersion: "6.0.200" instead of "6.0.x" 

6.0.200 is the sdk version of 6.0.2 framework https://dotnet.microsoft.com/en-us/download/dotnet/6.0

11/03/2022 see also this https://github.com/dotnet/core/issues/7259 were i have pinpointed the issue in our code and added a sample app to reproduce

if we look into that repo https://github.com/microsoft/ApplicationInsights-Kubernetes/blob/69f44c6ec3fda26d76a01836b851402e3f8a02ad/src/ApplicationInsights.Kubernetes/Extensions/ApplicationInsightsExtensions.cs

we indeed find the same piece of code on the other answers

enter image description here

Upvotes: 2

Fazel Saeedi
Fazel Saeedi

Reputation: 95

i faced to this problem when i update my SDK both in docker and my window 11

my sdk is : 6.0.3

but i cant understand why this problem is happend

Upvotes: 0

kvanover
kvanover

Reputation: 41

It is because you use the old way of getting the settings from the configuration manager, like:

using (var serviceProvider = services.BuildServiceProvider())
{
...
}

If you remove these lines and just use the configuration as-is with

options = configuration.GetOptions<Object>("xxx");

it will work

Upvotes: 4

Related Questions