本主题提供关于从代码而不是通过配置文件加载 WF 和 WCF 拦截器的信息。
谨慎
此示例或指南引用敏感信息,例如连接字符串或用户名和密码。 切勿在代码中硬编码这些值,并确保使用最安全的身份验证来保护机密数据。 有关详细信息,请参阅以下文档:
从代码加载 WF 拦截器
若要从代码中加载 WF 拦截器运行时,必须创建一个新的 WorkflowRuntime 实例,并使用一个新的 BamTrackingService 实例调用 AddService 方法。 下面演示了这一点。
string connectionString = "Integrated Security=SSPI;Data Source=.;Initial Catalog=BAMPrimaryImport";
int PollingIntervalSec = 300;
WorkflowRuntime workflowRuntime = new WorkflowRuntime();
workflowRuntime.AddService(new BamTrackingService(connectionString, interceptorConfigurationPollingInterval));
从代码加载 WCF 拦截器
若要加载 WCF 拦截器,必须创建一个派生类,该类启动服务并可供您的实现访问。 下面演示了这一点。
// Your project must have a reference to Microsoft.BizTalk.BAM.Interceptors.dll.
// Create a derived class accessible to the implementation that opens the service.
internal class MyBamBehaviorExtension : BamBehaviorExtension
{
internal MyBamBehaviorExtension(string connectionString, int pollingInterval)
: base()
{
this.ConnectionString = connectionString;
this.PollingIntervalSec = pollingInterval.ToString();
}
internal IEndpointBehavior Create()
{
return (IEndpointBehavior) this.CreateBehavior();
}
}
// Add the endpoint behavior just before the service is opened.
// In this example the connection string and polling intervals are being read from appSettings in App.config.
MyBamBehaviorExtension bamBehaviorExtension = new MyBamBehaviorExtension(ConfigurationManager.AppSettings["ConnectionString"], int.Parse(ConfigurationManager.AppSettings["PollingIntervalSec"]));
IEndpointBehavior bamBehavior = bamBehaviorExtension.Create();
foreach (System.ServiceModel.Description.ServiceEndpoint endpoint in myServiceHost.Description.Endpoints)
{
if (endpoint.Behaviors.Find<MyBamBehaviorExtension>() == null)
endpoint.Behaviors.Add(bamBehavior);
}