How to configure a WCF service for a load balancer - wcf

How to configure a WCF service for a load balancer and specific the end points

You could try writing a custom service host factory which will use the load balancer's url as base address:
public class CustomServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(
Type serviceType, Uri[] baseAddresses)
{
Uri uri = null;
if (baseAddresses.Length < 2)
{
uri = baseAddresses[0];
}
else
{
// TODO: You need to choose the load balancer's url here:
uri = baseAddresses[????];
}
return base.CreateServiceHost(serviceType, new Uri[] { uri });
}
}

Related

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.

wcf unknownmessagereceived with iis host

If I have a WCF application hosted on IIS, how can I handle
UnknownMessageReceived events?
I know how to do it when I build a console host.
You can use a service host factory to get access to the service host instance which is used under IIS. You can find more information about service host factories at http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx, and the code below shows an example of a factory which listens to the UnknownMessageReceived events.
public class MyFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);
host.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(host_UnknownMessageReceived);
return host;
}
void host_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
{
// do something with the message
}
}

Hosting a WCF service in a unit test

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));
}

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;
}
}
}
}