Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core WebAPI crashes because I don't have StaticFileMiddleware?

I'm trying to build a simple WebAPI serving JSON data to a web app, but for some reason every call ends in not finding anything and instead crashing.

I have a controller with a route:

[Produces("application/json")]
[Route("api/Players")]
public class PlayersController : Controller
{
    [HttpGet("{name}", Name = "GetPlayer")]
    public Player Get(string name)
    {
        return playerService.GetPlayer(name);
    }

The playerservice method simply returns a dto. Unfortunately, this endpoint isn't even called.
The request pipeline gets called, and afterwards crashes with System.MissingMethodException: 'Method not found: 'Void Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware..ctor, although I'm not even trying to get any static files, just /api/players/dummy or /swagger, none work. The call stack here is just Program.Main and then external code.

ConfigureServices goes

services.AddMvcCore().AddApiExplorer();
services.AddSwaggerGen(c => c.SwaggerDoc("v1", new Info { Title = "API", Version = "v1" }));

services.AddControllers();

Configure goes

app.UseSwagger()
    .UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"))
    .UseRouting()
    .UseEndpoints(endpoints => endpoints.MapControllers());

Is my pipeline configured incorrectly? Do I need some static file middleware although I don't even serve static files? What am I doing wrong?

Edit: Screenshot of the exception: enter image description here

Here's the stacktrace from the exception, it seems it happens in swagger:

at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.CreateStaticFileMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, ILoggerFactory loggerFactory, SwaggerUIOptions options)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware..ctor(RequestDelegate next, IHostingEnvironment hostingEnv, ILoggerFactory loggerFactory, SwaggerUIOptions options)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.WebHost.BuildApplication()
   at Microsoft.AspNetCore.Hosting.WebHost.<StartAsync>d__27.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.<RunAsync>d__5.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.<RunAsync>d__5.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.<RunAsync>d__4.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
   at Teambuilder_WebAPI.Program.Main(String[] args) in D:\Sources\Teambuilder\Teambuilder WebAPI\Program.cs:line 11
like image 393
Squirrelkiller Avatar asked Nov 23 '25 02:11

Squirrelkiller


1 Answers

Version 5 of Swashbuckle.AspNetCore package is in pre-release stage and having issue is expected.

As OP confirmed, this github issue is related to this problem, so solution would be updating to latest version.

Update: As OP confirmed, you can use latest stable version of Swashbuckle v4.0.1 with .Net Core v3.1 and you are good to go.

like image 155
Mehdi Dehghani Avatar answered Nov 24 '25 18:11

Mehdi Dehghani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!