This project is designed to be repeated not cloned or deployed from here ... please following the instructions below and use this site as a comparison for the final output.
- Install .NET 6.0 SDK
- Install VSCode
- Go to your projects/development folder
- Such as
cd c:\Development\
- Such as
- Create first project and open in VSCode
dotnet new web -n HelloNet cd HelloNet code .
- If loading .NET code for the first time you may be prompted to install OmniSharp and other extensions, please click through to install recommendations to complete this tutorial.
- Run project
- Open Terminal (
Ctrl+Shift+'
orMenu->Terminal->New Terminal
) - dotnet run
- Open Terminal (
- Check project output and view the site root (e.g.
http://localhost:5094/
) - Open main code file
Program.cs
- Modify default "/" method to return "Welcome to Hello .NET"
- Add additional handler for "/hello" to return "Hello World!"
- Stop and run project e.g.
ctrl+c
in terminal thendotnet run
again - View updated site
- Check /
- Check /hello You should see the updated responses
- Add a method to return an example object
app.MapGet("/json", () => new { example=123, myString="This is a string", source=nameof(app) });
- Restart the server again and check "/json" endpoint
- Below app.MapGet(...) calls and above app.Run() add the following function
IResult MyCustomEndpoint()
{
var myDictionary = new Dictionary<string,dynamic>();
myDictionary.Add("anInteger", 123);
myDictionary.Add("aString", "Example string");
myDictionary.Add("now", DateTime.UtcNow);
myDictionary.Add("nowString", DateTime.UtcNow.ToString("yyyy-MM-dd mm:HH:ss"));
myDictionary.Add("compositeString", $"Now={DateTime.UtcNow:yyyy-MM-dd}, Count={myDictionary.Keys.Count}");
return Results.Ok(myDictionary);
}
- Add another endpoint (in existing MapGet block):
app.MapGet("/custom", MyCustomEndpoint);
- Restart and test
/custom
endpoint
- Create a folder called "Interfaces"
- Right click the folder and select "New C# Interface"
- Call it
IMyService
- Call it
- Shorten namespace declaration and add example GetDataMethod by replacing it's body/content with this:
namespace HelloNet.Interfaces;
public interface IMyService
{
string GetData();
}
- Create a
Services
folder, right click and select "New C# Class"- Call it
MyService
- Call it
- Amend
MyService
to the following:
namespace HelloNet.Services;
public class MyService : IMyService
{
}
- You should see that IMyService has a red squiggly line underneath it showing an issue
- Move the cursor to
IMyService
and pressctrl+.
then selectusing HelloNet.Interfaces
- This inserts the required
using HelloNet.Interfaces;
line at the top, which you could have entered manually but useful to know this technique too.
- This inserts the required
- Move the cursor to
- You should now see the colour of IMyService has changed to match other names, but that it still has a red squiggly line showing another issue
- Again select and use
ctrl+.
, this time selectingImplement interface
- Again select and use
- That will have generated out a template method with a
NotImplementedException
, so we will replace that with this short lambda version:
public string GetData() => $"Called {nameof(GetData)} in instance of {nameof(MyService)}";
- Add the following endpoint to the current MapGet block:
app.MapGet("/services", (IMyService service) => service.GetData());
- Again you'll have a red squiggly line, use
ctrl+.
->using ...
or addusing HelloNet.Services;
to the top ofProgram.cs
to resolve the missing reference. - Restart project and you'll find it complains with
Unhandled exception. System.InvalidOperationException: Body was inferred but the method does not allow inferred body parameters.
Below is the list of parameters that we found:
Parameter | Source
---------------------------------------------------------------------------------
service | Body (Inferred)
Did you mean to register the "Body (Inferred)" parameter(s) as a Service or apply the [FromService] or [FromBody] attribute?
This is because we have no registered anything of type IMyService so it isn't connecting this to out implementation...
- After
var builder = WebAppllication.CreateBuilder(args);
and beforevar app = builer.Build();
add the following builder.Services.AddSingleton<IMyService,MyService>(); - Resolve the missing dependency with
ctrl+.
or by addingusing HelloNet.Services
- Restart and view
/services
- Add the
myData
property to appsettings.json e.g.
{
...
"myData": "This is from appsettings.json"
...
}
- Update
MyService
to use this value by replacing theGetData
method with the following:
protected IConfiguration Configuration;
public MyService(IConfiguration configuration)
{
Configuration = configuration;
}
public string GetData() => Configuration.GetValue<string>("myData");
- Restart and refresh
/services
- Set
myData
inappsettings.Development.json
to"This is from appsettings.Development.json"
- Restart and refresh
/services
... you will see the new value, as when running in development environment this file takes precedence over base values fromappsetings.json