如何編寫商務原則 Import-Export 和部署腳本
本節說明如何使用 RuleSetDeploymentDriver以程式設計方式匯入/匯出和部署原則。
下面範例將顯示如何將原則匯出到檔案。
using System;
using Microsoft.RuleEngine;
using Microsoft.BizTalk.RuleEngineExtensions;
namespace SimpleExport
{
class ExportPolicy
{
[STAThread]
static void Main(string[] args)
{
if (args.Length != 3)
Console.WriteLine("Format: PolicyName MajorVersion MinorVersion");
else
{
string policyName = args[0];
int majorRev = Convert.ToInt16(args[1]);
int minorRev = Convert.ToInt16(args[2]);
RuleSetInfo rsi = new RuleSetInfo(policyName,majorRev,minorRev);
Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver dd;
dd = new Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver();
string fileName = (rsi.Name + "-" + rsi.MajorRevision + "." + rsi.MinorRevision + ".xml");
dd.ExportRuleSetToFileRuleStore(rsi,fileName);
}
}
}
}
下面範例將顯示如何從檔案匯入和部署原則。
using System;
using Microsoft.RuleEngine;
using Microsoft.BizTalk.RuleEngineExtensions;
namespace SimpleImport
{
class ImportPolicy
{
[STAThread]
static void Main(string[] args)
{
String filename = args[0];
Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver dd;
dd = new Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver();
SqlRuleStore sqlRuleStore = (SqlRuleStore) dd.GetRuleStore();
FileRuleStore fileRuleStore = new FileRuleStore(filename);
RuleSetInfoCollection rsic = fileRuleStore.GetRuleSets(RuleStore.Filter.All);
foreach (RuleSetInfo rsi in rsic)
{
RuleSet ruleSet = fileRuleStore.GetRuleSet(rsi);
bool publishRuleSets = true;
sqlRuleStore.Add(ruleSet,publishRuleSets);
dd.Deploy(rsi);
}
}
}
}
注意
此程式碼假設詞彙之間沒有相依性;若原則使用詞彙,必須先匯入這些詞彙。