ASP.NET - Adding a Service to an ASP.NET Core Website [4 of 13]

Play ASP.NET - Adding a Service to an ASP.NET Core Website [4 of 13]
Sign in to queue

Description

ASP.NET uses a technique called "dependency injection" that's built right in. That lets us make classes called "services" that we can make available to any other classes in our website. Let's add a service now!

Follow: Scott HanselmanLeslie Richardson 

Watch the entire series here
Download Visual Studio 2019 here
Download .NET Core 3.0 here

Learn more about ASP.NET: 

Follow us: TwitterFacebookBlogsShows and Podcasts 

Useful Links: Learn .NET, .NET Community.NET Documentation

Embed

Download

The Discussion

  • User profile image
    David

    This code blows up when I enter it. It has no idea what IWebHostEnvironment is, it's underlined and I can't find any documentation, I have all the using statements you have but nothing's working! :(

  • User profile image
    Niner838598
    I added the "Microsoft.AspNetCore.Hosting" Nuget, still it's showing "IWebHostEnvironment" could not be found. I am using VS2019 community. Is there any work around?
  • User profile image
    freyzhao
    @Niner838598: IWebHostEnviornment only works for ASP.Net 3.0. Make sure you have VS2019 and the solution is targeting ASP.Net 3.0(Last time I checked it does not support VS2017).
  • User profile image
    CopBlaster
    This is really lame because nobody is going to use a static json file. So, when you try to create a service with an entities model the code will be so different that this is totally useless. Microsoft is making the same mistake they made over a decade ago with their ASP.Net tutorials that would always say something like "you could use a database but for the purpose of this demo I will just use some static data" then when someone decides to use a database they have to do a lot of work that the tutorial does not help with, so in an effort to save time they create more time.

    For instance there is no option to create a CRUD service file from the entities model like there is for a CRUD razor page or CRUD controller, so to have a service at all you will have to find a solution yourself.

Add Your 2 Cents