locked
Problem WorkflowInspectionServices.CacheMetadata when using reflection RRS feed

  • Question

  • Hi All,
     
    This is a little bit complicate to explain, we have the following issue when using reflecation
     
      public class MainApp
      {
        static Activity _a;
        public void Init()
        {
           _a = new Activity1();
    
          WorkflowInspectionServices.CacheMetadata(_a); 
        }
    
    
        public void Start()
        {
          WorkflowInvoker.Invoke(_a);
        }
      }
    in the same name space we have a class
      public class Test
      {
        public string GetString()
        {
          return "Test";
        }
      }

    now in the workflow we have a variable name variable1 of type Test and a console.writeline activity that call variable1.GetString()
     
    <Activity mc:Ignorable="sap" x:Class="ActivityLibrary1.Activity1" sap:VirtualizedContainerService.HintSize="273,240" mva:VisualBasic.Settings="Assembly references and imported namespaces for internal implementation" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:local="clr-namespace:ActivityLibrary1" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:sa="clr-namespace:System.Activities;assembly=System.Activities" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Text;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <Sequence sad:XamlDebuggerXmlReader.FileName="c:\users\avik.internal\documents\visual studio 2010\Projects\ConsoleApplication9\ActivityLibrary1\Activity1.xaml" sap:VirtualizedContainerService.HintSize="233,200">
      <Sequence.Variables>
       <strong><Variable x:TypeArguments="local:Test" Default="[New Test()]" Name="variable1" />
    </strong>  </Sequence.Variables>
      <sap:WorkflowViewStateService.ViewState>
       <scg3:Dictionary x:TypeArguments="x:String, x:Object">
        <x:Boolean x:Key="IsExpanded">True</x:Boolean>
       </scg3:Dictionary>
      </sap:WorkflowViewStateService.ViewState>
      <strong><WriteLine sap:VirtualizedContainerService.HintSize="211,61" Text="[variable1.GetString()]" />
    </strong> </Sequence>
    </Activity>
    
     Now when we load the application from a different process using
    <strong>Assembly assm = Assembly.LoadFile(filepath);
    </strong>
    dynamic _app = null;
          var tp = assm.GetTypes().Where((t) =>
          {
            return (t.FullName == "ActivityLibrary1.MainApp");
          });
    
          if (tp.Count() > 0)
          {
            _app = Activator.CreateInstance(tp.ElementAt(0));
          }
    
    
    
          tp = assm.GetTypes().Where((t) =>
          {
            return (t.FullName == "ActivityLibrary1.Test");
          });
    
          if (tp.Count() > 0)
          {
            dynamic blah = Activator.CreateInstance(tp.ElementAt(0));
    
            Console.WriteLine(blah.GetString());
    
          }
    
    
          _app.Init();
    
          _app.Start();
    
          Console.ReadLine();
    
     
    everything is working fine
     
    when instead of Assembly assm = Assembly.LoadFile(filepath);
     
    we use Assembly assm = Assembly.Load(File.ReadAllBytes(filepath));
     
    Like in the following
    <strong>Assembly assm = Assembly.Load(File.ReadAllBytes(filepath));
    </strong>dynamic _app = null;
    
          var tp = assm.GetTypes().Where((t) =>
          {
            return (t.FullName == "ActivityLibrary1.MainApp");
          });
    
          if (tp.Count() > 0)
          {
            _app = Activator.CreateInstance(tp.ElementAt(0));
          }
    
    
         <strong> _app.Init();
    </strong>
          _app.Start();
    
          Console.ReadLine();
    
     The reflection work just fine but when reaching WorkflowInspectionServices.CacheMetadata(_a);  in the _app.Init we get the following exception
     
     Unhandled Exception: System.Activities.InvalidWorkflowException: The following errors were encountered while processing the workflow tree:
    'Activity1': The private implementation of activity '1: Activity1' has the following validation error:   Compiler error(s) encountered processing expression "New Test()".
    Type 'Test' is not defined.
    'Activity1': The private implementation of activity '1: Activity1' has the following validation error:   Compiler error(s) encountered processing expression "variable1.GetString()".
    Type 'Test' is not defined.
       at System.Activities.Validation.ActivityValidationServices.ThrowIfViolationsExist(IList`1 validationErrors)
       at System.Activities.WorkflowInspectionServices.CacheMetadata(Activity rootActivity, LocationReferenceEnvironment hostEnvironment)
       at System.Activities.WorkflowInspectionServices.CacheMetadata(Activity rootActivity)
       at ActivityLibrary1.MainApp.Init()
       at CallSite.Target(Closure , CallSite , Object )
       at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1[T0](CallSite site, T0 arg0)
       at ConsoleApplication9.Program.Main(String[] args) in c:\users\build\documents\visual studio 2010\Projects\ConsoleApplication9\ConsoleApplication9\Program.cs:line 58
     

    To make thing short when using Assambly.Load to load a byte array containing a workflow in which that workflow use some local class as a variable the workflow fail to cache.

    When Reflecting from a local file everything run fine.

    can someone elaborate more on this issue? Or better help how to solve this issue?

     

    Thanks,

     

    Avi

     
     
     
    Thursday, August 18, 2011 10:39 AM

Answers

  • I have good news and bad news...

    First the good news - The problem does not reproduce in the next release of .NET using either VB or C# expressions.

    The bad new - it does repro in .NET 4 and I don't think there is anything that could be done to work around the problem.


    Sr. Program Manager, AppFabric Development Platform (WF/WCF) http://blogs.msdn.com/rjacobs http://www.twitter.com/ronljacobs
    • Marked as answer by Avi Kadosh Thursday, August 25, 2011 6:48 PM
    Thursday, August 25, 2011 2:59 PM