"Could not load type [Namespace].Global" causing me grief
Asked Answered
J

34

111

In my .Net 2.0 Asp.net WebForms app, I have my Global.asax containing the following code:

<%@ Application CodeBehind="Global.asax.cs" Inherits="MyNamespace.Global" Language="C#" %>

However when I build I get an error stating-

Could not load type 'MyNamespace.Global'.

This seems to be because the MyNamespace namespace (defined in the code behind file Global.asax.cs) is not seen by the compiler in the Global.asax file (does not show in R# intellisence..). This turned out to be a very hard nut to crack... any help will be appreciated!

Note: The Global.asax and the Global.asax.cs are located in the same folder.

Note2: When compiling from the vs prompt with csc it compiles o.k.

Judon answered 5/1, 2010 at 11:39 Comment(2)
Do other pages in the app work? Sometimes this happens when the webserver is configured to run 1.1 but the app is compiled for 2.0Lyautey
yes, 'Target Framework' is 2.0Judon
O
166

One situation I've encountered which caused this problem is when you specify the platform for a build through "Build Configuration".

If you specify x86 as your build platform, visual studio will automatically assign bin/x86/Debug as your output directory for this project. This is perfectly valid for other project types, except for web applications where ASP.NET expects the assemblies to be output to the Bin folder.

What I found in my situation was that they were being output to both (Bin and Bin/x86/Debug), with the exception that some of the dll's, and inexplicably the most important one being your web application dll, being missing from the Bin folder.

This obviously caused a compilation problem and hence the "Could not load type Global" exception. Cleaning the solution and deleting the assemblies made no difference to subsequent builds. My solution was to just change the output path in project settings for the web app to Bin (rather than bin/x86/Debug).

Oxidation answered 21/5, 2011 at 0:34 Comment(7)
Also: Try closing and re-opening VS. If you are using built in debugging server then it might be running a different website. Opening/closing will re-register the localhost ports.Subeditor
In my case it wanted to put the project output in inetpub\wwwroot\myproject\bin, but I had iisexpress configured to look in myProject\bin. Just changing the output path solved the problem.Cassette
In my case I had a .dll that should have been included, but was not set to 'copy local' for some reason. This caused a similar compilation problem to that stated in this answer.Furfuraceous
Thanks, Stantona. This is what worked for me, and just in time for me to go home.Dalmatic
@Stantona. any possible solution when we have multiple projects with one output folder.Caudill
i changed my project platform target to x86, and recompiled. Error solvedThorlay
Changing project's output path to bin folder solved the issue. Thanks for saving my day!Catafalque
G
18

Have you changed the namespace of your project? I've seen this happen occasionally where I've changed the namespace in the Project Properties dialog but Visual Studio hasn't changed the namespace declaration in existing code files.

Granuloma answered 5/1, 2010 at 12:28 Comment(7)
What do you think i should do?Judon
Check the namespace in your .cs file and make sure it's the same as the one declared in the asax file. My suspicion is they'll be different...Granuloma
I'm not sure then. All I can suggest for right now is to rebuild and check the contents of namespaces in your assembly with ILDASM.Granuloma
Thank you, this was the solution that helped me. I'll use Resharper to check for all namespaces in my solution.Talapoin
I just had this occur: In an MVC 5 project (not that it matters) and I changed the project namespace but forgot to change the Global.asax (and the App_Start folder). Regardless of experience, sometimes the silly oversights still get you.Giagiacamo
I changed my namespace and solved this by changing the namespace also under Project-Properties-Application then Assembly name and Root namespace. Nothing else worked.Porch
I had changed in both Project Properties and also replaced all previous Namespace with new ones (but somehow it had missed to replace the one in asax.cs). Renaming that fixed the issue. (y)Roguery
H
16

I am new to asp .net developement and I faced the similar problem.

I updated the class as a partial class and it worked fine.

public partial class Global : System.Web.HttpApplication
Hoke answered 28/6, 2010 at 2:0 Comment(1)
Not sure why my Global file had ended up without this but this was the issue for me, thanks!Pewit
S
10

I restarted Visual Studio and error was gone!

Severin answered 26/11, 2014 at 1:28 Comment(1)
the same worked for me. Make sure you close ALL Visual Studio opened.Angiology
S
5

Here's another one for the books. It appears that this happens when you launch more than one web application from the same port number.

Basically, I have a couple of branches that I work off, I have a main branch and a staging branch and a release branch. When I switched branch to the staging branch I noticed that it was using the same port address configuration so I opted to change that. I then received another warning that this reservation conflicts with another configured application. The IIS Express server is sensitive about that and for whatever reason it bricks the configuration.

By simply picking a third unaffected port this problem went away because it then maps the port to a new directory mapping (my branches are located differently on disk). I noticed this because I tried to change the type name pointed to by Global.asax but the type name was unchanged even after restarting the server, so clearly, the code I was changing wasn't reflected by the IIS Express deployment.

So, before you lose too much sleep over this, try changing the IIS port number that is currently used to run the web project.

Sagerman answered 16/4, 2014 at 18:42 Comment(0)
S
5
  1. Right Click on Project Solution and Select Batch Build.
  2. Then select Your Project Name and Clean And Rebuild.

Works Fine For Me IN VS 2015.Now I can Use Global event. My Global.asax file has this Line

<%@ Application Language="C#" CodeBehind="~/App_Code/Global.asax.cs" Inherits="Global" %>

And I make class file Global.asax.cs that is in AppCode folder that look like

public partial class Global : HttpApplication
{
    public Global()
    {
        //
        // TODO: Add constructor logic here
        //
    }
}

I hope this will help

Shaylyn answered 7/4, 2016 at 9:28 Comment(0)
K
3

Check the Build Action of Global.asax.cs. It should be set to Compile.

In Solution Explorer, Right-click Global.asax.cs and go to Properties. In the Properties pane, set the Build Action (while not debugging).

It seems that VS 2008 does not always add the .asax(.cs) files correctly by default.

Kenelm answered 7/1, 2010 at 20:4 Comment(0)
D
3

I experienced a similar error when having a

<clear/>

tag as a child (the first child) of the

<assemblies>

tag in my Web.config. I had inserted the tags in my web.config in an attempt to prevent configuration inheritance in an application deployed under the 'Default Web Site' in IIS.

Desiredesirea answered 30/8, 2012 at 22:50 Comment(2)
This definitely pointed me in the right direction. Ends up I had put <clear/> in the parent directory since that was an empty placeholder. I believe the real problem was that it cleared out assemblies my application referenced which prevented the application assembly from loading.Stab
I experienced this too - removing selected assemblies explicitly instead of clearing the list worked out nicely.Themselves
F
2

In my situation it was related to Web Site/Web Application type of the project. We recently moved to MVC and had to change it to Web Application.

So, the solution was simple: select your web-site in Solution Explorer and remove it from the solution, then right click on the solution and select Add -> Existing Project (not Web Site), recompile the web-site.

Felixfeliza answered 28/11, 2011 at 21:51 Comment(0)
S
2

Old post but I go this error when trying to convert from website project to web application project.

Follow the instructions on this Link. I still got the global.asax error but all I did was delete it and re-add it back by right clicking on the project in visual studio and selecting add new item. Add the global.asax file and it worked.

Soph answered 23/8, 2012 at 15:24 Comment(0)
A
2

None of these worked for me, unfortunately. The fix I found was more specific to development, specifically, debugging on your local machine. Also unfortunately, it doesn't really fix the issue in the way I was hoping, but if you're at your wit's end, this might get you running again.

TL;DR: In the project properties, in the web tab, under Servers, Select Use Local IIS Web Server. The address,

http://localhost/MyApp" 

was already populated (I have IIS7, .NET 4.0). What was originally selected was the "Use Visual Studio Development Server" with a Virtual path of "/"

What's really puzzling to me is the fact that nothing else worked, I went through all of the suggestions I could find on SO, and nothing would work. The weird thing is, the error was (seemingly, it's been a few months since I last looked) manifested when I added a new ascx file that was similar to an existing one that was added from an old .net 2.0 (I think) project that would allow for custom user management from within the application. It was working, beautifully, for a long time, until I tried adding this new file. After adding it, and seeing the error, I promptly reverted all changes, but the Global.ascx error would not go away, not even blowing away the whole project and re-getting latest from source control.

Antitoxic answered 6/12, 2013 at 20:27 Comment(0)
S
2

I had converted my solution from VS2003 to VS2010 and had had problems converting the web application project.

I experienced the exact same problem and none of the answers worked for me.

What worked for me was:

  • Right Click on the solution and select Configuration Manager
  • Looked at each of the configurations in the 'Active solution configuration' drop down list.
  • Included the web application in the build by ticking the 'Build' check-box.

it would seem that the problems I had during the conversion had removed the web application project from the build for some reason.

Hopefully this answer helps anyone else that has the same problem...

Scherman answered 20/5, 2014 at 6:34 Comment(0)
S
1

If you rebuild or change a project and move over files from an old one, make sure to check the Inherit block of your global. In my case, the former project/solution was named intranet, and I had recreated it as Intranet, but when I moved over the files, it didn't like the lowercase (duh). Just do a general look-through of the names of the files.

Spathose answered 6/4, 2012 at 17:40 Comment(0)
K
1

I had a similar issues where I was receiving this error on a project.

“Could not load type [Namespace].Global
Error in Line 1   etc etc

After spending some time I suspect a function with possible errors in a Class ..later commenting that specific function my problem get resolved.

I dont know why Visual Studio did't give me that specific error at debugging time. But this error might occure due to some errors in class file..

