locked
Visual Studio doesn't build with InternalsVisibleTo if using a CodeActivity within the same assembly as the workflow itself (with the designer). RRS feed

  • Question

  • I have unit test projects to test some workflows. So I have some InternalVisiblesTo in my main project so my test project can call them. But when I do workflows (.xaml) in the test project using some codeactivity also defined into the test projects, the build doesn't see the InternalVisiblesTo attribute and I get errors like this one :

    Error 1 'BugVSInternalVisiblesTo.Class1' ne contient pas de définition pour 'Method1' c:\Projects\BugVSInternalVisiblesTo\ActivityLibrary1\CodeActivity1.cs 9 41 ActivityLibrary1

    Here are the steps to reproduce easily my case :

    1- Create a C# Class Library project with this code :

    using System.Runtime.CompilerServices;
    [assembly: InternalsVisibleTo ("ActivityLibrary1")]
    
    namespace BugVSInternalVisiblesTo
    {
      public static class Class1
      {
       internal static void Method1 ()
       {
       }
      }
    }
    
    

    2 : Create a new C# Workflow ActivityLibrary project named "ActivityLibrary1" with this code :

    using System.Activities;
    
    namespace ActivityLibrary1
    {
      public class Class1FromActivityLibrary1
      {
       public static void Test ()
       {
         BugVSInternalVisiblesTo.Class1.Method1 ();
       }
      }
    
      public sealed class CodeActivity1 : CodeActivity
      {
       protected override void Execute (CodeActivityContext context)
       {
       }
      }
    }
    

    3 : Add a reference from ActivityLibrary1 to the ClassLibrary

    4 : Add a new Workflow > Activity (with designer) in the ActivityLibrary1 project.

    5 : Drop "CodeActivity1" into the designer of the Activity1.xaml .

    6 : Build

    Problem : I get this error at build time :

    Error 1 'BugVSInternalVisiblesTo.Class1' ne contient pas de définition pour 'Method1' c:\Projects\BugVSInternalVisiblesTo\ActivityLibrary1\CodeActivity1.cs 9 41 ActivityLibrary1

    If I remove the CodeActivity1 from the xaml to place a standard activity (like the Delay), I don't get this problem.

    So, is there any fix available for this bug? Or something I'm doing wrong?

    Friday, September 24, 2010 3:17 PM

Answers

  • Hi,

    For the usage of InternalsVisibleToAttribute Class , see the remark section of document:
    http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx

    "The friend assembly (that is, the assembly that can access the current assembly's internal types and members) is identified by the InternalsVisibleToAttribute constructor. Both the current and the target assembly must be unsigned, or both must be signed with a strong name. If they are signed with a strong name, the argument to the InternalsVisibleToAttribute constructor must include the full public key as well as the name of the assembly."

    Regards
    This posting is provided "AS IS" with no warranties, and confers no rights. Microsoft Online Community Support. My Blog:http://xhinker.com "Microsoft Windows Workflow Foundation 4.0 Cookbook"
    • Marked as answer by Andrew_Zhu Friday, October 1, 2010 7:54 AM
    Friday, October 1, 2010 7:53 AM

All replies

  • Hi,

    Since Method1 is declared as a internal method which can only be accessed by code in the same assembly. when you build the two project in VS, these two projects will be build into two respective assemby that why Method1 cannot be found in ActivityLibrary1 project. solution is simple, replace "internal" with "public".

    Regards


    This posting is provided "AS IS" with no warranties, and confers no rights. Microsoft Online Community Support. My Blog:http://xhinker.com
    Monday, September 27, 2010 9:19 AM
  • If you try to reproduce the problem with the sample code I wrote, you'll see I have put a [assembly: InternalsVisibleTo ("ActivityLibrary1")], so the internal methods are also visible in the other assembly (very useful for unittestings...). The problem occurs only when using the .xaml like described above. Try the sample, and exclude from the project the .xaml to see the code compile... and it doesn't compile only when it is included...

    Monday, September 27, 2010 11:48 AM
  • Hi,

    For the usage of InternalsVisibleToAttribute Class , see the remark section of document:
    http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx

    "The friend assembly (that is, the assembly that can access the current assembly's internal types and members) is identified by the InternalsVisibleToAttribute constructor. Both the current and the target assembly must be unsigned, or both must be signed with a strong name. If they are signed with a strong name, the argument to the InternalsVisibleToAttribute constructor must include the full public key as well as the name of the assembly."

    Regards
    This posting is provided "AS IS" with no warranties, and confers no rights. Microsoft Online Community Support. My Blog:http://xhinker.com "Microsoft Windows Workflow Foundation 4.0 Cookbook"
    • Marked as answer by Andrew_Zhu Friday, October 1, 2010 7:54 AM
    Friday, October 1, 2010 7:53 AM
  • I don't think you've answered this question.  Is there a way to get WF to play nicely with InternalsVisibleToAttribute?
    Thursday, July 21, 2011 12:32 PM
  • Hmm. I seem to be able to repro this too. The repro also seems to satisfy 'Both the current and the target assembly must be unsigned'.

    I was going to recommend someone open this bug on connect.microsoft.com.
    However, I found out it's actually known issue. Please try this workaround.

    
      <!--Workaround: adding the following line between <PropertyGroup> tags in the ActivityLibrary .csproj file -->
      <XamlTemporaryAssemblyName>$(AssemblyName)</XamlTemporaryAssemblyName>
    

    Tim

    Thursday, July 21, 2011 8:00 PM
  • Few years ago I created a utility that makes it very easy to add InternalsVisibleTo to your AssemblyInfo.cs file. 

    Here's the link:
    http://vkreynin.wordpress.com/2007/12/09/testing-internals-members-with-internalsvisibleto-attribute/
    Monday, April 30, 2012 3:09 PM
  • Maybe you are placing the "[assembly: InternalsVisibleTo("..")] in the wrong file? I believe it is intended to be used inside the Project\Properties\AssemblyInfo.cs file.
    Wednesday, December 18, 2013 3:06 PM