Expose Backend
Goal
Turn a .NET class into a remotely callable backend service using Graftcode Gateway - no controllers, no REST routes, no OpenAPI specs needed.
What You'll See
- Create a small .NET class library with public methods.
- Host it through Graftcode Gateway using Docker.
- Explore the exposed methods in Graftcode Vision - your service is now accessible from any app as a strongly-typed client.
Prerequisites
- Docker installed and running
- .NET SDK 9 installed locally (matches the
sdk:9.0Docker image used below)
Step 1. Create a project folder
Create a new .NET class library project targeting net9.0 (required so the project matches the Docker SDK image; a newer local SDK would otherwise create net10.0):
dotnet new classlib -n EnergyService -f net9.0 cd EnergyService
Step 2. Write a .NET class with public methods
Delete the auto-generated Class1.cs and create a file EnergyPriceCalculator.cs:
namespace EnergyService; public class EnergyPriceCalculator { public static int GetPrice() { return new Random().Next(100, 105); } }
This is a plain C# class - no attributes, no frameworks, no special annotations. Any public method you write here will automatically become available for remote consumption once hosted through Graftcode Gateway.
Step 3. Host it with Graftcode Gateway
Create a Dockerfile in the project root:
FROM mcr.microsoft.com/dotnet/sdk:9.0 WORKDIR /usr/app COPY . /usr/app/ RUN dotnet build RUN dotnet publish -c Release -o /usr/app/ RUN curl -fsSL grft.dev/get/gg | sh && dpkg -i gg.deb && rm gg.deb EXPOSE 80 CMD ["gg", "--modules", "EnergyService.dll"]
The key line is the last one - gg (Graftcode Gateway) analyzes your EnergyService.dll, discovers all public methods in your assembly, and exposes them automatically. The --port listener (default 80) carries WebSocket traffic (for example ws://localhost:80/ws) and serves HTTP for Graftcode Vision, MCP, and /status on the same TCP port (for example http://localhost:80/GV). Gateway also starts a second HTTP listener on --httpPort (default 81); you can map that in Docker too, but one host port is enough for this tutorial.
Build and run the container:
docker build --no-cache --pull -t myenergyservice-dotnet:test . docker run -d -p 80:80 --name graftcode_demo_dotnet myenergyservice-dotnet:test
Your .NET service is now running and exposed through Graftcode Gateway.
Step 4. Explore the service in Graftcode Vision
Open http://localhost:80/GV in your browser.
Gateway startup logs often print that Graftcode Vision is available on http://localhost:80 — that matches this tutorial's single port mapping (-p 80:80).
You will see all public methods from your .NET class - their names, parameter types, and return types. Graftcode Vision also provides:
- A "Try it out" button to call methods live, directly from the browser.
- A package manager command (npm, NuGet, PyPI, etc.) to install this service as a strongly-typed client in any other application.
Step 5. Run with a Project Key (recommended for real-world usage)
Everything above works without any account - perfect for learning and local development. When you're ready for real-world usage, create a free account at portal.graftcode.com, set up a project, and copy its Project Key.
Then pass the key when starting your gateway:
CMD ["gg", "--modules", "EnergyService.dll", "--projectKey", "YOUR_PROJECT_KEY"]
A Project Key gives you:
- Stable registry URL - consumers always find and update your Graft through a permanent address, so install commands don't change when you redeploy.
- Portal visibility - see all your gateways and exposed services in one place at gateways.graftcode.com.
- Access control - decide who can download your Grafts using package manager authentication and permissions.
- MCP endpoint - Graftcode Gateway automatically exposes an MCP (Model Context Protocol) endpoint alongside your service, making your methods callable by AI agents and LLM-based tools out of the box.
Step 6. Call it from another app
Your service is now accessible from any application. From Graftcode Vision, select your target package type - for example NuGet - and copy the generated install command. That installs a Graft: a strongly-typed client that lets any app call your service methods directly.
using graft.nuget.energypricecalculator; var price = await EnergyPriceCalculator.GetPrice(); Console.WriteLine(price);
No REST clients, no request/response models, no endpoint URLs in your code. When you add or update a public method, consumers update their Graft with a single package manager command.