windows工作流基础-在工作流中的不同活动之间传递自定义变量
本文关键字:工作流 之间 活动 自定义 变量 windows | 更新日期: 2023-09-27 18:03:53
我正在将一个自定义变量类型从一个操作传递到工作流中的另一个操作。下面是自定义对象
的定义 public class ConfigDatabase
{
public string Name;
public string Host;
public string Port;
public string Instance;
public string User;
public string Password;
}
public class ConfigDatabases
{
public string DatabaseToUse;
public List<ConfigDatabase> DatabaseList;
public ConfigDatabases()
{
DatabaseList = new List<ConfigDatabase>();
}
}
public class ConfigEnvironment
{
public ConfigDatabases EnvironmentConfigDatabase;
public ConfigEnvironment()
{
EnvironmentConfigDatabase = new ConfigDatabases();
}
public ConfigDatabase ReturnDatabaseInfo()
{
ConfigDatabase ConfigDatabaseInfo = new ConfigDatabase();
for (int Count1 = 0; Count1 < EnvironmentConfigDatabase.DatabaseList.Count; Count1++)
{
if (EnvironmentConfigDatabase.DatabaseList[Count1].Name == EnvironmentConfigDatabase.DatabaseToUse)
{
ConfigDatabaseInfo = EnvironmentConfigDatabase.DatabaseList[Count1];
return ConfigDatabaseInfo;
}
}
return ConfigDatabaseInfo;
}
public string GetDatabaseConnectionString()
{
ConfigDatabase DatabaseInfo = ReturnDatabaseInfo();
string ConnectionString = "Data Source=(description=(address=(protocol=tcp)(host=" + DatabaseInfo.Host + ")(port=" + DatabaseInfo.Port + "))(connect_data=(sid=" + DatabaseInfo.Instance + ")));User ID=" + DatabaseInfo.User + ";Password=" + DatabaseInfo.Password + ";";
return ConnectionString;
}
}
在操作的第一步中,它将运行以下代码从文件中加载配置数据,并将其存储在Execute
函数返回的对象(ConfigEnvironment)中public sealed class InitializeEnvironment : CodeActivity<ConfigEnvironment>
{
// Define an activity input argument of type string
public InArgument<string> EnvironmentFileLocation { get; set; }
// If your activity returns a value, derive from CodeActivity<TResult>
// and return the value from the Execute method.
protected override ConfigEnvironment Execute(CodeActivityContext context)
{
// Obtain the runtime value of the Text input argument
string EnvironmentFile = context.GetValue(this.EnvironmentFileLocation);
EnvironmentConfigInitialization EnvironmentInitialize = new EnvironmentConfigInitialization(EnvironmentFile);
ConfigEnvironment EnvironmentDetail = EnvironmentInitialize.LoadData();
return EnvironmentDetail;
}
}
在工作流的后续活动中,我希望获得存储在该对象中的数据。然而,下面的代码会有一个编译错误,因为EnvironmentDetail对象找不到GetDatabaseConnectionString函数。
public sealed class ExecuteSQL : CodeActivity<DataRowCollection>
{
// Define an activity input argument of type string
public InArgument<string> SQLScript { get; set; }
public InArgument<ConfigEnvironment> EnvironmentDetail { get; set; }
// If your activity returns a value, derive from CodeActivity<TResult>
// and return the value from the Execute method.
protected override DataRowCollection Execute(CodeActivityContext context)
{
string connectionString4 = EnvironmentDetail.GetDatabaseConnectionString(); //This create a compile error
}
}
编译警告如下所示"System.Activities。InArgument'不包含'GetDatabaseConnectionString'的定义,也没有'GetDatabaseConnectionString'的扩展方法接受类型为'System.Activities '的第一个参数。事实证明,EnvironmentDetail
是InArgument
(或InArgument<ConfigEnvironment>
)类型,但不是ConfigEnvironment
类型,您需要做context.Get<ConfigEnvironment>()
以获得ConfigEnvironment
类型的变量。
让我知道这是否解决了你的问题,或者是否还有其他地方不对;-)