2.02.2011

Setting up WCF to use StructureMap

As I previously posted, I recently deployed a new web service using WCF. My team is using NHibernate as our ORM and using StructureMap to handle inversion of control and dependency injection. Obviously, we do not want to reinvent the wheel when deploying a WCF service so we needed to have StructureMap handle dependency injection for us. Jimmy Bogard came to our rescue with a post detailing how this can be done. However, there are a few things that can be trimmed from his example and a way to setup the service to work for all services in a project. The first thing that needs to be created is an implementation of IInstanceProvider.
public class StructureMapInstanceProvider : IInstanceProvider
{
    private Type _serviceType;

    public StructureMapInstanceProvider(Type serviceType)
    {
        this._serviceType = serviceType;
    }

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return ObjectFactory.GetInstance(_serviceType);
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return this.GetInstance(instanceContext, null);
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {
        //No cleanup required
    }
}
This is almost verbatim from Jimmy's post. So now we have a way that WCF can instantiate objects. We just need to tell WCF about it. This can be done by implementing the IServiceBehavior interface. Again, we use Jimmy's code:
public class StructureMapServiceBehavior : IServiceBehavior
{
    public void ApplyDispatchBehavior(ServiceDescription desc, ServiceHostBase host)
    {
        foreach (ChannelDispatcherBase cdb in host.ChannelDispatchers)
        {
            ChannelDispatcher cd = cdb as ChannelDispatcher;
            if (cd != null)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    ed.DispatchRuntime.InstanceProvider = 
                        new StructureMapInstanceProvider(desc.ServiceType);
                }
            }
        }
    }

    public void AddBindingParameters(ServiceDescription desc, ServiceHostBase host,  
                                     Collection endpoints, 
                                     BindingParameterCollection bindingParameters)
    {
    }

    public void Validate(ServiceDescription desc, ServiceHostBase host)
    {
    }
}
This ties our IInstanceProvider to every endpoint that we expose via our service. Jimmy then recommends extending ServiceHost and ServiceHostFactory and using those classes to attach the Factory to each service. I wanted to be able to configure this behavior once for the entire project. So I modified the StructureMapServiceBehavior class to extend BehaviorExtensionElement. This forced me to implement the following methods:
public class StructureMapInstanceProvider : IInstanceProvider
{
...
public override Type BehaviorType
{
    get { return this.GetType(); }
}

protected override object CreateBehavior()
{
    ObjectFactory.Initialize(cfg =>
    {
        cfg.Scan(scan =>
        {
            scan.WithDefaultConventions();
        });
    });
    return this;
}
...
}
This will initialize StructureMap. With this complete, I can use StructureMapServiceBehavior as a behavior in my Web.Config.
<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <StructureMapServiceBehavior />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add name="StructureMapServiceBehavior" 
             type="Services.StructureMapServiceBehavior, Services, Version=1.0.0.0, Culture=neutral"/>
      </behaviorExtensions>
    </extensions>
  </system.serviceModel>
Now StructureMap is loaded up and controls instantiation for any of my classes, just like in my MVC projects. This can be used with any dependency injection framework. Just modify the IInstanceProvider.GetInstance methods and the BehaviorExtensionElement.CreateBehavior method.

1.20.2011

Application Can't Find References

For the past several hours I have been trying to understand why a C# console application would not build. I'm using Visual Studio 2010. I have several other console applications that are working properly. However, this one kept claiming that it could not find two custom assemblies that I had referenced. I was getting
The type or namespace name 'StructureMap' could not be found (are you missing a using directive or an assembly reference?)
Of course, I checked repeatedly that the assemblies were in fact referenced. I even went as far as deleting and recreating the project, all to no avail. I finally found a stackoverflow post that addressed the problem. Turns out my custom assemblies are compiled targeting the .NET 4 Framework. My console application was only targeting the .NET 4 Client Profile. This caused the console application to lose track of the custom assemblies. Extremely frustrating as the error message was not indicative of the problem. Glad it was an easy fix though.

1.06.2011

Using NHibernate with Windows Communication Foundation (WCF)

Our team recently wanted to use WCF to create a new web service. We are using nHibernate as our ORM. We used a pattern described by Jeffery Palermo to integrate NHibernate into our MVC applications and were interested in doing something similar with WCF. Unfortunately, this method doesn’t work for WCF, at least not without the overhead of turning on AspNetCompatibilityRequirementsMode (just typing that hurts a little.) In my research, I discovered the IDispatchMessageInspector interface. This interface provides an AfterReceiveRequest and BeforeSendReply methods. This will allow me to open an NHibernate session before the message is processed and close it just before my reply is sent to the consumer. Additionally, because it is an interface, I can simply have my exisiting HttpModule implementation extend the IDispatchMessageInspector interface. My methods are implemented as follows:
public object AfterReceiveRequest(ref Message req, IClientChannel chan, InstanceContext context)
{
    context_BeginRequest(null, null);
    return null;
}
and
public void BeforeSendReply(ref Message reply, object correlationState)
{
    context_EndRequest(null, null);
}
This allows me to use the same code as my HttpModule. All that is left to do is tie the IDispatchMessageInspector to my service. We can do this by implementing the IServiceBehavior interface, we’ll call the class NHibernateWcfBehaviorExtension. I was lazy and did this on the same HttpModule class, multiple inheritance is awesome! The IServiceBehavior requires three methods, only one will actually do anything:
public void ApplyDispatchBehavior(ServiceDescription descriptoin, ServiceHostBase hostBase)
{
    foreach (ChannelDispatcher cd in hostBase.ChannelDispatchers)
    {
        foreach (EndpointDispatcher ed in cd.Endpoints)
        {
            ed.DispatchRuntime.MessageInspectors.Add(this);
        }
    }
}
This will load the message inspector on each of the service’s endpoints. Now all we need to do is tell WCF about this IServiceBehavior. This can be done by extending BehaviorExtensionElement. We’ll call our implementation NHibernateSessionStarter . This class should return the type and a new instance of NHibernateWcfBehaviorExtension. Once we have the BehaviorExtensionElement, we can use it to tell WCF about our IServiceBehavior in the Web.Config.
<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
        <!-- Service behavior is added here. -->
        <NHibernateSessionStarter />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <extensions>
    <behaviorExtensions>
      <!-- Service behavior is defined here. -->
      <add name="NHibernateSessionStarter" type="Infrastructure.NHibernateWcfBehaviorExtension, Infrastructure, Version=1.0.0.0, Culture=neutral" />
    </behaviorExtensions>
  </extensions>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Voila, WCF configured to use the same NHibernate startup and cleanup as our MVC apps.

12.23.2010

Using an NHibernate discriminator with a stored procedure

Recently I needed to split the results of a stored procedure into one of two classes based on one of the columns using NHibernate. Having recently discovered discriminators, I applied the discriminator tag to my mapping file and created my subclasses. (I've changed the names of the classes and columns, so please excuse the lame example.) I want to generate either a teacher or a student object for a specified semester from each row in the stored procedures result set. Teacher if the class_room value is not null, student otherwise. Initially I tried to accomplish this using a discriminator formula.

<hibernate-mapping  xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core">
  <class name="AbstractPerson" abstract="true">
    <id name="Id" />
    <property name="FirstName" />
    <property name="LastName" />
 <discriminator formula="case when class_room is null then 1 else 0 end" />
    <subclass discriminator-value="0" name="Student">
      <property name="DormRoom" />
    </subclass>
    <subclass discriminator-value="1" name="Teacher">
      <property name="Classroom" />
    </subclass>
  </class>
  <sql-query name="GetPeopleWithLocation" callable="true">
    <return class="AbstractPerson">
      <return-property name="FirstName" column="first_name" />
      <return-property name="LastName" column="last_name" />
      <return-property name="DormRoom" column="dorm_room" />
   <return-property name="ClassRoom" column="class_room" />
    </return>
    exec get_person_location :semester_id
  </sql-query>
</hibernate-mapping>

Of course, I had put the discriminator in the incorrect location and got the following error:

XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'discriminator' in namespace 'urn:nhibernate-mapping-2.2'
Doing some research, I realized that the discriminator tag needs to be directly under the id element. I moved it and executed my test again (you are doing TDD right?).

This time the query ran, but I got a rather cryptic error:

[SQL: exec get_person_location :semester_id] ---> System.IndexOutOfRangeException: clazz_0_
Based on previous experience with discriminators and some research, I believe this error was caused because NHibernate is attempting to add the discriminator formula to the sql statement that is being executed. This is obviously problematic when using a stored procedure. Back to the drawing board.

I continued researching and finally found the return-discriminator tag from the Hibernate documentation. This tag can be used in the return class to specify which column should be used as the discriminator. Now that sounds promising! Another visit to the mapping file yeilded another error:

XML validation error: The element 'return' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'return-discriminator' in namespace 'urn:nhibernate-mapping-2.2'.
I had placed the return-discriminator tag at the end of the return class. It must be the first element after the return tag.

The final mapping file looks like this:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core">
  <class name="AbstractPerson" abstract="true">
    <id name="Id" />
 <<discriminator column="class_room" />
    <property name="FirstName" />
    <property name="LastName" />
    <subclass discriminator-value="null" name="Student">
      <property name="DormRoom" />
    </subclass>
    <subclass discriminator-value="not null" name="Teacher">
      <property name="ClassRoom" />
    </subclass>
  </class>
  <sql-query name="GetPeopleWithLocation" callable="true">
    <return class="AbstractPerson">
      <return-discriminator column="class_room" />
      <return-property name="FirstName" column="first_name" />
      <return-property name="LastName" column="last_name" />
      <return-property name="DormRoom" column="dorm_room" />
   <return-property name="ClassRoom" column="class_room" />
    </return>
    exec get_person_location :semester_id
  </sql-query>
</hibernate-mapping>

Hope this saves someone a few minutes.