Mapping concrete to interface, exception

150 views
Skip to first unread message

CVertex

unread,
Apr 4, 2009, 1:45:16 AM4/4/09
to AutoMapper-users
I get the following exception when I try to run a simple concrete to
interface mapper (code below the exception):

AutoMapper.AutoMapperMappingException: Trying to map Blah to IBling.
Using mapping configuration for AutoMapper.Demo.Blah to
AutoMapper.Demo.IBling
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
---> System.TypeLoadException: Type 'IBlingProxy' from assembly
'IBlingProxyAssembly, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null' is attempting to implement an inaccessible
interface.
at System.Reflection.Emit.TypeBuilder._TermCreateClass(Int32
handle, Module module)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at LinFu.DynamicProxy.ProxyFactory.CreateUncachedProxyType(Type[]
baseInterfaces, Type baseType)
at LinFu.DynamicProxy.ProxyFactory.CreateProxyType(Type baseType,
Type[] baseInterfaces)
at LinFu.DynamicProxy.ProxyFactory.CreateProxy(Type instanceType,
IInterceptor interceptor, Type[] baseInterfaces)
at
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.CreateObject
(Type type)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context,
IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map
(ResolutionContext context)
--- End of inner exception stack trace ---
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map
(ResolutionContext context)
at AutoMapper.MappingEngine.Map(Object source, Type sourceType,
Type destinationType)
at AutoMapper.MappingEngine.Map[TSource,TDestination](TSource
source)
at AutoMapper.Mapper.Map[TSource,TDestination](TSource source)
at AutoMapper.Demo.BlahToBling.SetUpAndConvert() in C:\Projects
\AutoMapper.Demo\AutoMapper.Demo\Program.cs:line 33
at AutoMapper.Demo.Program.Main(String[] args) in C:\Projects
\AutoMapper.Demo\AutoMapper.Demo\Program.cs:line 51

public class Blah {
public int Id { get; set; }

public string GovernmentId { get; set; }

public string FirstName { get; set; }
public string LastName { get; set; }
public string PreferredName { get; set; }

}

public interface IBling {
int Id { get; set; }
string GovernmentId { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
string PreferredName { get; set; }
}

public static class BlahToBling {
public static void SetUpAndConvert() {
Mapper.CreateMap<Blah, IBling>();

Mapper.AssertConfigurationIsValid();


var bling = Mapper.Map<Blah, IBling>(new Blah() {

FirstName = "name",
Id = 23,
LastName = "adsad",
PreferredName = "adasds",
});


System.Console.WriteLine(bling);
}
}

class Program {
static void Main(string[] args) {

try
{
BlahToBling.SetUpAndConvert();

} catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
Console.WriteLine("enter to exito");
Console.ReadLine();
}

}
}


I could be doing something very simple very badly

CVertex

unread,
Apr 4, 2009, 1:49:19 AM4/4/09
to AutoMapper-users
It's weird the unit test for this situation works okay, but my simple
demo mapping doesn't.

anyone using this scenario in production and have it working?

CVertex

unread,
Apr 4, 2009, 1:50:53 AM4/4/09
to AutoMapper-users
I don't get how the interface is inaccessible if it's public
Maybe LinFu is not referencing the source assembly?

Is there a circular assembly reference here that LinFu isn't
exceptioning about?



CVertex

unread,
Apr 4, 2009, 2:10:09 AM4/4/09
to AutoMapper-users
Very confusing.

I added a sample that works okay inside the AutoMapper Samples
project.

Here's the code file

============================================= InterfaceMapping.cs
===========

using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using NUnit.Framework;
using NBehave.Spec.NUnit;