Kantar answered 30/3, 2013 at 17:8 Comment(0)
C
1

Change the GUID of the assembly. This fixes many MANY problems, I have found.

Critchfield answered 23/8, 2013 at 5:43 Comment(1)
How do you change the GUID ?Glue
I
1

This work for me: First thing: it's seem that no matter what did you say to visual studio, the ide always look the file in: bin (for web app and off course in my case) So, even when i said to visual studio a specific path to load file, the ide keep looking for the wrong path. So i change in the: Build/Configuration Manager the output type to: Release (previous i clean up the solution, even manually) so, when the file .dll was created then i moved manually to "bin" folder under the project/solution folder. Hope this will be helpfull !!

Infusorian answered 6/1, 2014 at 13:45 Comment(0)
F
1

When I encountered this problem most recently I tried everything mentioned here but to no avail. After tearing my hair out I decided to try deleting my entire code base (yes, pretty desperate!) and then re-downloaded everything from my code repository. After doing this everything worked fine once more.

Seems an extreme solution but just thought I'd include it here as it hasn't been mentioned before in this thread.

(Note that the other time I have encountered this issue, is when the Global.asax was inheriting from a component which needed to be registered on the host machine. This was missing hence I got this same issue).

TL;DR; If all answers in this thread don't work for you, try deleting and then re-downloading your entire code base!

Finley answered 31/1, 2015 at 0:59 Comment(0)
L
1

In my case, It was because of my target processor (x64) I changed it to x86 cleaned the project, restarted VS(2012) and rebuilt the project; then it was gone.

Lindsy answered 11/2, 2015 at 1:55 Comment(0)
D
1

I've run across this issue a few times and in each case I was rebuilding a computer or switching to a new computer. My first step (besides updating the machine and installing Visual Studio) is to pull my projects down from Git and test them.

I hit this error each time because I tried to access my local code before compiling it. You see, I have Git and Subversion setup to ignore my bin/build folders, so after a pull from my repository I forgot to run a build which pulls required packages from Nuget (since I have Git/SVN ignore those too) and creates the DLLs needed to actually run my app.

I doubt this will resolve most people's issues, but I didn't see it on the list of potential solutions so I thought I'd add it.

Dunker answered 12/3, 2015 at 1:26 Comment(0)
D
1

In my case, I had added Global.asax to a WCF project to experiment with it, but decided to remove it. I removed it from Solution Explorer, but because it was still in the folder, the pipeline was still finding it and causing this error.

I removed the Global.ASAX and GLobal.asax.cs from the filesystem and that did resolve the error.

Duration answered 28/1, 2016 at 17:58 Comment(1)
THIS solved it for me, thanks, but I would also like to add for others in the future that in my case I did not ever add Global.asax to my project. But as mentioned here, the global.asax and global.asax.cs files were in my file system. When I deleted them, the bug went away.Kittykitwe
S
1

Having been in the development game for almost 20 years, this chestnut has continued to plague me across multiple projects.

As such, today whilst experiencing this same problem again, against another project, I decided to investigate further and I do believe this is related to Bin folder location... or more specifically, the output path of the bin folder.

For me, for a simple service based web application configured to run/debug through IIS, it was changing the output path from bin\debug to bin\ that resolved it

Project > Properties > Build > Output path

Seriously hopes this helps.

Schreck answered 14/12, 2018 at 6:41 Comment(0)
I
0

I was befuddled by the same darn issue. I tried to remove and and the global.asax (closed VS2010 before adding). Cleaned the project/solution, checked for any changes in the web application configuration and other stuffs that had worked for other people here in SO threads. I finally cleaned the solution, deleted the bin/obj folders and stopped any running VS2010 development servers then I reverted all the changes back and found the application was running again. I redid the same things and now its working fine.

