Hosting a WCF service in a unit test - wcf

I'm wanting to unit test my custom ServiceHostFactory. Unfortunately, I get this InvalidOperationException when calling CreateServiceHost:
'ServiceHostFactory.CreateServiceHost' cannot be invoked within the current hosting environment. This API requires that the calling application be hosted in IIS or WAS.
I can work around this by refactoring my class such that it exposes a public method that can be directly invoked by the unit test instead of using its inherited public interface; I hate to change my interface just for the sake of a unit test. I also see another SO answer that recommends spawning a Cassini host, but I'd hate to complicate my unit tests in this manner.
Is there a way to work around this ServiceHostFactory limitation without resorting to these measures?

I figured out the issue. In my custom ServiceHostFactory, I had only overridden the protected method, CreateServiceHost(Type serviceType, Uri[] baseAddresses). By overriding the public CreateServiceHost(string constructorString, Uri[] baseAddresses) method, I was able to construct the service host factory with no issues.
Before:
public class MyServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
// ...
}
}
After:
public class MyServiceHostFactory : ServiceHostFactory
{
public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
{
return this.CreateServiceHost(typeof(MyService), baseAddresses);
}
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
// ...
}
}

Here is my solution to this problem (I am not 100% sure whether we should be doing this).
In the set up of the unit test fixture, set up a hosting environment (NUnit example):
[TestFixtureSetUp]
public void TestFixtureSetup()
{
if(!HostingEnvironment.IsHosted)
{
// The instance constructor hooks up the singleton hosting environment, ewww...
new HostingEnvironment();
// Check the hosting environment is fully initialized
ServiceHostingEnvironment.EnsureInitialized();
}
}
Then you should be free to use your custom ServiceHostFactory from inside of your unit tests:
[Test]
public void ServiceHostIsCorrect()
{
// Arrange
var serviceType = typeof (string);
var factory = new UnityServiceHostFactory();
// Act
var serviceHost = factory.CreateServiceHost(serviceType.AssemblyQualifiedName, new Uri[] {});
// Assert
Expect(serviceHost, Is.TypeOf<UnityServiceHost>());
var unityServiceHost = (UnityServiceHost)serviceHost;
Expect(unityServiceHost.Description.ServiceType, Is.EqualTo(serviceType));
}

Related

Ninject WCF extention with already existing ServiceHostFactory and ServiceHost

We have a difficulty to use Ninject wcf extensions. We host our services in IIS and using svc files. We have read a lot of posts describing usage of NinjectServiceHostFactory(in svc files) with NinjectHttpApplication (in global.asax file) and it seems very simple.
Our problem is that we already have our custom ServiceHostFactory and ServiceHost.
Can someone point us what is the right way combining our ServiceHostFactory with NinjectServiceHostFactory?
Our svc files point to the our custom ServiceHostFactory.
UPDATE
It seems that we managed to implement it.
Our ServiceHostFactory implementation:
public class CustomServiceHostFactory : NinjectServiceHostFactory
{
protected override Type ServiceHostType
{
get
{
return typeof(CustomServiceHost<>);
}
}
}
Our ServiceHost implementation:
public class CustomServiceHost<T> : NinjectAbstractServiceHost<T>
{
public CustomServiceHost(IServiceBehavior serviceBehavior, T serviceType, params Uri[] baseAddresses) : base(serviceBehavior, serviceType, baseAddresses)
{
...
}
protected override void OnOpening()
{
...
base.OnOpening();
}
}

Resolving the same interface with Unity from two different assemblies in a single custom WCF Service Host Factory

