-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ModuleScope.SaveAssembly produces incorrect / incomplete assemblies when proxying types from a weak-named assembly #327
Comments
@stakx firstly thanks for such a detailed explanation of what you are seeing.
You'll get a file named
I don't think this is a beginner mistake at all, it sounds like a defect.
The no args
This sounds like a longstanding bug that hasn't hurt anyone because few people save assemblies and those that have have likely strong named their own assemblies (as you said that case works). Obviously using a weak named assembly everything works at runtime because there are two I added the following at the bottom of your foreach (System.Reflection.Assembly asm in System.AppDomain.CurrentDomain.GetAssemblies())
{
System.Console.WriteLine(asm);
} And get the expected result of two
Did you want to dig in to see if you can fix the assembly that the invocation types gets generated into. |
@jonorossi, thank you for the analysis and confirming that this is a defect.
To tell the truth, it doesn't really hurt me, either, but...
I'm happy to look into this. (PS: I haven't forgotten about the PR for some new documentation regarding DynamicProxy's handling of by-ref parameters, I just got delayed. It's still in the pipeline, though. :-) |
TL;DR: I am either misunderstanding how
ModuleScope.SaveAssembly
works, or DynamicProxy — when it produces proxies for types in weak-named assemblies — puts generated proxy types in a weak-named module and proxy method invocation types in a strong-named module, resulting in failure or incomplete output byModuleScope.SaveAssembly
.The generated assemblies mentioned below can be found in
CastleDynProxy2_strong_and_weak.zip
.Saving with
ModuleScope.SaveAssembly(strongNamed: false)
Suppose I'm trying to save the dynamic assembly generated by DynamicProxy as follows:
Note that I am not strong-naming the assembly containing above code—if I did, then everything would work as expected.
This generates an assembly
CastleDynProxy2
, which appears to have two problems:It is missing types
Castle.Proxies.Invocations.IFoo_Method
andCastle.Proxies.Invocations.Bar_Method
. PEVerify is a bit cryptic about it, but seems to tell me the same thing:There is an assembly reference to a strong-named
DynamicProxyGenAssembly2
, which is confusing, because the generated assembly already has the exact same name (except for being weak-named).If I try to run code that references and uses the
CastleDynProxy2.dll
as produced above, I will get aFileNotFoundException
(perhaps due to the second issue above):Saving with
ModuleScope.SaveAssembly(strongNamed: true)
When I change the last line of the original program (at the top) to:
and inspect the generated assembly
CastleDynProxy2.dll
(which is now strong-named), I see this:Castle.Proxies.Invocations.IFoo_Method
andCastle.Proxies.Invocations.Bar_Method
, but it is missing the proxy typesCastle.Proxies.IFooProxy
andCastle.Proxies.BarProxy
.Saving with
ModuleScope.SaveAssembly()
If I try to save the assembly without specifying
strongNamed: false
, norstrongNamed: true
, I get an exception right away:Possible conclusions
I am making a beginner mistake (hinted at by the last exception shown just above). What I don't understand in this case is, how would I let DynamicProxy save a correct, weak-named dynamic assembly referencing types
IFoo
,Bar
from my own weak-named assembly?SaveAssembly(strongNamed: false)
doesn't seem to do the trick.When generating proxies for types in weak-named assemblies, DynamicProxy puts the proxy types in a weak-named assembly and the invocation types in a strong-named assembly. Therefore it will only be able to save either of those at a time, but fails to save both. This results in incomplete output assembly either way.
What is going wrong here?
The text was updated successfully, but these errors were encountered: