Browse files

Make sure service bus provider is usable.

  • Loading branch information...
1 parent bf320db commit d9223d81cc443391c452c174676033424a40aefe @davidfowl davidfowl committed Oct 29, 2012
View
1 src/Microsoft.AspNet.SignalR.Core/Microsoft.AspNet.SignalR.Core.csproj
@@ -36,6 +36,7 @@
<DocumentationFile>bin\Release\Microsoft.AspNet.SignalR.Core.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="Microsoft.WindowsAzure.ServiceRuntime, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
View
77 src/Microsoft.AspNet.SignalR.ServiceBus/DependencyResolverExtensions.cs
@@ -1,19 +1,86 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
using System;
-using System.Collections.Generic;
-using Microsoft.AspNet.SignalR.Infrastructure;
+using System.Runtime.CompilerServices;
namespace Microsoft.AspNet.SignalR.ServiceBus
{
public static class DependencyResolverExtensions
- {
- public static IDependencyResolver UseServiceBus(this IDependencyResolver resolver, string connectionString, int partitionCount, int nodeCount, int nodeId, string topicPrefix)
+ {
+ /// <summary>
+ /// Use the Windows Azure Service Bus backplane for SignalR.
+ /// </summary>
+ /// <param name="resolver">The dependency resolver.</param>
+ /// <param name="connectionString">The service bus connection string.</param>
+ /// <param name="instanceCount">The number of role instances in the deployment.</param>
+ /// <returns>The dependency resolver.</returns>
+ public static IDependencyResolver UseWindowsAzureServiceBus(this IDependencyResolver resolver, string connectionString, int instanceCount)
{
- var bus = new Lazy<ServiceBusMessageBus>(() => new ServiceBusMessageBus(connectionString, partitionCount, nodeCount, nodeId, topicPrefix, resolver));
+ return UseWindowsAzureServiceBus(resolver, connectionString, instanceCount, topicCount: 1);
+ }
+
+ /// <summary>
+ /// Use the Windows Azure Service Bus backplane for SignalR.
+ /// </summary>
+ /// <param name="resolver">The dependency resolver.</param>
+ /// <param name="connectionString">The service bus connection string.</param>
+ /// <param name="instanceCount">The number of role instances in the deployment.</param>
+ /// <param name="topicCount">The number of topics to use.</param>
+ /// <returns>The dependency resolver.</returns>
+ public static IDependencyResolver UseWindowsAzureServiceBus(this IDependencyResolver resolver, string connectionString, int instanceCount, int topicCount)
+ {
+ AzureRoleInfo azureRole = null;
+
+ try
+ {
+ azureRole = GetRoleInfo();
+ }
+ catch (TypeLoadException ex)
+ {
+ throw new InvalidOperationException("Unable to resolve the instance index of this role. Make sure Microsoft.WindowsAzure.ServiceRuntime.dll is deployed with your application.", ex);
+ }
+
+ return UseServiceBus(resolver, connectionString, topicCount, instanceCount, azureRole.Index, azureRole.Name);
+ }
+
+ /// <summary>
+ /// Use the Windows Azure Service Bus backplane for SignalR.
+ /// </summary>
+ /// <param name="resolver">The dependency resolver.</param>
+ /// <param name="connectionString">The service bus connection string.</param>
+ /// <param name="topicCount">The number of topics to use.</param>
+ /// <param name="instanceCount">The number of role instances in the deployment.</param>
+ /// <param name="instanceIndex">The zero-based index of this role instance in the deployment.</param>
+ /// <param name="topicPrefix">The topic prefix.</param>
+ /// <returns>The dependency resolver.</returns>
+ public static IDependencyResolver UseServiceBus(this IDependencyResolver resolver, string connectionString, int topicCount, int instanceCount, int instanceIndex, string topicPrefix)
+ {
+ var bus = new Lazy<ServiceBusMessageBus>(() => new ServiceBusMessageBus(connectionString, topicCount, instanceCount, instanceIndex, topicPrefix, resolver));
resolver.Register(typeof(IMessageBus), () => bus.Value);
return resolver;
}
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private static AzureRoleInfo GetRoleInfo()
+ {
+ var roleInstance = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.CurrentRoleInstance;
+ var roleInstanceId = roleInstance.Id;
+ var li1 = roleInstanceId.LastIndexOf(".");
+ var li2 = roleInstanceId.LastIndexOf("_");
+ var roleInstanceNo = roleInstanceId.Substring(Math.Max(li1, li2) + 1);
+
+ return new AzureRoleInfo
+ {
+ Index = Int32.Parse(roleInstanceNo),
+ Name = roleInstance.Role.Name
+ };
+ }
+
+ private class AzureRoleInfo
+ {
+ public string Name { get; set; }
+ public int Index { get; set; }
+ }
}
}
View
1 src/Microsoft.AspNet.SignalR.ServiceBus/Microsoft.AspNet.SignalR.ServiceBus.csproj
@@ -43,6 +43,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.WindowsAzure.ConfigurationManager.1.7.0.3\lib\net35-full\Microsoft.WindowsAzure.Configuration.dll</HintPath>
</Reference>
+ <Reference Include="Microsoft.WindowsAzure.ServiceRuntime, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />

0 comments on commit d9223d8

Please sign in to comment.