Skip to content

Instantly share code, notes, and snippets.

@JoshClose
Created November 15, 2011 05:20
Show Gist options
  • Save JoshClose/1366231 to your computer and use it in GitHub Desktop.
Save JoshClose/1366231 to your computer and use it in GitHub Desktop.
Setting Up WCF with Multiple Services and Multiple Databases Using NHibernate and Ninject 3
using System;
using System.ServiceModel.Configuration;
namespace Wnn.Service.Host
{
public class NHibernateBehaviorExtensionElement : BehaviorExtensionElement
{
protected override object CreateBehavior()
{
return new NHibernateServiceBehavior();
}
public override Type BehaviorType
{
get { return typeof( NHibernateServiceBehavior ); }
}
}
}
using System;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
namespace Wnn.Service.Host
{
public class NHibernateServiceBehavior : Attribute, 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( var channelDispatcherBase in serviceHostBase.ChannelDispatchers )
{
var channelDispatcher = channelDispatcherBase as ChannelDispatcher;
if( channelDispatcher == null )
{
continue;
}
foreach( var ed in channelDispatcher.Endpoints )
{
ed.DispatchRuntime.InstanceProvider = new NHibernateInstanceProvider( serviceDescription.ServiceType );
}
}
}
}
}
namespace Wnn.Service.Host.Console
{
class Program
{
static void Main( string[] args )
{
var service = new ServiceHostEngine();
service.Start();
System.Console.WriteLine( "Press any key to stop the services" );
System.Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment