Archive for the ‘Azure AppFabric’ Category.

Host WCF Services in IIS with Service Bus Endpoints

Update: To see how to leverage Windows Azure AppFabric Autostart to activate the WCF Service, please see AutoStart WCF Services to Expose them as Service Bus Endpoints.


Vishal Chowdhary, a Senior Test Lead on the Azure AppFabric team, recently posted a whitepaper on hosting WCF services with Service Bus endpoints from IIS.  This whitepaper provides two solutions to a (previously) significant challenge in hosting WCF services in IIS that connect to the Azure AppFabric Service Bus.

The primary challenge is activation. As Vishal writes, “For the on-premise WCF service to start receiving messages from the Service Bus in the cloud (aka Relay Service), the on-premises service opens an outbound port and creates a bidirectional socket for communication.  It connects to the Service Bus, authenticates itself, and starts listening to calls from the relay service before the client sends its requests.”  He goes on to say that “IIS/WAS relies on message-based activation & will launch the host only after the first request comes in.”  Consequently, until the first message is received by IIS the service will never establish a connection to the Service Bus; with no connection to the Service Bus, it will never receive a message.  A bit of a dilemma.

In the whitepaper, Vishal points out two ways to resolve this challenge:

  • IIS Application Warm-Up
  • ASP.NET 4.0 Auto-Start

In this post, I’m going to highlight exactly how to go about using IIS Application Warm-Up to get a WCF service hosted in IIS 7.5 to receive messages from the Service Bus.  This post borrows heavily from Visha’s whitepaper; I strongly suggest you spend the time to read the entire paper.

  1. If you’re using .NET 4.0, you must setup .NET 4.0 to work with the Azure AppFabric SDK.
  2. Create a new ASP.NET Web Application project called EchoSample in Visual Studio 2010 using .NET 4.0.
  3. To validate this approach, we want this project hosted in IIS.  Right-click the project and choose Properties.  Select the Web tab, and switch from Use Visual Studio Development Server to Use Local IIS Web server and click Create Virtual Directory.
  4. Add the Microsoft.ServiceBus reference from the “C:\%Program Files%\Windows Azure platform AppFabric SDK\V1.0\Assemblies\” folder.
  5. You have to create a custom BehaviorExtensionElement for the ServiceRegistrySettings to make the discoverability policy ‘Public’ in the configuration file.  Consequently, we need to create a class that we’ll call MyServiceRegistrySettingsElement that inherits the BehaviorExtensionElement.
public class MyServiceRegistrySettingsElement : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof(ServiceRegistrySettings); }
    }
    protected override object CreateBehavior()
    {
        return new ServiceRegistrySettings()
        {
            DiscoveryMode = this.DiscoveryMode,
            DisplayName = this.DisplayName
        };
    }
    [ConfigurationProperty("discoveryMode", DefaultValue = DiscoveryType.Private)]
    public DiscoveryType DiscoveryMode
    {
        get { return (DiscoveryType)this["discoveryMode"]; }
        set { this["discoveryMode"] = value; }
    }
    [ConfigurationProperty("displayName")]
    public string DisplayName
    {
        get { return (string)this["displayName"]; }
        set { this["displayName"] = value; }
    }
}
  1. Now, let’s add a new WCF Service called EchoService to the project.  Remove the existing method in the ServiceContract and create the following GetData method in the IEchoService.cs file.
[OperationContract]
string GetData(int value);
  1. Also, update the EchoService.svc.cs with the implementation of the GetData method.
public string GetData(int value)
{
    if (value < 0)
        throw new ApplicationException("Negative values not allowed!!!");

    Thread.Sleep(value);
    return string.Format("You entered: {0}", value);
}
  1. Now we need to update the web.config settings.  This is fairly extensive. Be sure and replace YOUR_NAMESPACE, YOUR_ISSUER_NAME, and YOUR_ISSUER_SECRET with your own values.
