Nhibernate mapped internal classes and InternalsVisibleTo
My proxy generator is having trouble generating proxies for internal mapped Nhibernate classes. I have tried adding them as visible using InternalsVisibleTo in assembl开发者_运维百科yinfo.cs but it doesn't seem to work. Worse, I don't know how to tell if I've even successfully managed to friend the proxy assemblies I want to because if I change a few numbers in the proxy assemblies public key in assemblyinfo.cs there is no error thrown.
Error:
Test method TestProject1.UnitTest1.TestMethod1 threw exception: NHibernate.HibernateException: Creating a proxy instance failed ---> Castle.DynamicProxy.Generators.GeneratorException: Type is not public, so a proxy cannot be generated. Type: BaseSystemCore.Domain.Lot
StackTrace:
Castle.DynamicProxy.DefaultProxyBuilder.AssertValidType(Type target) Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options) Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors) NHibernate.ByteCode.Castle.ProxyFactory.GetProxy(Object id, ISessionImplementor session) NHibernateUtilities.BaseUnitOfWork.handleException(Exception e) in C:\Users\Isaac.G\Desktop\svn.bolinger.ca\Library Projects\NHibernateUtilities\NHibernateUtilities\BaseUnitOfWork.cs: line 871 NHibernateUtilities.BaseUnitOfWork.getAllT in C:\Users\Isaac.G\Desktop\svn.bolinger.ca\Library Projects\NHibernateUtilities\NHibernateUtilities\BaseUnitOfWork.cs: line 115 TestProject1.UnitTest1.TestMethod1() in C:\Users\Isaac.G\Desktop\svn.bolinger.ca\Library Projects\BaseSystemCore\TestProject1\UnitTest1.cs: line 71
Has anyone ever got this to work before?
Thanks
Isaac
NHibernate 3.2 has built in proxy provider. I briefly look at the sources at it seems like they use this format: {0}ProxyAssembly. Where {0} is a type name for lazy mapped class. I have not tested it though. Try adding following to your AssemblyInfo.cs and replace {0} with your lazy class name:
[assembly: InternalsVisibleTo("{0}ProxyAssembly")]
If you still want to use older version of NHibernate you can try using this (for castle byte code provider):
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
DynamicProxyGenAssembly2 is a temporary assembly that gets generated on the fly by Castle. It contains classes derived from your mapped classes (proxies).
For what its worth, this is what I've dropped into a T4. It automatically generates all the InternalsVisibleTo
attributes for my entities. Hope it helps.
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\Domain\bin\Debug\Domain.dll" #>
<#@ import namespace="System.Linq" #>
<#@ output extension=".cs" #>
using System.Runtime.CompilerServices;
<#
var publicType = typeof(Domain.Foo);
var allTypes = publicType.Assembly.GetTypes();
var entityType = allTypes.Single(t => t.FullName == "Domain.Entities.Entity"); // my entity supertype
foreach(var type in allTypes.Where(t => !t.IsAbstract && entityType.IsAssignableFrom(t)))
{#>
[assembly: InternalsVisibleTo("<#= type.Name #>ProxyAssembly")]
<#}#>
精彩评论