简体   繁体   English

Windows Workflow Foundation-在工作流中的不同活动之间传递自定义变量

[英]windows Workflow foundation - passing custom variable between different activity in a workflow

I am passing a custom variable type from one action to another action in a workflow. 我正在将自定义变量类型从一个动作传递到工作流中的另一个动作。 Here is the definition of the custom object 这是自定义对象的定义

       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;
            }
        }

During the first step of the action, it will run the following code to load the config data from a file and store in an object (ConfigEnvironment) that is returned in function Execute 在操作的第一步期间,它将运行以下代码以从文件加载配置数据,并将其存储在函数Exec​​ute中返回的对象(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;
    }
}

In the subsequent activity in the workflow, I would like to obtain the data stored in this object. 在工作流的后续活动中,我想获取存储在该对象中的数据。 However, the following code will have a compile error as EnvironmentDetail object could not find the function GetDatabaseConnectionString. 但是,以下代码将出现编译错误,因为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

}

} }

The compile warning is the following 'System.Activities.InArgument' does not contain a definition for 'GetDatabaseConnectionString' and no extension method 'GetDatabaseConnectionString' accepting a first argument of type 'System.Activities.InArgument' could be found (are you missing a using directive or an assembly reference?) 编译警告为以下“ System.Activities.InArgument”不包含“ GetDatabaseConnectionString”的定义,并且找不到扩展方法“ GetDatabaseConnectionString”接受类型为“ System.Activities.InArgument”的第一个参数(您是否缺少使用指令还是程序集引用?)

As it turns out, EnvironmentDetail is of Type InArgument (or InArgument<ConfigEnvironment> ) but not of Type ConfigEnvironment You need to do a context.Get<ConfigEnvironment>() to get a variable of Type ConfigEnvironment . 事实证明, EnvironmentDetail的类型为InArgument (或InArgument<ConfigEnvironment> ),但不是ConfigEnvironment类型。您需要执行context.Get<ConfigEnvironment>()以获取ConfigEnvironment类型的变量。

Let me know if this solves your problem or if there's something else still amiss ;-) 让我知道这是否解决了您的问题,或者还有其他问题;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows Workflow Foundation 3在并行活动中等待 - Windows Workflow Foundation 3 wait inside parallel activity Windows Workflow Foundation NullReferenceException - Windows Workflow Foundation NullReferenceException 在Windows Workflow Foundation中创建自定义活动设计而不参考Design DLL - Creating custom activity design without reference to Design DLL in Windows Workflow Foundation CRM:在 C# 中创建工作流和工作流自定义活动的区别 - CRM: Difference between creating Workflow and Workflow custom activity in C# Windows Workflow Foundation WF4-工作流托管 - Windows Workflow Foundation WF4 - Workflow hosting Windows Workflow Foundation和Persistence中的子工作流 - Sub Workflow in Windows Workflow Foundation and Persistence 如何对Windows Workflow Foundation(WF)活动可以序列化进行单元测试? - How to unit test that a Windows Workflow Foundation (WF) activity can be serialized? Windows Workflow Foundation每隔X分钟重复一次活动 - Windows Workflow Foundation Repeat an Activity Every X minutes Windows Workflow Foundation 4.5接收活动队列行为 - Windows Workflow Foundation 4.5 Receive Activity Queuing Behavior 将图像添加到Windows工作流自定义活动设计器 - Add an image to a windows workflow custom activity designer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM