Service Bus - micklpl/avanade-azure-workshop Wiki

Available tags

service-bus-send-message service-bus-process-message

Azure Service Bus send message and add webjob

git checkout service-bus-send-message

Populate values of these keys in application settings in Azure Portal. All settings except serviceBusSharedAccessKeyValue should have already been set as they come from ARM templete. If you want to run your app locally you have to populate all of them in Web.config.

<add key="serviceBusScheme" value="sb" />

<add key="serviceBusServiceName" value="NAME_OF_SERVICE_BUS_RESOURCE_ALREADY_HERE" />

<add key="serviceBusSharedAccessKeyName" value="RootManageSharedAccessKey" />

<add key="serviceBusSharedAccessKeyValue" value="PASTE_RootManageSharedAccessKey_HERE" />

Get missing values from Azure Portal. Click on the Service Bus resource, go to "Shared access policies" and copy one of the keys.

Now it is time to add a web job to the project. Right click on WebApp project and add New Azure WebJob Project.

Important! Make sure that newly created project is in the same .Net Framework version. They should be for instance 4.5.2 like in our example:

It would be better if you add NuGet packages by managing them in whole solution context, so you are sure all packages are at the same version across all projects. Add Autofac and Microsoft.Azure.WebJobs.ServiceBus NuGet to Topics project.

Important! Check if other packages such as Newtonsoft.Json and WindowsAzure.Storage are at same versions across other projects. To do so, right click on the solution, then go to "Manage NuGet Packages for solution..."

Paste the following code to Program.cs file:

internal class Program
{
    private static void Main()
    { 
        var config = new JobHostConfiguration();
        config.UseServiceBus();
        var host = new JobHost(config);
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
}

And the following code to Functions.cs file:

public class Functions
{
    private const string SubscriptionName = "webjobssubscription";

    public async Task ProcessGameMessage([ServiceBusTrigger(nameof(GameMessageModel), SubscriptionName)] GameMessageModel message, TextWriter textWriter)
    {
        await ProcessTopic(message, textWriter);
    }

    private async Task ProcessTopic<TTopic>(TTopic message, TextWriter textWriter) where TTopic : BaseMessageModel
    {
        await WriteMessage($"Processing topic message {typeof(TTopic).Name}. Body: {JsonConvert.SerializeObject(message)}", textWriter);
    }

    private static async Task WriteMessage(string message, TextWriter writer)
    {
        await writer.WriteLineAsync(message);
    }
}

Add reference to Avanade.AzureWorkshop.WebApp, check if all compiles and publish the app. At this point you should be able to consume messages.

STOP!!!, a task for you! Using Storage Explorer, create a games table.

Now, you can play a game, simply click "play a game" link under group stadings. Open Web Jobs logs to check if the message has arrived.

You should be able to see which group has played a game.

At this commit you get a logic that saves played game when the message is processed. Play some games, analyze the code and see which teams advance to the next stage of championships.

Obstacles

Please check that option Always On is set to On in application settings.

⚠️ **GitHub.com Fallback** ⚠️