<system.serviceModel>
  <extensions>
    <behaviorExtensions>
      <add name="ServiceRegistrySettings"
            type="EchoSample.MyServiceRegistrySettingsElement, EchoSample,
                  Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </behaviorExtensions>
  </extensions>
  <services>
    <clear />
    <service behaviorConfiguration="MyServiceTypeBehavior"
              name="EchoSample.EchoService">
      <endpoint
         address="http://localhost/EchoSample/EchoService.svc/LocalEchoService"
         binding="basicHttpBinding"
         bindingConfiguration="BasicHttpConfig"
         name="Basic" contract="EchoSample.IEchoService" />
      <endpoint
         address="https://YOUR_NAMESPACE.servicebus.windows.net/EchoServiceHttp/"
         behaviorConfiguration="sharedSecretClientCredentials"
         binding="basicHttpRelayBinding"
         bindingConfiguration="HttpRelayEndpointConfig"
         name="RelayEndpoint"
         contract="EchoSample.IEchoService" />
      <endpoint
         address="sb://YOUR_NAMESPACE.servicebus.windows.net/EchoServiceNetTcp/"
         behaviorConfiguration="sharedSecretClientCredentials"
         binding="netTcpRelayBinding"
         bindingConfiguration="NetTcpRelayEndpointConfig"
         name="RelayEndpoint"
         contract="EchoSample.IEchoService" />
    </service>
  </services>
  <bindings>
    <basicHttpBinding>
      <binding name="BasicHttpConfig" />
    </basicHttpBinding>
    <!--service bus binding-->
    <basicHttpRelayBinding>
      <binding name="HttpRelayEndpointConfig">
        <security relayClientAuthenticationType="RelayAccessToken" />
      </binding>
    </basicHttpRelayBinding>
    <netTcpRelayBinding>
      <binding name="NetTcpRelayEndpointConfig">
        <security relayClientAuthenticationType="RelayAccessToken" />
      </binding>
    </netTcpRelayBinding>
  </bindings>
  <behaviors>
    <endpointBehaviors>
      <behavior name="sharedSecretClientCredentials">
        <transportClientEndpointBehavior credentialType="SharedSecret">
          <clientCredentials>
            <sharedSecret issuerName="YOUR_ISSUER_NAME"
                          issuerSecret="YOUR_ISSUER_SECRET" />
          </clientCredentials>
        </transportClientEndpointBehavior>
        <ServiceRegistrySettings discoveryMode="Public" />
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="MyServiceTypeBehavior">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>


