Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a bug in UseInMemoryDatabase #33593

Open
PrestigeDevop opened this issue Apr 22, 2024 · 2 comments
Open

a bug in UseInMemoryDatabase #33593

PrestigeDevop opened this issue Apr 22, 2024 · 2 comments

Comments

@PrestigeDevop
Copy link

PrestigeDevop commented Apr 22, 2024

File a bug

hello , I was following the minimal api documentation here

and got this error
Unhandled exception. Cannot print exception string because Exception.ToString() failed.

Include your code

using Microsoft.EntityFrameworkCore;
using NSwag.AspNetCore;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoList"));//DI?
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiDocument(config =>
{
    config.DocumentName = "TodoAPI";
    config.Title = "TodoAPI v1";
    config.Version = "v1";
});
var app = builder.Build(); 

// GET
app.MapGet("/todoitems", async (TodoDb db) =>
    await db.Todos.ToListAsync());
// Get with filter
app.MapGet("/todoitems/complete", async (TodoDb db) =>
    await db.Todos.Where(t => t.IsComplete).ToListAsync());

app.MapGet("/todoitems/{id}", async (int id, TodoDb db) =>
    await db.Todos.FindAsync(id)
        is Todo todo
            ? Results.Ok(todo)
            : Results.NotFound());
// Post
app.MapPost("/todoitems", async (Todo todo, TodoDb db) =>
{
    db.Todos.Add(todo);
    await db.SaveChangesAsync();

    return Results.Created($"/todoitems/{todo.Id}", todo);
});

//Put for update
app.MapPut("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) =>
{
    var todo = await db.Todos.FindAsync(id);

    if (todo is null) return Results.NotFound();

    todo.Name = inputTodo.Name;
    todo.IsComplete = inputTodo.IsComplete;
    
    await db.SaveChangesAsync();

    return Results.NoContent();
});
// Delete
app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) =>
{
    if (await db.Todos.FindAsync(id) is Todo todo)
    {
        db.Todos.Remove(todo);
        await db.SaveChangesAsync();
        return Results.NoContent();
    }

    return Results.NotFound();
});

// Entry point -- optional
app.MapGet("/", () => "Hello World!");

app.MapGet("/users/{userId}/{postID}", 
    (string userId, int postID) => $"The user id is {userId} and post id is {postID}");

app.Run();

Include stack traces

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostApplicationLifetime Lifetime: Singleton ImplementationType: Microsoft.Extensions.Hosting.Internal.ApplicationLifetime': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostLifetime Lifetime: Singleton ImplementationType: Microsoft.Extensions.Hosting.Internal.ConsoleLifetime': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Logging.ILoggerFactory Lifetime: Singleton ImplementationType: Microsoft.Extensions.Logging.LoggerFactory': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Hosting.IApplicationLifetime Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Hosting.GenericWebHostApplicationLifetime': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Connections.IConnectionListenerFactory Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Server.Kestrel.Core.IHttpsConfigurationService Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Server.Kestrel.Core.HttpsConfigurationService': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Hosting.Server.IServer Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Connections.IConnectionListenerFactory Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Server.Kestrel.Transport.NamedPipes.Internal.NamedPipeTransportFactory': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Server.Kestrel.Core.HttpsConfigurationService+IInitializer Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Server.Kestrel.Core.HttpsConfigurationService+Initializer': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Connections.IMultiplexedConnectionListenerFactory Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.QuicTransportFactory': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder Lifetime: Transient ImplementationType: Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Routing.LinkGenerator Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Routing.DefaultLinkGenerator': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Routing.LinkParser Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Routing.DefaultLinkParser': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Diagnostics.IDeveloperPageExceptionFilter Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseDeveloperPageExceptionFilter': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.) (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Hosting.GenericWebHostService': Could not load type 'System.Runtime.CompilerServices.NullableAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.)'

Include provider and version information

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.UseInMemoryDatabase
Target framework: NET 8.0
Operating system: Windows 11 Pro
IDE: Visual Studio 2022 preview ( also vs code latest )

@AndriySvyryd
Copy link
Member

Try using global.json to explicitly specify a recent SDK

@ajcvickers
Copy link
Member

This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants