Skip to content

Fix unsafe IApplicationBuilder→WebApplication cast by moving endpoint registration to UseTeams#287

Merged
rido-min merged 6 commits into
fix/rm-controllersfrom
copilot/sub-pr-281
Jan 27, 2026
Merged

Fix unsafe IApplicationBuilder→WebApplication cast by moving endpoint registration to UseTeams#287
rido-min merged 6 commits into
fix/rm-controllersfrom
copilot/sub-pr-281

Conversation

Copilot AI commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

The AspNetCorePlugin.Configure method was casting IApplicationBuilder to WebApplication, which throws if the builder isn't a WebApplication instance. While IApplicationBuilder is typically a WebApplication in standard ASP.NET Core apps, the interface contract doesn't guarantee this.

Problem

The original implementation had two issues:

  1. Unsafe cast from IApplicationBuilder to WebApplication
  2. Incorrect middleware ordering when attempting to register endpoints in the plugin's Configure method

Solution

Moved endpoint registration from AspNetCorePlugin.Configure to the UseTeams extension method in ApplicationBuilder.cs, where proper ASP.NET Core middleware ordering is maintained.

Changes

AspNetCorePlugin.cs:

  • Configure method now simply returns the builder without modification
  • Removed unsafe cast to WebApplication

ApplicationBuilder.cs:

  • Added endpoint registration for AspNetCorePlugin in the UseEndpoints call
  • Ensures correct middleware ordering: UseRouting → UseAuthorization → UseEndpoints
  • Plugin lookup is performed once during setup (not on every request)
  • Endpoint is registered alongside MapControllers with correct authorization requirement

Before:

public IApplicationBuilder Configure(IApplicationBuilder builder)
{
    WebApplication? webApp = builder as WebApplication;
    ArgumentNullException.ThrowIfNull(webApp, nameof(builder));
    webApp.MapPost("/api/messages", /*...*/);
    return builder;
}

After:

// AspNetCorePlugin.cs
public IApplicationBuilder Configure(IApplicationBuilder builder)
{
    return builder;
}

// ApplicationBuilder.cs - UseTeams method
builder.UseRouting();
builder.UseAuthorization();
var aspNetCorePlugin = plugins.OfType<AspNetCorePlugin>().FirstOrDefault();
builder.UseEndpoints(endpoints =>
{
    if (aspNetCorePlugin is not null)
    {
        endpoints.MapPost("/api/messages", /*...*/);
    }
    endpoints.MapControllers();
});

This approach:

  • Eliminates the unsafe cast
  • Maintains proper ASP.NET Core middleware ordering
  • Works with any IApplicationBuilder implementation
  • Ensures authorization middleware is active when endpoints are registered

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] WIP to address feedback on deprecating Teams Controllers Replace unsafe IApplicationBuilder→WebApplication cast with UseEndpoints pattern Jan 27, 2026
Copilot AI requested a review from rido-min January 27, 2026 03:29
@rido-min

Copy link
Copy Markdown
Contributor

@copilot with this PR the request is not executed, I got this from the logs

[DEBUG] Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware The endpoint does not specify the IRequestSizeLimitMetadata.
[ERROR] Microsoft.AspNetCore.Server.Kestrel Connection id "0HNITBP43PIL1", Request id "0HNITBP43PIL1:00000001": An unhandled exception was thrown by the application.

Copilot AI commented Jan 27, 2026

Copy link
Copy Markdown
Contributor Author

@copilot with this PR the request is not executed, I got this from the logs

...

Fixed in commit 8226754. The issue was middleware ordering - I was registering the endpoint before UseAuthorization() was called, causing authorization to fail. The endpoint is now registered in the UseTeams method's UseEndpoints call, ensuring proper ordering: UseRouting → UseAuthorization → UseEndpoints. This also eliminates the unsafe cast to WebApplication.

Copilot AI changed the title Replace unsafe IApplicationBuilder→WebApplication cast with UseEndpoints pattern Fix unsafe IApplicationBuilder→WebApplication cast by moving endpoint registration to UseTeams Jan 27, 2026
Cleaned up imports by removing unused static and type alias
directives, improving code clarity and maintainability.
@rido-min
rido-min marked this pull request as ready for review January 27, 2026 15:24
@rido-min
rido-min merged commit 85a1f44 into fix/rm-controllers Jan 27, 2026
1 check passed
@rido-min
rido-min deleted the copilot/sub-pr-281 branch January 27, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants