As our shared library package isn’t published to a hosted server, we need to add a new source pointing at our local-nuget-packages folder, so let’s start with that.
Add Local NuGet Feed
To use our local NuGet folder as a feed in our microservices, we need to create a nuget.config
file at the same level as our solution, specifying a new source:
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="local-nuget-packages" value="../local-nuget-packages" /> </packageSources> </configuration>
Here, we add a new entry in <packageSources>
, pointing to the folder that contains the ECommerce.Shared
NuGet package.
Now we can install our package locally:
dotnet add package ECommerce.Shared -v 1.0.0
If installing via Visual Studio, we need to ensure we’ve selected the correct Package source from the drop-down in the NuGet Package Manager:
At this point, our project will build fine if we attempt to do so from the command line or Visual Studio. However, if we attempted to build from Docker, we’d run into some issues as we haven’t copied our local NuGet package into the Docker image, so let’s address that now.
Update Dockerfile
Unfortunately, when we work with Docker and Dockerfiles, we can only use the current folder or any child folders to copy content from. Docker cannot access parent folders to copy content, so we are going to have to change the context for where we run our docker commands. Furthermore, we need to copy our NuGet package folder so our project can use it as a NuGet source. So, let’s do that in our Dockerfile
:
FROM mcr.microsoft.com/dotnet/aspnet:9.0 as base WORKDIR /app FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build ARG BUILD_CONFIGURATION=Release COPY ["order-microservice/Order.Service/Order.Service.csproj", "Order.Service/"] COPY ["local-nuget-packages/", "/local-nuget-packages/"] COPY ["order-microservice/nuget.config", "./"] RUN dotnet restore "Order.Service/Order.Service.csproj" COPY ["order-microservice/Order.Service/", "Order.Service/"] WORKDIR "Order.Service" RUN dotnet build "Order.Service.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build as publish RUN dotnet publish "Order.Service.csproj" -c $BUILD_CONFIGURATION -o /app/publish FROM base as final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "Order.Service.dll"]
First, we change two of our existing COPY
commands to use the correct folder structure as we’ll be running from a parent folder when building our images. Then, we copy our l
ocal-nuget-packages
folder, as well as our nuget.config
file before we run dotnet restore
. Before we build a new image, let’s consume the new library code and remove the duplicated code.
Replace Existing Code
First, we can remove the EventBus
and RabbitMq
folders, including any folders/classes inside. Next, we need to update our OrderCreatedEvent record so we can use the new Event record from the shared library:
using ECommerce.Shared.Infrastructure.EventBus; namespace Order.Service.IntegrationEvents.Events; public record OrderCreatedEvent(string CustomerId) : Event;
Similarly, we need to update our OrderApiEndpoint
class to use the IEventBus
interface from our shared library:
using ECommerce.Shared.Infrastructure.EventBus.Abstractions;
Finally, in our Program
class, we update the using
statement. Further, we need to call a new method to register our IEventBus
interface:
using ECommerce.Shared.Infrastructure.RabbitMq; var builder = WebApplication.CreateBuilder(args); builder.Services.AddRabbitMqEventBus(builder.Configuration) .AddRabbitMqEventPublisher(); var app = builder.Build();
Let’s follow a similar set of steps for our Basket microservice next.