namespace AutoMapperSamples {
namespace Interface {
public interface IBling {
int Id { get; set; }
string StringId { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
string PreferredName { get; set; }
}

public class Blah {
public int Id { get; set; }
public string StringId { get; set; }

public string FirstName { get; set; }
public string LastName { get; set; }
public string PreferredName { get; set; }
}

[TestFixture]
public class InterfaceMapping {
[SetUp]
public void SetUp() {
Mapper.Reset();
}

[Test]
public void Example() {
// Complex model
var blah = new Blah()
{
FirstName = "George",
LastName = "Costanza",
PreferredName = "Georgey",
Id = 0,
StringId = "Can't Stand Ya!"

};

// Configure AutoMapper
Mapper.CreateMap<Blah, IBling>();

// Perform mapping
IBling dto = Mapper.Map<Blah, IBling>(blah);

dto.FirstName.ShouldEqual("George");
dto.LastName.ShouldEqual("Costanza");
dto.PreferredName.ShouldEqual("Georgey");
dto.Id.ShouldEqual(0);
dto.StringId.ShouldEqual("Can't Stand Ya!");
}
}
}

}

CVertex

unread,
Apr 4, 2009, 2:13:22 AM4/4/09
to AutoMapper-users
Also, i'm using Automapper built from the trunk

Jimmy Bogard

unread,
Apr 4, 2009, 12:38:27 PM4/4/09
to automapp...@googlegroups.com
Hmm...would it be possible to send me a zipped project/solution with the issue?  The test passed fine for me too.  All very strange indeed.

Jimmy Bogard

unread,
Apr 4, 2009, 12:51:00 PM4/4/09
to automapp...@googlegroups.com
Hmm...would it be possible to send me a zipped project/solution with the issue?  The test passed fine for me too.  All very strange indeed.

On Sat, Apr 4, 2009 at 1:13 AM, CVertex <vijay.s...@gmail.com> wrote:

CVertex

unread,
Apr 4, 2009, 1:57:57 PM4/4/09
to AutoMapper-users
I created a issue on the tracker

http://automapper.codeplex.com/WorkItem/View.aspx?WorkItemId=1493

which has the attached zip proj/sln with the issue.

Jimmy Bogard

unread,
Apr 4, 2009, 2:25:44 PM4/4/09
to automapp...@googlegroups.com
Sorry for the multiple messages, gmail has been acting weird for me today.

I was able to reproduce the issue, by referencing the ILMerge'd assembly in the samples project instead of the project directly.  I must be doing something wrong there.

Jimmy Bogard

unread,
Apr 4, 2009, 5:57:53 PM4/4/09
to automapp...@googlegroups.com
OK, R51 fixes this problem.  I modified the samples project so that it runs against the released DLL, and not the project.  The fix turned out that as part of the ILMerging, I needed to make a couple of types public in LinFu.

CVertex

unread,
Apr 4, 2009, 11:07:18 PM4/4/09
to AutoMapper-users
Thanks Jimmy,

I svn updated, then rebuilt and retried my demo app (from the issue
tracker), and I get this exception

AutoMapper.AutoMapperMappingException: Trying to map Blah to IBling.
Using mapping configuration for AutoMapper.Demo.Common.Blah to
AutoMapper.Demo.Common.IBling
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
---> System.TypeLoadException: Access is denied:
'LinFu.DynamicProxy.ProxyDummy'.
at AutoMapper.Demo.BlahToBling.SetUpAndConvert() in C:\projects
\AutoMapper.Demo\AutoMapper.Demo\Program.cs:line 14
at AutoMapper.Demo.Program.Main(String[] args) in C:\projects
\AutoMapper.Demo\AutoMapper.Demo\Program.cs:line 32

On Apr 5, 7:57 am, Jimmy Bogard <jimmy.bog...@gmail.com> wrote:
> OK, R51 fixes this problem.  I modified the samples project so that it runs
> against the released DLL, and not the project.  The fix turned out that as
> part of the ILMerging, I needed to make a couple of types public in LinFu.
>
> On Sat, Apr 4, 2009 at 1:25 PM, Jimmy Bogard <jimmy.bog...@gmail.com> wrote:
> > Sorry for the multiple messages, gmail has been acting weird for me today.
>
> > I was able to reproduce the issue, by referencing the ILMerge'd assembly in
> > the samples project instead of the project directly.  I must be doing
> > something wrong there.
>

CVertex

unread,
Apr 4, 2009, 11:12:07 PM4/4/09
to AutoMapper-users
Ok, I added to AutoMapper.exclude

^LinFu\.DynamicProxy\.ProxyDummy$


Then I get the error (for my demo)

AutoMapper.AutoMapperMappingException: Trying to map Blah to IBling.
Using mapping configuration for AutoMapper.Demo.Common.Blah to
AutoMapper.Demo.Common.IBling
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
---> AutoMapper.AutoMapperMappingException: Trying to map Int32 to
Int32.
Using mapping configuration for AutoMapper.Demo.Common.Blah to
AutoMapper.Demo.Common.IBling
Destination property: Id
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
---> System.Reflection.TargetInvocationException: Exception has been
thrown by the target of an invocation. --->
System.MethodAccessException: LinFu.DynamicProxy.InvocationInfo..ctor
(System.Object, System.Reflection.MethodInfo,
System.Diagnostics.StackTrace, System.Type[], System.Object[])
at IBlingProxy.set_Id(Int32 )
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target,
Object[] arguments, SignatureStruct& sig, MethodAttributes
methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object
[] arguments, Signature sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj,
Object value, BindingFlags invokeAttr, Binder binder, Object[] index,
CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj,
Object value, Object[] index)
at AutoMapper.Internal.PropertyAccessor.SetValue(Object
destination, Object value)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context,
IMappingEngineRunner mapper)
--- End of inner exception stack trace ---
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context,
IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map
(ResolutionContext context)
--- End of inner exception stack trace ---
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map
(ResolutionContext context)
at AutoMapper.MappingEngine.Map(Object source, Type sourceType,
Type destinationType)
at AutoMapper.MappingEngine.Map[TSource,TDestination](TSource
source)
at AutoMapper.Mapper.Map[TSource,TDestination](TSource source)
at AutoMapper.Demo.BlahToBling.SetUpAndConvert() in C:\projects
\AutoMapper.Demo\AutoMapper.Demo\Program.cs:line 14
at AutoMapper.Demo.Program.Main(String[] args) in C:\projects
\AutoMapper.Demo\AutoMapper.Demo\Program.cs:line 32




Weird!! Not sure what's up there.

CVertex

unread,
Apr 4, 2009, 11:15:23 PM4/4/09
to AutoMapper-users
I'm using the merged DLL cos that's the only way I'd get AutoMapper
into production. I'm sure this works fine if I used the unmerged DLL.

I'm just not sure what else to make public

Jimmy Bogard

unread,
Apr 5, 2009, 12:30:36 AM4/5/09
to automapp...@googlegroups.com
Ah, there was one more I missed.  Fixed in R54, and I updated the release on CodePlex.

Jimmy Bogard

unread,
Apr 5, 2009, 12:32:08 AM4/5/09
to automapp...@googlegroups.com
And I double-checked your example, it's working great now.

CVertex

unread,
Apr 5, 2009, 12:54:11 AM4/5/09
to AutoMapper-users
Thanks!
Reply all
Reply to author
Forward
0 new messages