Null Reference Exception with System.Reflection.Assembly
Asked Answered
C

3

19

I have developed a Library for internal email reporting. When I am using that Library from another project (By Adding Reference).

It gives NullReferenceException on the following line.

System.Reflection.Assembly.GetEntryAssembly().GetName().Name

Any idea, why Assembly is null?

Cockleshell answered 25/2, 2011 at 12:14 Comment(6)
Does this apply? From Assembly.GetEntryAssembly: The GetEntryAssembly method can return Nothing when a managed assembly has been loaded from an unmanaged application.Flapjack
Unmanaged code? Both Applications are developed in same version of .netCockleshell
@Ani, I think its managed, because both applications are developed in .net 3.5 and both are under same source control.Cockleshell
I had the same issue when I run unit test through Resharper Unit Test GUI.Roz
@Jenea, had you found any solution for it thn?Cockleshell
Unfortunately I didn't look for it. So I would be also interested in the answer.Roz
C
2

problem is solved guys,

I am using

Assembly.GetAssembly(ex.TargetSite.DeclaringType.UnderlyingSystemType).GetName().Name 

to get the EntryAssemblyName.
In this case I already has parameter which is taking Exception 'ex', so I solved it by using that.

Many Thanks Guys, specially @Aliostad

Cheers

Cockleshell answered 25/2, 2011 at 16:27 Comment(2)
Aliostad already answered this. It's not a good practice to make a program blow up in order to get a result. You can simple Follow Aliostad's cleaner approach by simply : System.Reflection.Assembly.GetExecutingAssembly().GetName().NameJennifer
@Cockleshell is there any other way to get the top parent Assembly/dll name? other than by ex,Pouliot
P
30

This is expected especially in the Windows Services where they are loaded by an unmanaged runtime.

Use:

  Process.GetCurrentProcess().MainModule.FileName

To get unmanaged entry point file.


Update

It seems you are looking for this:

  System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
Pisolite answered 25/2, 2011 at 12:23 Comment(8)
Thanks Aliostad, but in this case, its a .NET Library which is used from a web application. Do you have idea how to sort out this problem?Cockleshell
Well, loader is IIS which is not managed. Why do you need EntryAssembly? You can use Process.GetCurrentProcess().MainModule.FileName to find out which unmanaged entry point it was.Pisolite
@Aliostad, Process.GetCurrentProcess().MainModule.FileName gives me the full path C:\\Program Files\\Common Files\\Microsoft Shared\\DevelopmentServer\\9.0\\WebDev.WebServer.exe, Actually I want to get the name of the application.Cockleshell
What is name of the application? Do you mean web application? Your question need some work mate.Pisolite
sorry @Aliostad, yes want to get the name of Web application and version.Cockleshell
See my update. Name of the HTTP APPLICATION is IIS specific and you cannot use ASP.NET to get that.Pisolite
@Aliostad, I have solved my problem by using Assembly.GetAssembly(ex.TargetSite.DeclaringType.UnderlyingSystemType).GetName().Name I already had Exception (ex) as a parameter. Many thanks for your help.Cockleshell
Definitely, but thats not my Answer :) it worked my using 'Assembly.GetAssembly(ex.TargetSite.DeclaringType.UnderlyingSystemType).GetName().Name'Cockleshell
C
2

problem is solved guys,

I am using

Assembly.GetAssembly(ex.TargetSite.DeclaringType.UnderlyingSystemType).GetName().Name 

to get the EntryAssemblyName.
In this case I already has parameter which is taking Exception 'ex', so I solved it by using that.

Many Thanks Guys, specially @Aliostad

Cheers

Cockleshell answered 25/2, 2011 at 16:27 Comment(2)
Aliostad already answered this. It's not a good practice to make a program blow up in order to get a result. You can simple Follow Aliostad's cleaner approach by simply : System.Reflection.Assembly.GetExecutingAssembly().GetName().NameJennifer
@Cockleshell is there any other way to get the top parent Assembly/dll name? other than by ex,Pouliot
A
0

Answering both the OP's and @Neeraj 's questions: sometimes it can also be useful to get the root of your assembly with Assembly.GetExecutingAssembly().Location (e.g. when the Resharper test runner is making your life hard when using GetEntryAssembly())

string rootDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string someFile = Path.Combine(
            rootDir ?? throw new InvalidOperationException(),
            "Foo",
            "Bar.txt");
Aeolic answered 1/2, 2019 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.