How to add and remove assemblies available to "Imports" at runtime? RRS feed

  • Question

  • In a rehosted workflow designer, there is an "Import" options where one can import namespaces so that he can use these namespaces in the workflow, for example in InvokeMethod.

    I don't know how the library determine which assemblies are the source of the namespaces that I can import here, but I seem to be able to add an assembly using,

    AssemblyContextControlItem acci = new
    acci.LocalAssemblyName = new AssemblyName(Assembly.LoadFile(filename).FullName);

    After I execute that code, the namespaces in that assembly become available in the Import option. But I don't know how to remove/cancel this effect (i.e. to make the namespaces unavailable again). Is there a way to do that?

    Or is there an easier way to be able to add or remove assemblies from the Import options?


    Monday, February 14, 2011 2:58 AM

All replies

  • Hi Louis,

    As well as using the AssemblyContextControlItem.LocalAssemblyName, there's a good chance you also want to use AssemblyContextControlItem.ReferencedAssemblyNames, that to which you can add and remove multiple assemblies.

    That list, plus LocalAssemblyName, should equal the list of assemblies which appears in type browser dialog. I haven't checked whether in practice it is also the same as the assemblies which are the Import namespaces list, but I think theoretically it should be, so I think removing the item from ReferencedAssemblyNames is meant to have the effect that you desire.

    If I remember correctly, part of the point of the ReferencedAssemblyNames collection is to allow you to use a different set of assemblies as references than the set of assemblies loaded in the current app domain, which is the default behavior, but at times inconvenient, since you can't unload an assembly you don't want to appear in the list. Basically all the above is hearsay or guessing, I don't remember trying any of this out ever, and I could have misunderstand what I heard, so please take it with a grain of salt, and try it out and let us know if it works. Sorry I don't know enough to be more definitive.

    [A useful related question - what is the difference between LocalAssemblyName and ReferencedAssemblyNames? You should only use LocalAssemblyName for referring to an assembly which should mean 'my assembly' for the workflow type you are defining. If that concept doesn't apply for your workflow, then you should just use ReferencedAssemblyNames.]
    [Other readers, see also previous related thread -]


    Monday, February 14, 2011 4:57 AM
  • You're right I can add an assembly using


                        acci.ReferencedAssemblyNames = new List<AssemblyName>();
                        acci.ReferencedAssemblyNames.Add(new AssemblyName(assembly.FullName));


    But calling acci.ReferencedAssemblyNames.Clear() doesn't remove it

    Monday, February 14, 2011 6:21 AM
  • Hi Louis,
    Thanks for trying it out and letting me know.

    I did some code diving and it looks like ReferencedAssemblyNames is not hooked up to trigger automatically updating the Imports designer the way I imagine it would be, although in some particular circumstances ReferencedAssemblyNames will be used.

    There is a method on SAPV DesignerView which looks like it is used to update the imports namespaces, it looks like it is designed to be called in addition to updating the ReferencedAssemblyNames collection.

    public void OnReferenceUpdated(AssemblyName updatedReference, bool isAdded);

    Please also give this a try.

    Monday, February 14, 2011 9:20 PM
  • How can I get the DesignerView object where I should call the method??
    Tuesday, February 15, 2011 2:36 AM
  • I'm sorry, there has been a mistake.

    I found out that the effect is not related to acci, it's caused only by 


    that I called inside the AssemblyName constructor above. I removed all acci codes and left that line only, and the effect is still the same. Do you know how to reverse this effect?




    Tuesday, February 15, 2011 3:27 AM
  • example:
    private static DesignerView GetDesignerView(EditingContext context)

    WorkflowDesigner wd = new WorkflowDeisgner();
    wd.Load(... activity...);
    DesignerView dv = GetDesignerView(wd.Context);

    Tuesday, February 15, 2011 5:57 PM