I am building a POC that aims to demonstrate how a dependency hierarchy can be constructed from within a WCF service with Unity and keep all assemblies in an application completely loosely coupled.
What I did is to create the following class libraries:
Data Access Layer:
- One assembly with the repository interface.
- One assembly with the implementation of this interface that pretends to access a DB.
- One assembly with the implementation of this interface that pretends to access XML docs.
Business Layer:
- One assembly with the business object interface.
- One assembly with the implementation of this interface that receives on its constructor the repository interface.
Service Layer:
- One assembly with the service interface.
- One assembly with the implementation of this interface that receives on its constructor the business object interface.
Finally I created an assembly with a Service Host Factory, a Service Host, and an Instance Provider that will be in charge of creating the dependency hierarchy. The code looks like so:
public class UnityServiceHostFactory : ServiceHostFactory
{
private readonly UnityContainer _container;
public UnityServiceHostFactory()
{
_container = new UnityContainer();
new ContainerConfigurator().Configure(_container);
}
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new UnityServiceHost(_container, serviceType, baseAddresses);
}
}
public class ContainerConfigurator
{
public void Configure(UnityContainer container)
{
container.RegisterType<IInvoiceRepository, InvoiceRepository>("dbInvoiceRepository");
container.RegisterType<IInvoiceRepository, XmlInvoice>("xmlInvoiceRepository");
container.RegisterType<IInvoiceFinder, InvoiceFinder>();
}
}
public class UnityServiceHost : ServiceHost
{
public UnityServiceHost(UnityContainer container, Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
if(container == null) throw new ArgumentNullException("container");
var contracts = ImplementedContracts.Values;
foreach (var c in contracts)
{
var instanceProvider = new UnityInstanceProvider(container, serviceType);
c.Behaviors.Add(instanceProvider);
}
}
}
public class UnityInstanceProvider : IInstanceProvider, IContractBehavior
{
private readonly UnityContainer _container;
private readonly Type _serviceType;
public UnityInstanceProvider(UnityContainer container, Type serviceType)
{
if (container == null) throw new ArgumentNullException("container");
if (serviceType == null) throw new ArgumentNullException("serviceType");
_container = container;
_serviceType = serviceType;
}
public object GetInstance(InstanceContext instanceContext)
{
return GetInstance(instanceContext, null);
}
public object GetInstance(InstanceContext instanceContext, Message message)
{
return _container.Resolve(_serviceType);
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
_container.Teardown(instance);
}
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
dispatchRuntime.InstanceProvider = this;
}
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
}
I am testing this with a console application, I instantiate the service proxy and make the call to its method but, since both registrations are named Unity does not know which one to instantiate. If I remove the name from either of them it gets resolved successfully.
Basically I would like to be able to do something like this:
static void Main(string[] args)
{
//First call, want it to resolve to the InvoiceRepository concrete type
//new InvoiceService(new InvoiceFinder(new InvoiceRepository))
var invoiceService1 = new InvoiceProxy();
var response1 = invoiceService1.GetSumarizedInvoiceBy(new InvoiceRequest(1));
//Second call, want it to resolve to the XmlInvoice concrete type
//new InvoiceService(new InvoiceFinder(new XmlInvoice))
var invoiceService2 = new InvoiceProxy();
var response2 = invoiceService2.GetSumarizedInvoiceBy(new InvoiceRequest(2));
}
Notice how invoiceService1 and invoiceService2 are two different instances of the same service but the dependency within its own dependency was resolved differently for both.
What do I have to do so that I can tell Unity which repository to instantiate when I am either instantiating the service proxy or calling its method?
Thanks for your help.
Pass the name you gave Unity in the RegisterType<>() call to Resolve<>().
From Resolving an Object by Type and Registration Name
// Create container and register types
IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType(typeof(MyServiceBase), typeof(DataService), "Data");
myContainer.RegisterType(typeof(MyServiceBase), typeof(LoggingService), "Logging");
// Retrieve an instance of each type
MyServiceBase myDataService = (MyServiceBase)myContainer.Resolve(typeof(MyServiceBase), "Data");
You are using the generic extension methods that are equivalent to this example.

How to add OperationBehavior for all operations on IIS hosted WCF service?

I have a custom OperationBehavior. I would like to apply it for all operations at once.
Unfortunately, OperationBehaviors cannot be configured per entire service or in web.config.
When hosting WCF service in a test application, I can do the following:
foreach (var ep in _serviceHost.Description.Endpoints)
{
foreach (OperationDescription od in ep.Contract.Operations)
{
od.Behaviors.Add(new MyOperationBehavior());
}
}
_serviceHost.Open();
But how do I do it in a IIS hosted web application?
I tried to get OperationContext.Current.Host.Description.Endpoints in Application_Start but of course OperationContext.Current is not available before any operation has started, so my approach fails.
You can use a ServiceHostFactory to do that. With it, you can get access to the OM prior to the service being opened.
This is an example:
public class MyFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);
foreach (var ep in host.Description.Endpoints)
{
foreach (OperationDescription od in ep.Contract.Operations)
{
od.Behaviors.Add(new MyOperationBehavior());
}
}
return host;
}
}
And you can get more information about service host factories at http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx
At the end I found an alternative solution: use a contract behavior which injects any other behvaior as needed. Like this:
public class InjectAllOperationsBehavior : Attribute, IContractBehavior
{
private IOperationBehavior _operationBehavior = null;
public InjectAllOperationsBehavior(Type operationBehaviorType)
{
_operationBehavior =
(IOperationBehavior)Activator.CreateInstance(operationBehaviorType);
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
foreach (OperationDescription opDescription in contractDescription.Operations)
{
opDescription.Behaviors.Add(_operationBehavior);
}
}
... other IContractBehavior methods can be left empty. You might want also to use the ApplyClientBehavior method with the same code from the ApplyDispatchBehavior method.

How can I capture WCF Service endpoint

I'm implementing a WCF service hosted by IIS, that impersonates the caller. When I have the service endpoint configuration in the Web.config-file everything works as intended.
I want to set the service endpoint programmatically but I'm missing something because the caller is not impersonated (the endpoint works fine except for that little detail). Is there any way I can capture the service endpoint created from web.config in code so that when debugging I can find what the difference is between this one and the one I create programmatically?
Thanks,
Christian
You can use the default service host factory to access the endpoint from web.config in your code (and possibly attach a debugger to the IIS process to see what it contains).
public class MyServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new MyServiceHost(serviceType, baseAddresses);
}
}
public class MyServiceHost : ServiceHost
{
public MyServiceHost(Type serviceType, Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
}
protected override void OnOpening()
{
// At this point you have access to the endpoint descriptions
foreach (var endpoint in this.Description.Endpoints)
{
Console.WriteLine("Endpoint at {0}", endpoint.Address.Uri);
Binding binding = endpoint.Binding;
BindingElementCollection elements = binding.CreateBindingElements();
foreach (var element in elements)
{
Console.WriteLine(" {0}", element);
}
}
base.OnOpening();
}
}
And in the .svc file, specify the Factory="YourNamespace.MyServiceHostFactory" attribute.

