Monthly Archives: May 2013

ActiveMQ and NMS

Getting Started

ActiveMQ can be connected to in various ways. In this post I’m going to concentrate on NMS for .NET integration.

For the sample code I’ve created a console application and using nu-get have added the packages named Apache.NMS and Apache.NMS.ActiveMQ.

To begin with we need to run up ActiveMQ either locally or remotely (ensure you’ve noted down the connection string if you’re running remotely, for now I’ll assume the ActiveMQ server was started locally).

The first thing our client application must do is create both the connection and session object to allow us to interact with ActiveMQ, this is done using the following code (it’s pretty self explanatory)

IConnectionFactory factory = new ConnectionFactory();
using (IConnection connection = factory.CreateConnection())
{
   connection.Start();
   using(ISession session = connection.CreateSession())
   {
      // do something with the session
   }
}

Note: both the connection and session are disposable so don;t forget to dispose of them either directly by calling Close() or indirectly using the using statement.

The above code does not specify the endpoint for the connection, to handle a remote server (or one not on the default port on the local machine) we can use the following code as a replacement for the ConnectionFactory() code

Uri connecturi = new Uri("activemq:tcp://myserver:61616");
IConnectionFactory factory = new NMSConnectionFactory(connecturi);

Topics and Queues

Best to read the ActiveMQ documentation for a full explanation.

But basically a topic works on a publish and subscribe pattern. One or more subscriptions may exist to a topic and all will receive published messages. Whereas a queue is implemented as per load balancer semantics, in that each consumer will get a message until it’s been acknowledged.

Enumerating Topics and Queues

The following is pretty much taken from the source code accompanying the NMS source, I’ve removed the code for setting up the connection/session

public const string QUEUE_ADVISORY_DESTINATION = "ActiveMQ.Advisory.Queue";
public const string TOPIC_ADVISORY_DESTINATION = "ActiveMQ.Advisory.Topic";
public const string TEMPQUEUE_ADVISORY_DESTINATION = "ActiveMQ.Advisory.TempQueue";
public const string TEMPTOPIC_ADVISORY_DESTINATION = "ActiveMQ.Advisory.TempTopic";

public const string ALLDEST_ADVISORY_DESTINATION = QUEUE_ADVISORY_DESTINATION + "," +
                       TOPIC_ADVISORY_DESTINATION + "," +
                       TEMPQUEUE_ADVISORY_DESTINATION + "," +
                       TEMPTOPIC_ADVISORY_DESTINATION;

private void Enumerate(string destination, Action<DestinationInfo> action)
{
   IDestination dest = session.GetTopic(destination);
   using (IMessageConsumer consumer = session.CreateConsumer(dest))
   {
      IMessage advisory;

      while ((advisory = consumer.Receive(TimeSpan.FromMilliseconds(2000))) != null)
      {
         ActiveMQMessage am = advisory as ActiveMQMessage;
	 if (am != null & am.DataStructure != null)
 	 {
	    DestinationInfo info = am.DataStructure as DestinationInfo;
 	    if (info != null)
	    {
	       action(info);
	    }
	 }
      }
   }			
}

public void EnumerateQueues()
{
   Console.WriteLine("Listing all Queues on Broker:");
   Enumerate(QUEUE_ADVISORY_DESTINATION, info => 
                Console.WriteLine("   Queue: " + info.Destination));
   Console.WriteLine("Listing Complete.");
}

public void EnumerateTopics()
{
   Console.WriteLine("Listing all Topics on Broker:");
   Enumerate(TOPIC_ADVISORY_DESTINATION, info => 
                Console.WriteLine("   Topic: " + info.Destination));
   Console.WriteLine("Listing Complete.");
}

public void EnumerateDestinations()
{
   Console.WriteLine("Listing all Destinations on Broker:");
   Enumerate(ALLDEST_ADVISORY_DESTINATION, info =>
   {
      string destType = info.Destination.IsTopic ? "Topic" : "Qeue";
      destType = info.Destination.IsTemporary ? "Temporary" + destType : destType;
      Console.WriteLine("   " + destType + ": " + info.Destination);
   });
   Console.WriteLine("Listing Complete.");
}

I’ve also added the code for EnumerateDestinations which allows us to enumerate all both Queues and Topics.

Note the use of advisory messages, see Advisory Messages

Connection strings

Sample of alternate connection strings

activemq:tcp://activemqhost:61616
stomp:tcp://activemqhost:61613

Creating a producer

IDestination destination = SessionUtil.GetDestination(session, "queue://FOO.BAR");
using (IMessageConsumer consumer = session.CreateConsumer(destination))
{
   connection.Start();
   producer.DeliveryMode = MsgDeliveryMode.Persistent;
   poducer.RequestTimeout = TimeSpan.FromSeconds(10);

   // create message
   ITextMessage request = session.CreateTextMessage("Hello World!");
   request.NMSCorrelationID = "abc";
   request.Properties["myKey"] = "Cheddar";

   producer.Send(request);
}

Creating a consumer

IDestination destination = SessionUtil.GetDestination(session, "queue://FOO.BAR");
using (IMessageConsumer consumer = session.CreateConsumer(destination))
{
   connection.Start();  
   consumer.Listener += msg => 
   {
      ITextMessage message = msg as ITextMessage;
      if(message != null)
      {
         Console.WriteLine("Message Id: " + message.NMSMessageId);
         Console.WriteLine("Message text " + message.Text);
      }
   }
}

The above consumer code works in an event driven way and thus can be used as an async consumer an alternative would be to request data from the consumer in the following manner

ITextMessage message = consumer.Receive() as ITextMessage;
if(message != null)
{
   Console.WriteLine("Message Id: " + message.NMSMessageId);
   Console.WriteLine("Message text " + message.Text);
}

Note that ActiveMQ queues and topics can be created by simply creating a destination to use them, in the above case we’re creating a queue named FOO.BAR as in the following

queue://FOO.BAR

