So I was reading this fun article from Dustin Davis and decided to try it on my own with PostSharp 2.1.7.30 and StructureMap 2.6.4.1.
using System;
using PostSharp.Aspects;
namespace StructureMap
{
internal class Program
{
private static void Main(string[] args)
{
ObjectFactory.Initialize(x => {
x.For(typeof (IDbFind)).Use(typeof (DbFind)); });
var w = new Worker();
var f = w.Finder;
}
}
internal class Worker
{
[IoCResolution] private IDbFind finder;
public IDbFind Finder
{
get { return finder; }
}
}
internal class DbFind : IDbFind
{
}
internal interface IDbFind
{
}
[Serializable]
public class IoCResolution : LocationInterceptionAspect
{
public override void OnGetValue(LocationInterceptionArgs args)
{
args.ProceedGetValue();
if (args.Value == null)
{
object obj = ObjectFactory.GetInstance(
args.Location.PropertyInfo.PropertyType);
args.Value = obj;
args.ProceedSetValue();
}
}
}
}
Unfortunately during execution I get this error:
{"The type initializer for '<>z__Aspects' threw an exception."}
With an Inner Exception of:
{"Could not load file or assembly 'StructureMap, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' or one of its dependencies. The
located assembly's manifest definition does not match the assembly reference.
(Exception from HRESULT: 0x80131040)":"StructureMap, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null"}
FusionLog:
=== Pre-bind state information ===
LOG: DisplayName = StructureMap, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/Scratch/StructureMap/StructureMap/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : PostSharp, Version=2.1.0.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Scratch\StructureMap\StructureMap\bin\Debug\StructureMap.vshost.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Scratch/StructureMap/StructureMap/bin/Debug/StructureMap.DLL.
WRN: Comparing the assembly name resulted in the mismatch: PUBLIC KEY TOKEN
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
Any ideas what I could be doing wrong?
UPDATE 5 minutes later: I'm an idiot ... This will teach me not to call my namespace the same as a 3rd party DLL. After changing namespace to StructureMapPlay, it works just fine.
Change the namespace to StructureMapPlay, in code and project properties.