共用方式為


使用 BAM API 載入 BAM 攔截器

本主題提供從程式代碼載入 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);  
}  

另請參閱

使用 BAM WCF 和 WF 攔截器