all groups > dotnet clr > may 2006 >
You're in the

dotnet clr

group:

Reading assembly manifest without loading assembly


Reading assembly manifest without loading assembly Dale
5/27/2006 9:44:01 AM
dotnet clr:
I am working on a web application that is used for migrating other web
applications to production. The application currently loads each dll in the
target application using Assembly.LoadFile and then calls
GetCustomAttributes on the assembly to ensure it was not built in debug mode.
The problem with this approach is that it leaves the assembly loaded until
the worker process times out or is otherwise unloaded.

I need to be able to determine if the assembly was built in debug mode
without loading the assembly. Because we, by policy, build Release mode
with "Generate Debugging Information" set to true, we have a .pdb file even
in release so checking for the existence of the .pdb is not an option.

I assume the approach would mean to open the file as a binary file, parse
the appropriate metadata elements, and then close the file. Any ideas on how
to do read the metadata like this or on alternative methods to accomplish the
goal that leaves the target assembly files closed and unloaded?


Thanks in advance,

Dale
--
Dale Preston
MCAD C#
Re: Reading assembly manifest without loading assembly Dale
5/27/2006 5:50:02 PM
I appreciate the suggestion, Stephen.

I thought about that but in a web application that solution has all of the
same timing issues and risks as multi-threading a web app - waiting to get
the results without locking up the worker process.

I'd sure like to find a way to just read the metadata if possible.
Otherwise, you are most likely correct in your solution.

Thanks,

Dale

--
Dale Preston
MCAD C#
MCSE, MCDBA


[quoted text, click to view]
Re: Reading assembly manifest without loading assembly Stephen Gennard
5/27/2006 11:38:48 PM
Hello Dale,

I had a very similar problem while developing a MsBuild task and I resorted
to using a separate AppDomain.

It now works a treat!

--
Stephen


[quoted text, click to view]

Re: Reading assembly manifest without loading assembly Greg Young
5/28/2006 12:00:00 AM
http://www.mono-project.com/Cecil should help you out.

Cheers,

Greg Young
MVP - C#
[quoted text, click to view]

Re: Reading assembly manifest without loading assembly Dale
5/28/2006 5:19:01 PM
That's a great suggestion. I feel a little silly for not seeing it myself.
Thank you. I'll give it a try next week.
--
Dale Preston
MCAD C#
MCSE, MCDBA


[quoted text, click to view]
Re: Reading assembly manifest without loading assembly Dale
5/28/2006 5:21:01 PM
Thanks for the tip. I just checked out Cecil and loaded it in a project so I
could view it in the object browser. It definitely has the functionality I
need and the nice thing is I don't have to reinvent the wheel.

I am going to try Stephen Gennard's suggestion of using
Assembly.Load(Byte[]) first because that will work with the existing
application pretty easily but it is nice to have the Cecil suggestion in the
wings.

Thanks to all for helping me solve this problem.

Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA


[quoted text, click to view]
Re: Reading assembly manifest without loading assembly Stephen Gennard
5/28/2006 7:46:05 PM
Hello Dale,

Have you tried just loading the assembly into a byte[] and using Assembly.Load(byte[]);

This may prevent the file lock.

--
Stephen
[quoted text, click to view]

Re: Reading assembly manifest without loading assembly Greg Young
5/28/2006 9:39:28 PM
There is a bigger problem which is not being mentioned about the
Assembly.Load method ..

If you are loading theses assemblies into your own appdomain, there is no
way to unload them after! The memory for the assembly will still be used by
the asp.net worker process .. using cecil this memory will not be lost as
you are working directly with the assembly (i.e. only dealing with the
metadata)

Cheers,

Greg Young
MVP - C#

[quoted text, click to view]

Re: Reading assembly manifest without loading assembly Dale
5/29/2006 9:46:01 AM
Good point. Of course, I live with that condition now as well, where the
application uses Assembly.Load with the assembly file path passed as a
parameter.

Since this application resides on a shared server with many other
applications, memory usage is a critical issue but when patching a production
application, you know how management will likely respond - do the quick fix
not the right fix. :)

But I think I will just estimate both solutions and let them make the call.

Thanks again,

Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA


[quoted text, click to view]
AddThis Social Bookmark Button