Instead of using SessionUtil.GetDestination(session, “queue://FOO.BAR”) we can also use session.GetQueue(“FOO.BAR”) and/or session.GetTopic(“FOO.BAR”) or session.GetDestination(“queue://FOO.BAR”) and/or session.GetDestination(“topic://FOO.BAR”).

Note: if using SessionUtil.GetDestination(session, “FOO.BAR”) i.e. without the queue/topic prefix the call defaults to a queue.

Don’t forget to cleanup

As mentioned earlier, one thing to remember is that the session and connection instances need to be disposed of preferably by calling the Close() method directly when no longer required.

Handling data in WCF headers (service-side)

So my previous post looked at adding data into the headers of the WCF service calls from the client-side perspective.

Ofcourse for this to be of any use we need the service to handle the extraction of this header data. So this post is mean’t to cover the basics of this. First we need to create the extension, so we inherit from BehaviorExtensionElement (just as we did on the client)

public class SecurityTokenEndpointBehaviourExtensionElement : BehaviorExtensionElement
{
   public override Type BehaviorType
   {
      get { return typeof(SecurityTokenServiceBehaviour); }
   }
   protected override object CreateBehavior()
   {
      return new SecurityTokenServiceBehaviour();
   }
}

Now we need to create an implementation of the IServiceBehavior (on the client this is the IEndpointBehavior)

public class SecurityTokenServiceBehaviour : IServiceBehavior
{
   public void Validate(ServiceDescription serviceDescription, 
                        ServiceHostBase serviceHostBase)
   {
   }

   public void AddBindingParameters(ServiceDescription serviceDescription, 
                         ServiceHostBase serviceHostBase, 
                         Collection<ServiceEndpoint> endpoints, 
                         BindingParameterCollection bindingParameters)
   {
   }

   public void ApplyDispatchBehavior(ServiceDescription serviceDescription, 
                         ServiceHostBase serviceHostBase)
   {
      foreach(ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
      {
         foreach(EndpointDispatcher epd in cd.Endpoints)
         {
            epd.DispatchRuntime.MessageInspectors.Add(new SecurityTokenMessageInspector());
         }
      }
   }
}

and finally we implement an IDispatchMessageInspector (on the client this would be the IClientMessageInspector)

public class SecurityTokenMessageInspector : IDispatchMessageInspector
{
   public object AfterReceiveRequest(ref Message request, 
                     IClientChannel channel, 
                     InstanceContext instanceContext)
   {
      return null;
   }

   public void BeforeSendReply(ref Message reply, object correlationState)
   {
   }
}

Okay so we’ve got all the code in place but now we need to tie this into WCF using the configuration (ofcourse if you prefer you can do this in code).

Just as we need to do in the client, we need to register the extension in an extensions element and again as per the client we then need to link the behaviour to the extension, see the App.config for this service below

<system.serviceModel>
   <behaviors>
      <serviceBehaviors>
         <behavior name="MyBehavior">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true" />
            <!--our extension -->
            <securityTokenExtension /> 
         </behavior>
      </serviceBehaviors>
   </behaviors>
   <services>
      <service behaviorConfiguration="MyBehavior" name="SimpleService.MyService">
        <endpoint address="MyService" binding="basicHttpBinding"
          bindingConfiguration="" contract="SimpleService.IMyService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9095/" />
          </baseAddresses>
        </host>
      </service>
   </services>
   <extensions>
      <behaviorExtensions>
        <add name="securityTokenExtension" 
           type="SimpleService.SecurityTokenEndpointBehaviourExtensionElement, SimpleService" />
      </behaviorExtensions>
   </extensions>
</system.serviceModel>

Finally, how do we get the value stored in the header on the service ?

The following code shows how we extract the value from the header named “myToken”, the generic argument shown in the method call GetHeader represents the type we expect the data to be.

public object AfterReceiveRequest(ref Message request, 
                  IClientChannel channel, 
                  InstanceContext instanceContext)
{
   int idx = request.Headers.FindHeader("myToken", String.Empty);
   if(idx >= 0)
   {
      Console.WriteLine(String.Format("Header value: {0}", 
                       request.Headers.GetHeader<string>(idx));
   }
   return null;
}

Adding data to WCF message headers (client-side)

When working with WCF you may find a need to add data to the message headers, for example maybe you wish to add some form of authentication token or the likes.

The first thing we need to do is create a BehaviorExtensionElement and add the relevant configuration to the App.Config.

public class SecurityTokenEndpointBehaviourExtensionElement : BehaviorExtensionElement
{
   public override Type BehaviorType
   {
      get { return typeof(SecurityTokenEndpointBehaviour); }
   }
   protected override object CreateBehavior()
   {
      return new SecurityTokenEndpointBehaviour();
   }
}

Now we need the corresponding entry in the config file for the extension and also the behaviour that our endpoint will map to

<behaviors>
   <endpointBehaviors>
      <behavior name="serviceEndpointBehaviour">
         <securityTokenExtension />
      </behavior>
   </endpointBehaviors>
</behaviors>
<extensions>
   <behaviorExtensions>
      <add name="securityTokenExtension"
	type="PutridParrot.SecurityTokenEndpointBehaviourExtensionElement, PutridParrot" />
   </behaviorExtensions>
</extensions>

As you can see the configuration basically tells WCF to use our BehaviorExtensionElement which then creates the behaviour. This behaviour then adds inspectors to the client as in the sample below

public class SecurityTokenEndpointBehaviour : IEndpointBehavior
{
   public void AddBindingParameters(ServiceEndpoint endpoint, 
                   BindingParameterCollection bindingParameters)
   {
   }

   public void ApplyClientBehavior(ServiceEndpoint endpoint, 
                   ClientRuntime clientRuntime)
   {
      clientRuntime.MessageInspectors.Add(new SecurityTokenMessageInspector());
   }

   public void ApplyDispatchBehavior(ServiceEndpoint endpoint, 
                    EndpointDispatcher endpointDispatcher)
   {
   }

   public void Validate(ServiceEndpoint endpoint)
   {
   }
}

So this code is very simple. In this case we’re simply adding a new message inspector and it’s this IClientMessageInspector implementation that adds the token to the WCF headers. As shown below

public class SecurityTokenMessageInspector : IClientMessageInspector
{
   public object BeforeSendRequest(ref Message request, IClientChannel channel)
   {
      request.Headers.Add(MessageHeader.CreateHeader("myToken", 
                          String.Empty, SecurityToken.Token));
      return null;
   }

   public void AfterReceiveReply(ref Message reply, object correlationState)
   {
   }
}

The code above adds a new message header with the name “myToken” and a value of SecurityToken.Token (just assume this is a singleton which creates and stores a security token).

Before this will work on the client we need to configure the end point to use the behaviorConfiguration defined previously, so now in the endpoint add

behaviorConfiguration="serviceEndpointBehaviour"

So the client configuration will look something like

<client>
   <endpoint address="http://localhost:9095/MyService" 
            binding="basicHttpBinding" contract="MyService.IMyService"                    
            behaviorConfiguration="serviceEndpointBehaviour"/>
</client>

.gitignore – Ignoring files and folders in GIT

I was using a GIT GUI today to add my ignores to the .gitignore file and couldn’t see an option on adding folders, so here’s a 10 second reminder on how to create a .gitignore by hand…

So, if you need to ignore files and/or folders in GIT simply create a .gitignore text file in the root of your repository and enter the files/folders in the following fashion

/StockControl.suo
/StockControl/bin

where the .suo is a file and bin is a folder.

Hence the format is

/<folder_path>/<filename>
/<folder_path>

Random Port Assignment in Azure

One thing to remember when developing a service on Azure is that the port defined in ServiceDefinition.csdef by the following

<InputEndpoint name="MyService" protocol="tcp" port="200" />

is not the same as the port your service should map to.

In other words when creating a socket within your service, Azure assigns a “random” port to your service, so it may have assigned your service the port 1234. So had you created your socket listening to port 200 (as defined in the config file) you will find your service doesn’t receive any traffic.

This is because the port defined in the configuration file is really aimed at the load balancer. It will open the defined port for traffic then route it to the “random” port assigned by Azure to your service. So to create a socket to listen for traffic we need to use the Service Runtime API in Azure to find the port assigned to the service.

RoleEnvironment re = RoleEnvironment.CurrentRoleInstance.InstanceEndPoints["MyService"];
IPEndPoint ep = re.IPEndpoint;

Socket socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ep);
socket.Listen(1000);

Storing binary data using Entity Framework

This is a real quick post, not my usual verbose ramblings :)

If you want to store binary data within a database column using Code First, then store the data as a byte array (see below).

public class SomeData
{
   // properties etc.

   // binary data, will be converted to a varbinary(max) in SQL Server
   public byte[] Data { get; set; }
}

Custom storing our ApplicationSettingsBase derived object

In my post I details how you can create an application settings class to read application settings and read/write user settings in a simple and consistent manner. But there’s more…

By default the user scoped properties are stored in a user.config file in the user’s profile. But we can actually customise where/how the settings are persisted by implementing code in a class derived from the SettingsProvider class. Firstly we need to apply the following

[SettingsProvider(typeof(MySettingsProvider))]

to the ApplicationSettingsBase derived class (in my previous post this would look like)

[SettingsProvider(typeof(ConfigurationSettingsProvider))]
public sealed class ConfigurationSettings : ApplicationSettingsBase
{
   // properties
}

Next we obviously need to implement the ConfigurationSettingsProvider. In fact Microsoft implemented this to create the LocalFileSettingsProvider class which by default is applied to our settings class (allowing us to read from the app.config and persist to the user.config).

By overriding methods such as Initialize, GetPropertyValues and SetPropertyValues we can get our settings from our own mechanism, for example a web service and persist changes back to the web service.

For further information checkout .

User application settings using ApplicationSettingsBase

There are many ways to store application settings, whether they’re roll your own XML or old INI profile file settings. Another option is to use a class derived from ApplicationSettingsBase.

ApplicationSettingsBase

To create your own configuration class, you derive it from the ApplicationSettingsBase class and then add properties to define the values you want to persist. On top of this we can override methods such as Save to allow us to customise the saving of values etc. A simple example is listed below (note: we’d often write this class as a singleton but I’ve removed extraneous code from the sample).

public sealed ProxySettings : ApplicationSettingsBase
{
   [UserScopedSetting]
   public string Host
   {
      get { return (string)this["Host"]; }
      set { this["Host"] = value; }
   }

   [UserScopedSetting, DefaultSettingValue("8080")]
   public int Port
   {
      get { return (int)this["Port"]; }
      set { this["Port"] = value; }
   }

   [ApplicationScopedSetting]
   public string ApplicationContext
   {
      get { return (string)this["ApplicationContext"]; }
   }
}

For a property to be stored or read using this mechanism, it must have the UserScopedSetting or the ApplicationScopedSettings attribute applied to it, otherwise it’s ignored from the perspective of persistence but obviously you can still use it as a non-persisted property.

ApplicationScopedSettings is used for application specific settings whereas UserScopedSetting is used for user specific settings (I know the names give this away but I thought I should make it clear). In the case of UserScopedSettings these, by default, are stored in the Documents and Settings\\Application Data in a user.config file whereas ApplicationScoreSettings are stored within the app.config for the application.

Note: The user scoped settings are not stored automatically, you need to call the ApplicationSettingsBase.Save method.

ApplicationScopedSettings are in essence read-only. Whilst we can have a getter and setter on the class (above), allowing us to change the application setting at run-time the altered values are not persisted to a storage mechanism.

Q. So what’s the point of ApplicationScopedSetting ?

A. You can add these values to your app.config and use it from your project in a similar way to using ConfigurationManager.AppSettings but obviously it can reside along with your user settings in a class as above and thus both the user and application scoped values are read in the same way, using this class. Underneath they’re separated into the user.config and app.config.

You can declare a default value to an ApplicationScopedSetting and then if you wanted to change the value in the app.config you’ll have a fallback default if the configuration is missing. The app.config will look something like

<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="MyApplication.ConfigurationSettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <MyApplication.ConfigurationSettings>
      <setting name="ProxyAdddress" serializeAs="String">
        <value>BlahBlah</value>
      </setting>
    </MyApplication.ConfigurationSettings>
  </applicationSettings>
</configuration>

Creating your application settings via a designer

Creating your ApplicationSettingsBase derived class is extremely useful if you have some more complex code (other than just read/write). But you can also create your own settings within Visual Studio and have it automatically created the source code for you. Simply go to the Properties folder in your solution and use the Settings.settings file, double clicking on this allows you to edit your properties and the Designer.cs will automatically be generated as a singleton and creates all the stuff as we’ve done previously ourselves. Better still this will generated the app.config code as well and save us having to remember the correct configuration xml.

You can ofcourse add more .settings files using the Add | New Item and select the Settings File.

Note: If you’re trying to track down where your configuration files might have been saved you can always call

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine(configuration.FilePath);
configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
Console.WriteLine(configuration.FilePath);
configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
Console.WriteLine(configuration.FilePath);

Partial classes and methods in C#

Partial classes, structs and interfaces

Partial classes, structs or even interfaces can be declared in C#. This mechanism allows us to split a class, struct or interface definition across multiple files which gives us some interesting possibilities.

For example, let’s same you reference a web service and you’ve generated a class like the following (I’ve removed extraneous base classes etc.)

public partial class Person
{
   private string string FirstNameField;
   private string string LastNameField;

   public string FirstName
   {
      get { return this.FirstNameField; }
      set
      {
         if((this.FirstNameField.Equals(value) != true))
         {
            this.FirstNameField = value;
            this.RaisePropertyChanged("FirstName");
         }
      }
   }

   public string LastName
   {
      get { return this.LastNameField; }
      set
      {
         if((this.LastNameField.Equals(value) != true))
         {
            this.LastNameField = value;
            this.RaisePropertyChanged("LastName");
         }
      }
   }
}

By default svcutil.exe will create the class as a partial. This allows us to now define an addition to the class that implements more functionality which can be run locally and will remain when/if we need to regenerate the proxies class above. We simply create another file (by convention this would be the .partial.cs, so in this case Person.partial.cs but of course this isn’t a requirement for partials to work).

public partial class Person
{
    public string FullName
    {
       get { return String.Format("{0} {1}", FirstName, LastName); }
    }
}

Partial Methods

Partial methods are methods whose signature can exist in one partial type but it implementation may defined in another. For example

public partial class Project
{
   partial void Run();
}

// in another file we might have
public partial class Project
{
   partial void Run()
   {
      Console.WriteLine("Running");
   }
}

A partial method must have the following:

  • The signature for the method must match in the partial type
  • The method can only return void
  • Partial methods are implicitly private and no access modifiers are allowed (even private is not allowed)

One of the key things about a partial method is the even if there’s no implementation of the method the code will still compile successfully and run. So you can use a partial method as a place holder for possible implementation at some later time if you wished and the code would still work. If no implementation exists for Run (in the above example) any calls to the Run method will be removed.

WCF Rest

Creating a REST style interface with WCF is fairly simple. The steps are listed briefly below, and in more depth below the list of steps.

1. Create your interface/service contract
2. Annotate the methods with WebGet or WebInvoke attributes
3. Create the implementation of the interface
4. Declare the endpoint within the configuration file (or in code). The binding should be webHttpBinding and should have an endpoint behaviour specificied with WebHttp
5. If need be declare the http baseAddress

In more depth

1. Create your interface/service contract
and
2. Annotate the methods with WebGet or WebInvoke attributes

[ServiceContract]
public interface IRestService
{
   [OperationContract]
   [WebGet(UriTemplate = "projects")]
   IList GetProjects();

   [OperationContract]
   [WebGet(UriTemplate = "execute/{projectName}")]
   void Execute(string projectName);
}

Note: Note to self. Don’t forget to put the ServiceContract attribute on the interface !

I’ve implemented both methods a GET methods. Assuming a baseAddress of http://localhost:8733/MyService/ to call these methods through a web browser we can simply type http://localhost:8733/MyService/projects and http://localhost:8733/MyService/execute/MyProject respectively.

3. Create the implementation of the interface

No need to list source code for this, simple implement the interface you created.

4. Declare the endpoint within the configuration file (or in code). The binding should be webHttpBinding and should have an endpoint behaviour specificied with WebHttp
and
5. If need be declare the http baseAddress

The key bit to getting everything to work is the configuration file. I’m self hosting for this example so implementing the code in the App.config

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="rest">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="StandardPlugins.MonitorService">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration=""
            contract="StandardPlugins.IRestService" behaviorConfiguration="rest">
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/MyService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
</system.serviceModel>

Note: The REST methods should be accessible via HTTP, hence the binding is webHttpBinding and the baseAddress is http. A behaviorConfiguration is required with the webHttp element.

Further reading

http://msdn.microsoft.com/en-us/magazine/dd315413.aspx#id0070050
http://msdn.microsoft.com/en-us/library/vstudio/bb412172(v=vs.90).aspx