Happened again and this time this solution worked for me.

Impatient answered 18/6, 2012 at 14:39 Comment(0)
H
0

go to configuration Manager under properties for your solution. Then make sure all projects and getting built, and this won't be a problem.

Haase answered 1/5, 2013 at 19:7 Comment(0)
L
0

I had to delete (duplicate) files from disk that were not included in the project. Looks like the duplicates were caused by a failed rename. The file names were different, but same code.

After deleting all the oof.* files I was able to scan.

  • foo.aspx
  • foo.aspx.cs
  • foo.aspx.designer.cs
  • oof.aspx
  • oof.aspx.cs
  • oof.aspx.designer.cs
Lively answered 22/7, 2015 at 13:56 Comment(0)
S
0

in my case it was IISExpress pointing to the same port as IIS to solve it go to

C:\Users\Your-User-Name\Documents\IISExpress\config\applicationhost.config

and search for the port, you will find <site>...</site> tag that you need to remove or comment it

Silverstein answered 8/9, 2015 at 3:42 Comment(0)
P
0

I had this issue when deploying on prod server only. In my other environments it works... I just deleted stuff in the bin folder then republish and it work after that.

Plectrum answered 24/3, 2017 at 11:56 Comment(0)
D
0

If you are using Visual Studio, You probably are trying to execute the application in the Release Mode, try changing it to the Debug Mode.

Drees answered 7/6, 2017 at 11:2 Comment(0)
B
0

I've tried to rebuild solution and clear ASP.NET Temporary files without success.
But after run IISRESET the error disappeared.

Update: I had the same problem again 1 month later. I've noticed that MyWebsite.DLL exists in bin folder, but doesn't exist in Temporary ASP.NET Files (C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files). I've tried a few things, that are suggested on this and "Parser Error Message: Could not load type" in Global.asax questions (I actually forgot about my own answer), but the error disappeared again only after IISRESET

Bawdy answered 21/6, 2017 at 5:59 Comment(0)
F
0

Well In my case VS 2017, the lightweight solution load was causing this issue. I disabled it & restarted VS then re-build my solution & the problem gone.

Filmore answered 22/8, 2017 at 7:24 Comment(1)
What is "the lightweight" solution?Ramer
C
0

Just wanted to add my two cents. I was receiving the same error, and I tried all the suggestions to no avail. My situation is probably different?

Turns out, an auto-generated "AssemblyInfo.cs" file had some extraneous spaces, which was preventing me from launching the web app (via debug). Here's what the file looked like:

[assembly: AssemblyTitle("WebApplication2")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("

            ")]
[assembly: AssemblyProduct("WebApplication2")]
[assembly: AssemblyCopyright("Copyright © 

             2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

After killing the spaces in the AssemblyCompany and AssemblyCopyright, I was finally able to build and launch the project.

Observed in the following environment: --Visual Studio 2017 Community version 15.3.0 --Win 7 x64 Enterprise --New Project > Visual C# > Web > ASP.NET Web Application > Web Forms

Castalia answered 6/9, 2017 at 14:14 Comment(0)
V
0

I experienced this problem when I accidentally set "Chrome" to be the default browser for debugging. When I set it back to "IE" the problem disappeared. I am not sure why...


EDIT: I was about to delete this answer, because I wasn't sure about it, but then I had the problem again. I switched to browsing with Chrome, then back again to IE and it stopped! What gives!?

Voile answered 22/9, 2017 at 11:58 Comment(0)
P
0

As silly as it may sound. Here is what I did..

My project was targeting 4.6.1 version. Changed the target version to 4.6.2. built it. deleted obj & bin folders. & once again changed the version to 4.6.1. Bingo!!

Plummer answered 1/3, 2018 at 11:32 Comment(0)
Q
0

You need to set your Output path to bin\

Q answered 6/6, 2020 at 1:48 Comment(0)
R
0

We have a .net app running on a PROD server. When we copied the bin folder to the server for the latest release, we got the same error. We couldn't understand what was going on. It worked fine locally and on the TEST server. After some digging I noticed that some of the uploaded files had a size of 0 bytes.

It turned out it was the FTP client (Filezilla) that corrupted some of the uploaded files, which understandably made .net think some libraries was located elsewhere or missing.

We tried another FTP client and re-uploaded all files. And that seemed to have done the trick.

Rennin answered 25/3, 2021 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.