Creating a Webservice
This tutorial will show you how to create, run and test a new web service implementedwith the GenHTTP framework.
Prerequisites
For this tutorial you will need
- the.NET SDK to be installed on your machine
- optionally an IDE of your choice, such asVisual Studio,VS Code orRider
Creating the Project
The easiest way of creating a new webservice is by using aproject template. If not alreadydone, install the templates by executing the following command in your terminal:
dotnet new -i GenHTTP.Templates
After the templates have been installed, you can create a new folder for your project and use the webservicetemplate to create the project:
mkdir MyServicecd MyServicedotnet new genhttp-webservice
After the command has been executed the content of the folder should look something like this:
- BookService.cs
- MyService.csproj
- Program.cs
- Project.cs
If you are using an IDE, you can open the fileMyService.csproj
to load your project there.TheProgram.cs
is the main entry point of your service and is used to set up your server instance.InProject.cs
our web service is actually defined and configured. TheBookService.cs
isan example service provided by our project.
Running the Project
After the project has been created, you can navigate to the service project in your terminal (cd MyService
)and run the project from there:
dotnet run
Alternatively you can directly run the project from your IDE (F5
in Visual Studio).
This will start the server process and make your app available onhttp://localhost:8080.To actually invoke a service method you can navigate tohttp://localhost:8080/books/ in your browser or by usingany other HTTP client tool (such as curl or Postman).
Testing the Project
Together with the actual service project, the template also created a test project that ensuresthat your service works the way it should. You can either run the tests from your IDEor by executing the following command in theMyService.Tests
folder:
dotnet test
Implementing your Service
You now have the basic setup to run and test your service project. To actually implementyour service, you can add new service classes to theServices
directory and register themin theProject.cs
.
.AddService<AnotherService>("another")
Next Steps
The documentation will show you the details of the web service framework as wellas all other aspects that are relevant when developing an webservice applicationusing the GenHTTP framework.