I made some changes to a razor webapp that still uses asp.net-core-tag-helpers (which is deprecated but according to the docs can still be used). The biggest change is the switch to .net9.0. Locally the application runs fine. But if i run
dotnet publish -p:TargetFramework=net9.0 -p:Nullable=enable
-p:Version=1.0.4.0 -p:AssemblyVersion=1.0.4 -p:FileVersion=1.0.4.0
--self-contained --runtime win-x64
# i also tried
dotnet publish -c release --self-contained --runtime win-x64
And start the myrazor_webapp.exe
under windows 10 i get
Unhandled exception. System.MissingMethodException:
Method not found:
'Microsoft.AspNetCore.Builder.RouteHandlerBuilder
Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions.MapGet(
Microsoft.AspNetCore.Routing.IEndpointRouteBuilder, System.String, System.Delegate)'.
at Program.<Main>$(String[] args)
This is the code in my program.cs
var builder = WebApplication.CreateBuilder(args);
// Logging
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddDebug();
// 2024-12-02
builder.Services.AddScoped<UserService>();
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllers();
// Entity Framework
builder.Services.AddDbContext<NvTradeContext>();
// Register other repositories
builder.Services.AddScoped<IFooRepo, FooRepository>();
builder.Services.AddScoped<IBarRepo, BarRepository>();
// 2024-12-04 Prepare switch to Dapper
builder.Services.AddScoped<DatabaseRepository>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddAuthentication(authOpt =>
{
authOpt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
authOpt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Authority = "https://localhost:7178/";
options.Audience = "http://localhost:5057/";
});
// add jwt token for later
builder.Services.AddAuthorization(options =>
{
var policies = builder.Configuration.GetSection("Policies:Permissions").Get<List<string>>();
policies.ForEach(p =>
{
options.AddPolicy(p, policy =>
{
policy.AddRequirements(new IsInRoleRequirement(p));
});
});
});
builder.Services.AddTransient<IAuthorizationHandler, IsInRoleAuthorizationHandler>();
var app = builder.Build();
app.UseStatusCodePages(async context =>
{
var response = context.HttpContext.Response;
if (response.StatusCode is (int)HttpStatusCode.Unauthorized or (int)HttpStatusCode.Forbidden)
response.Redirect("/Info?error=Unauthorized");
});
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<NvTradeContext>();
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers();
app.Run();
Update for .net-8.0
I switched to .net-8.0 updated csproj-file and ran
dotnet publish -p:TargetFramework=net8.0
-p:Nullable=enable -p:FileVersion=1.0.4.0
--self-contained
--runtime win-x64
This solved the MissingMethodException
Error but all styles and javascript files are 404
not found
How can i include all static files under wwwroot
so that they are part of the selfcontained exe
C:devmyappwwwroot>tree
C:.
├───css
│ └── site.css
├───js
│ ├── FilterTable.js
│ └── site.js
├───lib
│ ├───bootstrap
│ │ └───dist
│ │ ├───css
│ │ └───js
│ ├───jquery
│ │ └───dist
│ ├───jquery-validation
│ │ └───dist
│ └───jquery-validation-unobtrusive
└───static
How to fix MissingMethodException
- What causes this?
- How to fix self contained with .net 9?
- How to include static assets with .net 8?