2

I am new and am studying ASP.NET Core 6 MVC. I am stuck in this error when building a Startup class like in my tutorial.

Error unable to resolve service for type 'Microsoft.Extensions.FileProviders.IFileProvider'

This is my Program.cs class:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Startup.cs class:

using Microsoft.Extensions.FileProviders;
using System;
using System.Collections.Generic;

namespace HCMUE_ASPCore_Lab02
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IFileProvider>
            (
                new PhysicalFileProvider
                (
                    Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")
                )
            );
            services.AddMvc();
        }
    }
}

After reading many posts, I know that ASP.NET Core 6's Program and Startup have changed and my tutorial is old now. But I don't know how to update my Program and Startup to fit with ASP.NET Core 6.

Please help me. Any help would be appreciated.

Thank you for reading.

2
  • TLDR services => builder.Services Commented Sep 27, 2022 at 4:41
  • I'm sorry, I can't quite understand your solution. Can you explain it to me, please? Commented Sep 30, 2022 at 2:15

2 Answers 2

3

The error message is saying that your application is trying to create an instance of FileUploadController but it doesn't know how to create an instance of IFileProvider to pass into the constructor.

This is a typical dependency injection error, You can register it in this code:

builder.Services.AddSingleton<IFileProvider>(new PhysicalFileProvider
                (
                    Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")
                ));

You can follow this Docs to learn about Dependency injection and other fundamentals in .Net 6.

If you are not comfortable with the new changes in .Net6, you can add the Startup class according to this article.

Sign up to request clarification or add additional context in comments.

Comments

2

You want to ensure that both the static file middleware, and any services that inject your file provider, end up using the same instance.

var provider = new PhysicalFileProvider(
  Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")
);
builder.Services.AddSingleton<IFileProvider>(provider);

...

app.UseStaticFiles(
  new StaticFileOptions{ 
    FileProvider = provider
  });

But if you don't really need a different file provider, you can instead inject IWebHostEnvironment into your controller and reuse the default .WebRootFileProvider.

1 Comment

Thank you for your help. Very much appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.