Configuring the BAM WCF interceptor programmatically

Although adding the BAM WCF-WF interceptors via .config files works for most of the scenarios, there are some cases on which you need to add these interceptors programmatically. Dealing with multiple BAM databases, dynamically enabling and disabling tracking and dynamically adjusting the polling intervals are some of the most common reasons for not relying 100% on the app.config configuration.

The techniques for adding the WF interceptor programmatically are well documented and essentially are not different from adding any other tracking service. The case of the WCF interceptors is a little different and requires us to implement a small trick. This is mostly given to the fact that the BamEndpointBehavior (WCF Endpoint Behavior) class which abstracts the BAM interceptor functionality is not public and consequently it can’t be added to the service endpoint behaviors collection. The trick to access this endpoint behavior is to invoke the CreateBehavior method of the BAM behavior extension element. Given that this is a protected operation we need to create a small class that inherits from the BamBehaviorExtension and provides access to the IEndpointBehavior. The following code illustrates the details.

private static void StartHost()

{

using (ServiceHost host = new

ServiceHost(typeof(FinancialTransactionService)))

{

WCFBAMProgExtension bamExtension= new WCFBAMProgExtension();

bamExtension.PrimaryImportConnectionString = "Connection String…";

bamExtension.PollingIntervalSec= 1500;

foreach(ServiceEndpoint endpoint in host.Description.Endpoints)

  endpoint.Behaviors.Add(bamExtension.CurrentBAMBehavior);

host.Open();

Console.ReadLine();

host.Close();

}

}

internal class WCFBAMProgExtension : BamBehaviorExtension

{

public IEndpointBehavior CurrentBAMBehavior

{

get { return (IEndpointBehavior)base.CreateBehavior(); }

}

}

No Comments