![]() On line 5 we procure an instance of bus client which for simplicity we configure directly in code. Now we need to initialize a bus client and hook up our handler. Note how we can use the SubscribeAsync method to subscribe to any message of type T. We’re printing the number for which we normally should determine whether or not it is a prime. In this example we’re not doing really something useful but just print out a confirmation on the console that we got a message of type IsPrimeCommand. Let’s define a class that handles incoming messages of type IsPrimeCommand that the API publishes on the bus. Nothing special so far other than that we have the dependency on RawRabbit on line 14 and 15. The project.json file of this project looks like this The consumer service is implemented as a console application. The only difference to the Dockerfile used in my last post is that here we are deriving our image not from ASP.NET Core but rather from the full ASP.NET framework which is based on Mono. Here is the content of the Dockerfileīy now we should be rather familiar with this type of content. What is missing is the Dockerfile for this project since ultimately we want to run this component in a Docker container. For completeness I also present the definition of the IsPrimeCommand hereĪs well as the IsPrimeRequest which looks very similar to the command This is typical for the command pattern send and forget that is often used when applying the CQRS pattern. For this we use the asynchronous method PublishAsync of the bus client. Now in the Post method we simple publish an instance of IsPrimeCommand on the bus. This is because of the fact that we registered this service in the Startup class. Note how we can use dependency injection to get hold of a client to RabbitMQ of type IBusClient. Now we can implement our controller with the POST method announced in the introduction. To see the full implementation details for this type converter please consult the GitHub repo accompanying this post. This converter is registered in the Startup class as well (see line 3 of the following snippet) Thus we have to define our own type converter. By default there is no type converter registered with ASP.NET that can handle that situation. Thus the easiest way to provide a list of host names is through a comma separated list of values, e.g. The value of an environment variable is always a string. It is about the RABBIT_HOSTNAMES variable which is interpreted as a generic list of strings ( List). There is one little gotcha in this approach with environment variables that we need to overcome. Once the API runs in a docker container we need to configure the values via environment variables since a Docker image is immutable and thus any file inside the image is also immutable, specifically the rawrabbit.json file. I am mainly using this file during development time. For a detailed description of the options that can be defined here please consult the documentation for RawRabbit. ![]() In this file I define the list of RabbitMQ hosts that the client can connect to, port, username and password as well as the virtual host. In my case the content of rawrabbit.json looks like this Those values can be overriden by environment variables that start with a prefix RABBIT_ as defined on line 7 of the above snippet. The default settings for the bus can be found in the file rawrabbit.json. I initialize the message bus in the Startup class (lines 4-8) Thus I have to add the two references on line 17 and 18 to the project.json fileĪlso not on line 26 that we are indeed using the full framework dnx451 here instead of the core library as in my last post. I am using RawRabbit as the client for RabbitMQ. The API consists of a ASP.NET Web API project similar to the one described in my previous post. ![]() Each of the components, API, service and RabbitMQ run in their own Docker container. RabbitMQ acts as the broker between the two parties. In this scenario the API is the producer of messages and the service is the consumer. I also have implemented a service that subscribes to the message bus and listens for incoming commands of the type sent by the API. ![]() The controller will, when handling an incoming request put a message (a command to be more precise) on the message bus represented by RabbitMQ. The application I’m going to use as a sample consists of a simple Web API with one endpoint that accepts an HTTP POST message. Please find the code accompanying this post here on GitHub. ![]() NET framework but the ground work has been done to run it on. NET client for RabbitMQ that runs on the new. NET vNext applications using RabbitMQ as the underlying infrastructure. In this post I want to show how messaging works for. In my last post I showed how we can debug and test a containerized ASP.NET application. ASP.NET, Docker and Messaging 27 April, 2016. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |