Solutions    |    Downloads    |    License    |    Documentation    |    Training    |    Support    |    Customers    |    About Us

Getting Started - Creating A New Project

Last Updated: Apr 19, 2013 08:46PM IDT
Getting Started

Getting Started With NServiceBus

Create a new project

Create new project
Projects in solution

 

  • Choose "NServiceBus System" for the type of project.
  • Enter "Amazon" (or any other name) for the name of your solution.
After the project creation goes forward, you'll see that a number of projects have been created in your solution as shown to the right. You can ignore the .nuget folder as it is part of the infrastructure as is the .slnbldr file under Solution Items. The more important pieces are the Contract and InternalMessages projects as they will be the place where all message types are put. All events defined will be put in the Contract project while all commands will be put in the InternalMessages project.
Later on we'll see how messages from different services are partitioned within these projects.
Now let's take a look at the design environment in Solution Builder.
If you can't see a docked window in Visual Studio called Solution Builder, you can open it up via the View menu (alt+V).

 

Solution Builder

You should see 3 folders in Solution Builder called Infrastructure, Endpoints, and Services.

Services are logical constructs that are used to model the business domain. They are made of many components which can send, process, publish, and subscribe to messages.

All components must ultimately be deployed to Endpoints which can be web applications (both WebForms and MVC) or NServiceBus Hosts (a special kind of Windows Service that allows you to debug it as a Console Application).

Infrastructure is where cross-cutting concerns like authentication and auditing are handled.

Creating Endpoints

Right-click on Endpoints, select Host, and then NServiceBus ASP NET MVC Endpoint as shown below.

IMPORTANT: If you haven't installed ASP MVC on your machine, choose a Web Endpoint instead.

Create an MVC front end

Call it ECommerce (or another name if you like) and the Visual Studio project will be created for you.

We won't go into detail right now as to the code that's been generated, but we'll look into it later so you'll know how things are working behind the scenes. However, notice the Components folder that's been created under the ECommerce endpoint - that's where the components will be deployed.

Go ahead and create another endpoint called OrderProcessing as an NServiceBus Host.

Now let's see some services:

Creating Services

Right-click on the Services folder in Solution Builder and choose Add as shown below:

Creating services

Name the service "Sales" and you should see the image to the right:

Service structure

There is currently much to see, and you might notice that no code has been generated at this point.

It is only when you have components that are allocated to endpoints that code is generated.

Now, right-click on Commands, select Add, and name it SubmitOrder. Several things will happen.

After adding a command

First you'll see a command created under the Commands folder, as expected.

Also, you should see that 2 components have been created in Sales - a Processor component and a Sender component named after the command you created.

You'll see a link to the command under the Subscribes / Process folder of the Processor component and a link under the Publishes / Sends folder of the Sender component.

If you double-click the SubmitOrder command, the class file will be opened:

using System;

namespace Amazon.InternalMessages.Sales
{
    public class SubmitOrder
    {
    }
}

You can add all sorts of properties to your message - strings, integers, arrays, dictionaries, etc. Just make sure to provide both get and set.

Double-clicking on the components won't open any code just yet as their code is only created when they're deployed to an endpoint.

So, let's go ahead and do just that.

Deploying Components

Right-click the SubmitOrderSender component, choose "Deploy to...", and then pick the ECommerce endpoint from the drop-down.

If you try to build your solution at this point, you will get an error telling you that Sales.SubmitOrderProcessor should be allocated to an endpoint. This is also due to the fact that the code for the SubmitOrderProcessor component wasn't generated yet as we don't know in which project it should be put.

So let's go ahead and allocate the component to its endpoint.

This time we'll do that by starting at the endpoint, so right-click the Components folder of the OrderProcessing endpoint,
choose "Add component...", and then tick the box beside the Sales.SubmitOrderProcessor component, and click "OK".

Now build the solution and see how everything turns out.

Double-click the components (under the services folder, not the endpoints folder), to open their code. There isn't much to see in the SubmitOrderSender but you'll notice that it's a partial class:

using System;
using NServiceBus;
using Amazon.InternalMessages.Sales;

namespace Amazon.ECommerce.Components.Sales
{
   public partial class SubmitOrderSender
   {
   }
}

Navigate to the rest of its definition by selecting its name and then pressing F12. You should see this:

using System;
using NServiceBus;
using NServiceBus.Config;
using Amazon.InternalMessages.Sales;

namespace Amazon.ECommerce.Components.Sales
{
   public partial class SubmitOrderSender: ISubmitOrderSender, Amazon.ECommerce.Infrastructure.INServiceBusComponent
   {
        public void Send(SubmitOrder message)
        {
            Bus.Send(message);    
        }

        public IBus Bus { get; set; }
    }

    public interface ISubmitOrderSender
    {
        void Send(SubmitOrder message);
    }
}

This component comes with an interface you can inject into your own MVC controllers, and implements the INServiceBusComponent interface so that NServiceBus will know to register it into the container for you automatically. The first empty partial class is there for you to add any additional behavior, for example any logic you might want to have that transforms your model objects into messages.

Now double-click the SubmitOrderProcessor.

using System;
using NServiceBus;
using Amazon.InternalMessages.Sales;

namespace Amazon.OrderProcessing.Sales
{
   public partial class SubmitOrderProcessor
   {
        partial void HandleImplementation(SubmitOrder message)
        {
            //    Implement your handler logic here.
            Console.WriteLine("Sales received " + message.GetType().Name);        

        }
    }
}

Once again, there isn't much here - all you need to do is put your logic in. You can also F12 on the class to see its counterpart, but there isn't much to see there either - just a class which implements IHandleMessages<SubmitOrder> and has a reference to IBus that you can use to send out other messages, publish events, or reply with.

Almost there

The last thing we need to do is make the ECommerce website send a message.

IMPORTANT: If you created the ASP MVC application, take the following action, otherwise skip to where it says "Regular ASP.NET"

Find the HomeController in the Controllers folder in the Amazon.ECommerce project and add a property of the type ISubmitOrderSender and invoke its Send method like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Amazon.ECommerce.Components.Sales;
using Amazon.InternalMessages.Sales;

namespace Amazon.ECommerce.Controllers
{
    public class HomeController : Controller
    {
        public ISubmitOrderSender SubmitOrderSender { get; set; }

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            SubmitOrderSender.Send(new SubmitOrder());

            return View();
        }
    }
}

Regular ASP.NET, take the following action

Open up Default.aspx, drag a button from the toolbox onto the page (make sure the page is in "Design" view). Double-click on the button you just dragged, which should open the code-behind button-click handling method.

In that method, type:

	Global.Bus.Send(new SubmitOrder());

END, Regular ASP.NET

 

Now press F5 and run it. You should see something like the image below - a new tab in your browser and a console application. If you click "About" in the UI a couple of times, you should see the console application get a message each time.

If you're in a regular ASP.NET web project, you'll see a different image, but just click the button on the form.

Web to console messaging

Congratulations - you've just built your first NServiceBus application.

Wasn't that easy?

* You don't have to worry about the warnings - this is just NServiceBus telling you that it couldn't find the queues it needs
so it went ahead and created them for you.

Now let's take things a step forward so you can see the production-time benefits of NServiceBus because, let's face it, interprocess communication isn't that exciting and has been done many times before.

 

Continue to see how NServiceBus handles Fault Tolerance

About NServiceBus    |    Contact Us    |    Privacy    |    Follow us on:   
Copyright 2010-2013 NServiceBus. All rights reserved
NSB_Y@yahoo.com
http://assets2.desk.com/r1046ffeaa2233e531563a32d7edef6677d8a78b5/javascripts/
nservicebus
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
true
Invalid characters found
/customer/portal/articles/autocomplete
There was an error contacting Get Satisfaction
View All
0
discussions
replies
Questions
Ideas
Problems
Praise