In this post I will highlight what the different pain points are when hosting a WCF Service in Windows Azure with Service Bus Endpoints (e.g. using HttpRelayBinding)
Including Microsoft.ServiceBus in your deployment
As many people know already, the Windows Azure AppFabric SDK is not installed on Windows Azure. Which means that you won’t have the Microsoft.ServiceBus.dll once you deploy your solution to Windows Azure. This first problem is easily fixed by setting the ‘Copy Local’ property of this reference to true.
HttpRelayBinding error
Now chances are you have a web.config to accompany your svc file that uses one of the relay bindings that come with the AppFabric SDK.
<service name="test">
<endpoint name="PublishServiceEndpoint"
contract="WCFServiceWebRole1.IService1"
binding="basicHttpRelayBinding" />
</service>
Once you deploy and navigate to your svc you will get an error saying ….

Of course you can modify your web.config to include the necessary extensions manually, as specified in this post, but it won’t work anymore on your dev machine, since installing the AppFabric SDK already updated your machine.config with all the extensions. I wanted a solution where the machine.config was updated on Windows Azure prior to executing my code.
Using Startup Tasks
Luckily the AppFabric SDK comes with a tool called RelayConfigurationInstaller.exe which installs the Machine.config settings necessary for the Service Bus bindings to be supported in App.config. Now this just screams Startup Task.
So I went ahead and created a new solution and added the RelayConfigurationInstaller.exe to the project along with a RelayConfigurationInstaller.exe.config file which looks like this:
?xml version ="1.0"?>
<configuration>
<startup>
<requiredRuntime safemode="true" imageVersion="v4.0.30319"
version="v4.0.30319"/>
</startup>
</configuration>
I also added a Startup.cmd file which simply calls RelayConfigurationInstaller.exe /i. Then went into the ServiceDefinition.csdef and added the Startup Task with the call to Startup.cmd. This is what my project looks like so far:

Deploy…waiting 15 minutes… and no difference, I still get the same error, what went wrong?
Installing Microsoft.ServiceBus in the GAC
I found out that executing my startup task actually resulted in an error:

Which means that the Microsoft.ServiceBus.dll should be installed in the GAC prior to calling the executable.
Now this presents a problem, there is no Windows SDK installed on Windows Azure, so we can’t execute a gacutil to install the assembly. However I discovered that you can use the System.EnterpriseServices library to publish a dll into the GAC. Time to include powershell in my startup task.
I’ve created this ps script that installs the Microsoft.ServiceBus.dll in the GAC and added it to my project:
BEGIN {
$ErrorActionPreference = "Stop"
if ( $null -eq ([AppDomain]::CurrentDomain.GetAssemblies() |? { $_.FullName -eq "System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" }) )
{
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | Out-Null
}
$publish = New-Object System.EnterpriseServices.Internal.Publish
}
PROCESS {
$dir = [Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath
$assembly = Join-Path ($dir) "Microsoft.ServiceBus.dll"
if ( -not (Test-Path $assembly -type Leaf) )
{
throw "The assembly '$assembly' does not exist."
}
if ( [System.Reflection.Assembly]::LoadFile($assembly).GetName().GetPublicKey().Length -eq 0 )
{
throw "The assembly '$assembly' must be strongly signed."
}
Write-Output "Installing: $assembly"
$publish.GacInstall($assembly)
}
next I’ve modified the startup.cmd file to first call the ps script and then call the RelayConfigurationInstaller.exe to modify the machine.config with the needed bindings. This is what the final cmd file looks like:
1: powershell.exe Set-ExecutionPolicy RemoteSigned -Force
2: powershell.exe .\Startup\gacutil.ps1
3: Startup\RelayConfigurationInstaller.exe /i
Caveats
- Make sure you’ve selected osFamily=”2” in your ServiceConfiguration.cscfg file, since this gives you Windows Server 2008 R2, needed for caveat 2.
- If you want to execute powershell scripts, you have to set the ExecutionPolicy to RemoteSigned (see line 1 of startup.cmd)
Wouter Seye, Codit
cc791c97-b552-4197-bac3-c1bbfeb37d18|0|.0