다음을 통해 공유


활동 트리 검사

활동 트리 검사는 워크플로 애플리케이션 작성자가 애플리케이션에서 호스트되는 워크플로를 검사하는 데 사용됩니다. WorkflowInspectionServices를 사용하여 워크플로에서 특정 자식 활동을 검색하고, 개별 활동과 속성을 열거하며, 활동에 대한 런타임 메타데이터를 특정 시간에 캐시할 수 있습니다. 이 항목에서는 WorkflowInspectionServices와 이를 사용한 활동 트리 검사 방법에 대해 간략하게 설명합니다.

WorkflowInspectionServices 사용

GetActivities 메서드는 지정한 활동 트리의 모든 활동을 열거하는 데 사용됩니다. GetActivities는 자식, 대리자 처리기, 변수 기본값, 인수 식을 비롯하여 트리 내의 모든 활동을 연결하는 열거형을 반환합니다. 다음 예제에서는 Sequence, While, ForEach<T>, WriteLine 및 식을 사용하여 워크플로 정의를 만듭니다. 워크플로 정의를 만든 후에는 이를 호출한 다음 InspectActivity 메서드를 호출합니다.

Variable<List<string>> items = new Variable<List<string>>
{
    Default = new VisualBasicValue<List<string>>("New List(Of String)()")
};

DelegateInArgument<string> item = new DelegateInArgument<string>();

Activity wf = new Sequence
{
    Variables = { items },
    Activities =
    {
        new While((env) => items.Get(env).Count < 5)
        {
            Body = new AddToCollection<string>
            {
                Collection = new InArgument<ICollection<string>>(items),
                Item = new InArgument<string>((env) => "List Item " + (items.Get(env).Count + 1))
            }
        },
        new ForEach<string>
        {
            Values = new InArgument<IEnumerable<string>>(items),
            Body = new ActivityAction<string>
            {
                Argument = item,
                Handler = new WriteLine
                {
                    Text = item
                }
            }
        },
        new Sequence
        {
            Activities =
            {
                new WriteLine
                {
                    Text = "Items added to collection."
                }
            }
        }
    }
};

WorkflowInvoker.Invoke(wf);

InspectActivity(wf, 0);

활동을 열거하기 위해 GetActivities를 루트 활동에 대해 호출하고 반환되는 각 활동에 대해 다시 반복적으로 호출합니다. 다음 예제에서는 활동 트리의 각 활동과 식에 대한 DisplayName을 콘솔에 기록합니다.

static void InspectActivity(Activity root, int indent)
{
    // Inspect the activity tree using WorkflowInspectionServices.
    IEnumerator<Activity> activities =
        WorkflowInspectionServices.GetActivities(root).GetEnumerator();

    Console.WriteLine("{0}{1}", new string(' ', indent), root.DisplayName);

    while (activities.MoveNext())
    {
        InspectActivity(activities.Current, indent + 2);
    }
}

이 샘플 코드는 다음과 같이 출력됩니다.

List Item 1
List Item 2List Item 3List Item 4List Item 5컬렉션에 추가된 항목입니다.시퀀스리터럴<목록<문자열>>
While
AddToCollection<문자열>
VariableValue<ICollection<문자열>>
LambdaValue<문자열>
LocationReferenceValue<목록<문자열>>
LambdaValue<부울>
LocationReferenceValue<목록<문자열>>
ForEach<문자열>
VariableValue<IEnumerable<문자열>>
WriteLine
DelegateArgumentValue<문자열>
시퀀스
WriteLine
리터럴<문자열> 모든 활동을 열거하지 않고 특정 활동을 검색하기 위해 Resolve가 사용됩니다. ResolveGetActivities는 모두 WorkflowInspectionServices.CacheMetadata가 이전에 호출되지 않은 경우 메타데이터 캐싱을 수행합니다. CacheMetadata가 호출된 경우 GetActivities는 기존 메타데이터를 기반으로 합니다. 따라서 CacheMetadata, GetActivities를 마지막으로 호출한 이후에 트리를 변경한 경우 예기치 못한 결과가 나타날 수 있습니다. GetActivities를 호출한 후 워크플로가 변경된 경우 ActivityValidationServices Validate 메서드를 호출하여 메타데이터를 다시 캐시할 수 있습니다. 메타데이터 캐시에 대해서는 다음 단원에서 설명합니다.

메타데이터 캐시

활동에 대한 메타데이터 캐시에서는 활동의 인수, 변수, 자식 활동 및 활동 대리자에 대한 설명을 빌드하고 유효성을 검사합니다. 기본적으로 메타데이터는 런타임에 활동 실행이 준비될 때 캐시됩니다. 워크플로 호스트 작성자가 이에 앞서 활동 또는 활동 트리에 대한 메타데이터를 캐시하려는 경우(예: 모든 선행투자 비용을 가져오려는 경우) CacheMetadata를 사용하여 원하는 시간에 메타데이터를 캐시할 수 있습니다.