At this point, once you compile and build the solution, the service will not automatically connect to the Service Bus; this is because IIS/WAS waits until the first service call to activate.  Consequently, if you load the WCF service in the browser it will activate the service and establish a connection to the Service Bus (you can confirm by checking https://YOUR_NAMESPACE.servicebus.windows.net/).  So, we’re close, but not yet there.

To get the service to automatically establish the connection to the Service Bus, we’ll use the Application Warm-Up extension for IIS 7.5.

  1. Download and install the Application Warm-Up extension for IIS 7.5.  This gives us the ability to proactively load and initialize processes before the first request arrives.  In addition to improving responsiveness it also gives us the ability to connect our WCF service to the Azure AppFabric Service Bus.
  2. In IIS, select your virtual application EchoSample.  Double-click the Application Warm-Up feature, and click Settings.  Check the Start Application Pool ‘ASP.NET v4.0’ when service started checkbox.

Application Warm-Up settings

  1. Add a new request to register EchoService.svc as a warm-up request for the application.  Right-click and choose Add Request.  Enter EchoService.svc, and click OK.  You should now see it in the Request URL list.

Application Warm-Up add request

Application Warm-Up

And that’s it!  The service is now automatically started and “warmed up” by IIS.  To test, recycle the Application Pool and restart the web site.  Then, hit your Service Bus endpoint and confirm that you’re services are running.

Publicly Listed Services

Now, even if we restart the computer, the WCF service will reestablish the connection to the Service Bus because IIS 7.5, through the Application Warm-Up Extensions, will automatically refresh.

Now, to complete the test, let’s build a quick Console application to connect to the WCF service via the Service Bus.

  1. Create a new Console Application project called Client.
  2. Add a reference to the Microsoft.ServiceBus and System.ServiceModel.
  3. Add a link to the IEchoService.cs file in the EchoSample project.  Right-click the project, choose Add Existing, and change Add to Add as Link.

Add Existing Item - Add As Link 

  1. Update the Program.cs file with the following code.  Be sure and replace YOUR_NAMESPACE, YOUR_ISSUER_NAME, and YOUR_ISSUER_SECRET with your own values.
class Program
{
    static void Main(string[] args)
    {
        // Determine the system connectivity mode based on the command line
        // arguments: -http, -tcp or -auto  (defaults to auto)
        ServiceBusEnvironment.SystemConnectivity.Mode = GetConnectivityMode(args);
        string serviceNamespace = "YOUR_NAMESPACE";
        string issuerName = "YOUR_ISSUER_NAME";
        string issuerSecret = "YOUR_ISSUER_SECRET";
        // create the service URI based on the service namespace
        Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb",
            serviceNamespace, "EchoServiceNetTcp");
        //Uri serviceUri = ServiceBusEnvironment.CreateServiceUri(
        //    "https", serviceNamespace, "EchoServiceHttp");
        // create the credentials object for the endpoint
        TransportClientEndpointBehavior sharedSecretServiceBusCredential =
            new TransportClientEndpointBehavior();
        sharedSecretServiceBusCredential.CredentialType =
            TransportClientCredentialType.SharedSecret;
        sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName =
            issuerName;
        sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret =
            issuerSecret;
        // create the channel factory loading the configuration
        //BasicHttpRelayBinding myBinding = new BasicHttpRelayBinding();
        NetTcpRelayBinding myBinding = new NetTcpRelayBinding();
        EndpointAddress myEndpoint = new EndpointAddress(serviceUri);
        ChannelFactory<IEchoService> channelFactory =
            new ChannelFactory<IEchoService>(myBinding, myEndpoint);
        // apply the Service Bus credentials
        channelFactory.Endpoint.Behaviors.Add(sharedSecretServiceBusCredential);
        // create and open the client channel
        IEchoService channel = channelFactory.CreateChannel();
        Console.WriteLine("Enter text to echo (or [Enter] to exit):");
        string input = Console.ReadLine();
        while (input != String.Empty)
        {
            try
            {
                Console.WriteLine("Server echoed: {0}",
                    channel.GetData(Int32.Parse(input)));
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
            input = Console.ReadLine();
        }
        if (((IClientChannel)channel).State != CommunicationState.Faulted)
            ((IClientChannel)channel).Close();
        else
            ((IClientChannel)channel).Abort();
        channelFactory.Close();
    }
    static ConnectivityMode GetConnectivityMode(string[] args)
    {
        foreach (string arg in args)
        {
            if (arg.Equals("/auto", StringComparison.InvariantCultureIgnoreCase)
                 || arg.Equals("-auto", StringComparison.InvariantCultureIgnoreCase))
            {
                return ConnectivityMode.AutoDetect;
            }
            else if (arg.Equals("/tcp", StringComparison.InvariantCultureIgnoreCase)
                 || arg.Equals("-tcp", StringComparison.InvariantCultureIgnoreCase))
            {
                return ConnectivityMode.Tcp;
            }
            else if (arg.Equals("/http", StringComparison.InvariantCultureIgnoreCase)
                 || arg.Equals("-http", StringComparison.InvariantCultureIgnoreCase))
            {
                return ConnectivityMode.Http;
            }
        }
        return ConnectivityMode.AutoDetect;
    }


When you run the console application, it will connect to your WCF service through the Service Bus.  Run it to validate. You can also uncomment some of the code to use BasicHttpRelayBinding instead of NetTcpRelayBinding to try out a different configuration.

The ability to host a WCF service in IIS that exposes itself on the Service Bus is a significant milestone.  This opens up a number of fantastic opportunities and scenarios that otherwise would have been extremely difficult to accomplish.

Using the .NET Framework 4.0 with the Azure AppFabric SDK

The other day I attempted to build a sample application that communicated with the Azure AppFabric Service Bus by creating a Console application targeting the .NET Framework 4.0.  After adding a reference to Microsoft.ServiceBus I was bewildered to see that my Service Bus bindings in the system.ServiceModel section were not recognized.

I soon realized that the issue was the machine.config file.  When you install the Azure AppFabric SDK the relevant WCF extensions are added to the .NET Framework 2.0 machine.config file, which is shared by .NET Framework 3.0 and 3.5.  However, .NET Framework 4.0 has its own machine.config file, and the SDK will not update the WCF extensions.

Fortunately, there’s an easy solution to this issue: use the CLR’s requiredRuntime feature.

  1. Create a configuration file named RelayConfigurationInstaller.exe.config in the “C:\%Program Files%\Windows Azure platform AppFabric SDK\V1.0\Assemblies\” folder with the following code:
<?xml version ="1.0"?><configuration>  <startup>    <requiredRuntime safemode="true"       imageVersion="v4.0.30319"       version="v4.0.30319"/>  </startup></configuration>
  1. Open up an elevated Visual Studio 2010 Command Prompt, browse to the directory, and run: RelayConfigurationInstaller.exe/ i

Your .NET Framework 4.0 machine.config file will now have the required configuration settings for the Service Bus bindings.  Thanks to Vishal Chowdhary for the insight!

Release the hounds – Multicasting with Azure AppFabric

On an email thread today, someone was looking for suggestions on how to start a job simultaneously across multiple worker roles running in Windows Azure.  For example, image you have ten worker roles already running and, through the command of an admin or user, you want to “release the hounds!”

Definitely an interesting scenario, and many different ways to approach it.  Initial ideas and thoughts centered around using Windows Azure storage tables or blobs – in fact, Steve Marx quickly threw out some pseudo code highlighting a reasonable way to approach the problem:

   1: while (blob.DownloadText() != “RELEASE THE HOUNDS!”)
   2:     Thread.Sleep(TimeSpan.FromSeconds(1));
   3: // do the actual work

Then to release:

   1: blob.UploadText(“RELEASE THE HOUNDS!”);

You could definitely take this approach and have success.

Of course, to me this scenario screamed multicasting with NetEventRelayBinding.

NetEventRelayBinding supports multiple listeners on the same URI, which means that you can have 1 or 1000 worker roles in Windows Azure all listening to the same URI – this gives you the ability to push out events to all listeners, as any message sent by a client gets distributed to all the listeners.

Clemens Vasters sums NetEventRelayBinding it up nicely on his blog:

The NetEventRelayBinding doesn’t have an exact counterpart in the standard bindings. This binding provides access to the multicast publish/subscribe capability in the Relay. Using this binding, clients act as event publishers and listeners act as subscribers. An event-topic is represented by an agreed-upon name in the naming system. There can be any number of publishers and any number of subscribers that use the respective named rendezvous point in the Relay. Listeners can subscribe independent of whether a publisher currently maintains an open connection and publishers can publish messages irrespective of how many listeners are currently active – including zero. The result is a very easy to use lightweight one-way publish/subscribe event distribution mechanism that doesn’t require any particular setup or management.

So, the architecture might look something like this:

Apologies for the crappy graphic

In this scenario, an admin sitting on a laptop can send a message to the Service Bus, which in turn relays the message to all the listeners.  When the worker roles receive the message they will “release the hounds” and process whatever it is they need to process.

Note: this approach is just as valid for listeners that don’t reside in Windows Azure.  For example, if you have an application that is distributed across PCs and you want to send every client a message (without implementing some form of polling) this is the perfect approach.

So, without further ado, here’s the code to release the hounds!

Now, a few comments on the code:

  • I wrote this using Visual Studio 2010 RTM. Your mileage may vary.
  • Make sure you have the Windows Azure SDK and the Windows Azure AppFabric SDK.
  • The first thing you’re going to want to do is search on YOURSERVICENAME and YOURISSUERSECRET and replace with your own values.
  • It’s initially configured to run locally.  Just hit F5.
  • When you run locally, three things will launch:
    1. The local development fabric, with two worker roles.
    2. A Windows Forms application with a big button (hey, it’s better than a console window!).
    3. A console window that displays traces from all your worker roles.  This is especially useful for getting information from your worker roles once you’ve deployed to Windows Azure.  I’ll blog on this another time.
  • When you eventually deploy to Windows Azure, but sure to uncomment the <extensions> and <bindingExtensions> sections in the App.config, as the Windows Azure AppFabric SDK is not installed in Windows Azure, and it won’t understand NetEventRelayBinding.

I personally think this is a pretty neat solution, and can enable a lot of advanced scenarios.  I’d love to hear your feedback and comments.

What is the Azure AppFabric?

If you take a look at the official Windows Azure platform website, you’ll see two definitions for the Windows Azure platform AppFabric (hereafter referred to as the Azure AppFabric) prominently called out:

  • “… connects cloud services and on-premises applications.”
  • “… helps developers connect applications and services in the cloud or on-premises.”

While the purpose of the Azure AppFabric seems clear to me – enable developers to connect applications and services – there are a couple things that generally cause confusion: execution and branding.  I plan to talk about how to use the Azure AppFabric quite a bit in the future, but in this post I want to address the branding.

The Azure AppFabric has been rebranded numerous times.  This isn’t surprising given that it has largely been a community technology preview, but it has lead to some confusion.

So, some brief history …

Note: this is based entirely on my cyber-sleuthing and personal experience.  I’m sure I have gaps and perhaps an inadvertent inaccuracy, so as I get corrected I’ll update.  I didn’t join Microsoft until early 2008, so the early days of the Azure AppFabric precedes my Microsoft employment.

In April 2007, the BizTalk Server team announced that the CTP release of BizTalk Services was live.  They had created an Internet Services Bus (ISB) that allowed developers to create “Internet scale composite applications more rapidly.”  Clemens Vasters described this new ISB in a post.  Later, in July 2007, the BizTalk Server team talked about Hosted Workflows in BizTalk – an exciting extension to the ISB announcement.  Over time, Access Control was added into the mix as well.

Soon, word of Project Zurich started hitting the airways.  Mary-Jo Foley wrote about “’Zurich,’ Microsoft’s elastic cloud” back in July 2008, describing it as an initiative to “extend Microsoft’s .NET application development technologies to the Internet ‘cloud.’” Close, but not quite right.  My first introduction to Project Zurich came while working on a project with RedPrairie on a supply chain proof-of-concept, that ultimately culminated in a Bob Muglia keynote demonstration at PDC 2008 (around 59 minutes).

At the Professional Developers Conference (PDC) 2008 the platform was rebranded .NET Services and included as part of the Azure Services Platform.  You can actually still see some of the .NET Services branding on this BizTalk Service page.  By the fall of 2008, .NET Services had emerged as a mature platform (even though still in CTP) consisting of an Internet Service Bus, an Access Control Service (ACS), and Workflow Service.  In June 2009, the .NET Services team announced that they were pulling the Workflow Service.  As Windows Workflow Foundation in .NET 4.0 evolved, it was clear that most customers wanted Workflow Services to also follow to the .NET 4.0 model (not .NET 3.5), which it was not.  Consequently, .NET Services team pulled workflow and focused on the ISB and ACS.

At PDC 2009, .NET Services went through it’s most recent branding change, and was eventually launched in 2010 as the Windows Azure platform AppFabric.  Of course, this is a really long name, so most people just end up saying Windows Azure AppFabric or just Azure AppFabric

The biggest challenge I see today with the name is that, at PDC 2009, we also rebranded “Dublin” and “Velocity” as the Windows Server AppFabric – almost too much name overloading, although there are some good reasons for it that will emerge over time.  To make things clear, I’ll always say either Azure AppFabric or Server AppFabric.

If you really take a look at how this all has evolved, you can start to see how Microsoft’s cloud platform strategy has evolved over the last several years.

So, where does this leave us?

In my opinion, it leaves us with a technology that is a key differentiator in Microsoft’s cloud platform.  I’m not just saying this – I really believe it, or I wouldn’t be moving my family up to Redmond so that I can focus on it.

In closing, let’s be clear on two things – in Azure AppFabric, there’s both a Service Bus and Access Control Service.

The Service Bus is an Internet-scale enterprise service bus that makes it easy to connect applications over the Internet. Services that register on the Service Bus can easily be discovered and accessed across any network topology.

The Access Controls Service helps you build federated authorization into your applications and services, without the complicated programming that is normally required to secure applications that extend beyond organizational boundaries.

Okay, now that I’ve spent a little time  covering some history and the past, expect to see a major focus on what you can do today – and lots of code and examples.

Hope this helps.