Specifying a WCF binding when using ServiceRoute

I am currently registering a WCF service using the following code:
var factory = new DefaultServiceHostFactory();
RouteTable.Routes.Add(new ServiceRoute("XXXEndPoint", factory, IXXXEndPoint)));
This is all well and good however I also need to change the MaxStringContentLength property of the reader quota settings. It appears that the default value of 8192 is used, regardless of my attempts to change this and I guess this is from the DefaultServiceModel?
Are there any suitable hooks to override this setting on the DefaultServiceModel, or should I be deriving my own service host/model classes, or am I going about this the wrong way?
Any suggestions appreciated.
EDIT: Please note that the configuration of the binding must be performed programatically (not via configuration files).
Thanks
You can make it by extending host factory.
By little modification of code bellow you can pass an additional parameter to WCFServiceHostFactory to set it from Global.asax
I have used the classes bellow to always set it to Int32.MaxValue
//in Global.asax
routes.Add(new ServiceRoute(routePrefix,
new WCFServiceHostFactory(),
serviceType));
//WCFServiceHostFactory.cs
namespace MyServices.MyService
{
public class WCFServiceHostFactory:WebServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
WCFServiceHost host = new WCFServiceHost(serviceType, baseAddresses);
return host;
}
}
}
//WCFServiceHost.cs
namespace MyServices.MyService
{
public class WCFServiceHost:WebServiceHost
{
public WCFServiceHost(): base ()
{
}
public WCFServiceHost (object singletonInstance, params Uri [] baseAddresses)
: base (singletonInstance, baseAddresses)
{
}
public WCFServiceHost(Type serviceType, params Uri[] baseAddresses)
: base (serviceType, baseAddresses)
{
}
protected override void OnOpening ()
{
base.OnOpening ();
foreach (Uri baseAddress in BaseAddresses) {
bool found = false;
foreach (ServiceEndpoint se in Description.Endpoints)
if (se.Address.Uri == baseAddress)
{
found = true;
((WebHttpBinding)se.Binding).ReaderQuotas.MaxStringContentLength = Int32.MaxValue;//here you set it and also set it below
}
if (!found) {
if (ImplementedContracts.Count > 1)
throw new InvalidOperationException ("Service '"+ Description.ServiceType.Name + "' implements multiple ServiceContract types, and no endpoints are defined in the configuration file. WebServiceHost can set up default endpoints, but only if the service implements only a single ServiceContract. Either change the service to only implement a single ServiceContract, or else define endpoints for the service explicitly in the configuration file. When more than one contract is implemented, must add base address endpoint manually");
var enumerator = ImplementedContracts.Values.GetEnumerator ();
enumerator.MoveNext ();
Type contractType = enumerator.Current.ContractType;
WebHttpBinding binding = new WebHttpBinding();
binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue; //here also
AddServiceEndpoint (contractType, binding, baseAddress);
}
}
foreach (ServiceEndpoint se in Description.Endpoints)
if (se.Behaviors.Find<WebHttpBehavior> () == null)
se.Behaviors.Add (new WebHttpBehavior ());
// disable help page.
ServiceDebugBehavior serviceDebugBehavior = Description.Behaviors.Find<ServiceDebugBehavior> ();
if (serviceDebugBehavior != null) {
serviceDebugBehavior.HttpHelpPageEnabled = false;
serviceDebugBehavior.HttpsHelpPageEnabled = false;
}
}
}
}