Question

I am having issues merging an assembly containing my PostSharp aspects into my project via SmartAssembly and was wondering if any one can help.

The main assembly is fairly simple and looks like:-

 class Program
{
    static void Main(string[] args)
    {
        var doer = new Doer();                

        doer.Do();

        Console.WriteLine("press any key to continue");
        Console.ReadKey();          
    }
}

[MethodDebugLogging(AttributeTargetElements = MulticastTargets.Method)]
public class Doer 
{
    public void Do()
    {
        Console.WriteLine("stuff and nonesense");
    }
}

The MethodDebugLogging aspect works fine provided it's in the same assembly but adding it to it's own assembly and then merging via SmartAssembly results in:-

System.TypeInitializationException: The type initializer for '<>z__Aspects' thre
w an exception. ---> System.TypeInitializationException: The type initializer fo
r '<>z__AspectsImplementationDetails762586886' threw an exception. ---> System.I
O.FileNotFoundException: Could not load file or assembly 'Aspects, Version=1.0.0
.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system
 cannot find the file specified.
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String cod
eBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark&
stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppre
ssSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName as
semblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntr
ospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evid
ence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.Load(String assemblyString)
   at PostSharp.Aspects.Serialization.BinaryAspectSerializationBinder.BindToType
(String assemblyName, String typeName)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Bind(String as
semblyString, String typeString)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(Binary
AssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String obje
ctName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInf
ormationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, Bi
naryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWi
thMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(He
aderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAp
pDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCr
ossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
(Stream serializationStream, HeaderHandler handler, Boolean fCheck, IMethodCallM
essage methodCallMessage)
   at PostSharp.Aspects.Serialization.BinaryAspectSerializer.Deserialize(Stream
stream, IMetadataDispenser metadataDispenser)
   at PostSharp.Aspects.Serialization.AspectSerializer.Deserialize(Assembly asse
mbly, String resourceName, IMetadataDispenser metadataDispenser)
   at <>z__AspectsImplementationDetails762586886..cctor()
   --- End of inner exception stack trace ---
   at Obfuscation_Spike1.Doer.<>z__Aspects..cctor()
   --- End of inner exception stack trace ---
   at Obfuscation_Spike1.Doer.Do()
   at Obfuscation_Spike1.Program.Main(String[] )

And the aspect itself is fairly simple:-

 [Serializable]
public class MethodDebugLogging :
     OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
        Console.WriteLine("Entering: {0}.{1}", args.Method.DeclaringType, args.Method.Name);
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        Console.WriteLine("Exiting: {0}.{1}", args.Method.DeclaringType, args.Method.Name);
    }
}

From the exception it looks to me as if the aspects namespace of the Postsharp Distributable gets stripped out when I merge the assembly containing the aspect, is anyone able to shed some light on this?

Thanks

Paul

Was it helpful?

Solution

The easiest way to make your assembly easily mergeable is to use [PSerializable] instead of [Serializable].

If you want to keep using [Serializable], you will have to configure assembly binding at runtime. See http://doc.postsharp.net/postsharp-3.0/##PostSharp-3.0.chm/html/fbd7975f-6c20-41d8-bda5-38392ed2ad00.htm for details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top