Why is one of my `Global.asax.cs` file in my ASP.net solution with two startup projects not called?

旧时模样 提交于 2020-01-24 00:37:05

问题


In a Visual Studio (Professional 2017) solution, I have multiple projects, two of which are startup projects, the UI and the API. The curious thing is that the API\Global.asax.cs does not seem to be executed, although both startup projects are actually enabled to be launched for debugging. I am sure about that, since when hitting the green Debug arrow, two browser windows open, and both UI and API work.

There are two distinct Global.asax.cs-files, one for each startup project (whether that is a sensible design, I don't know - I took over the solution this way). Within the one for the API, breakpoints are not working, more specifically VS says:

The breakpoint will not currently be hit. A copy of Global.asax.cs was found in UI.dll, but the current source code is different from the version built into UI.dll.

To allow this breakpoint to be hit: right-click on the breakpoint, choose either 'Conditions...' or 'Setting...'. Then chcoose 'Location', 'Allow the source code to be different from the original.'

To allow this for all breakpoints, disable the option 'Required source files to exactly match the original version' under Tools, Options, Debugging, General.

The full hover-over-breakpoint-message looks as follows:

Following the last suggestion did not change anything. Concerning the first suggestion, I do not have the options to choose in the context menu under Conditions.

I cannot inquire to much about the solutions's architectural background, that is why I am asking here for help: I am assuming that the error message also means that the file API/Global.asax.cs is not read at all, or is that conclusion wrong?

More importantly: How do I ensure that API\Global.asax.cs is read/ executed? Differently put: When is it supposed to be executed when in debug mode? I have the impression, it executed every now and then since I see debug-output in my VS-Output window sometimes - I did not manage to guess when it is executed.

Edit: Further Background

In in the Solution Property Pages (right-click on the solution and choose Properties), there is the option Multiple startup projects chosen. The API project is in the list before UI.

Under Project Dependency is specified that the API project depends on different other projects, but not the UI project. The other projects have some inter-dependencies as well, but none of them depends on the UI project. The UI project does not depend on any other project, either.

References

  • Why would my ASP.NET project run on the development server when not among the startup projects?
  • Where does the output of System.Diagnostics.Debug.WriteLine go, if I put it in Global.asax.cs?

回答1:


In any one ASP.NET web app there can be only one Global.asax file (whether with code behind or not). One of the Global.asax files will be overwriting the other if both projects deployed to the same folder, and the one that wins will specify the winning start up type that hooks into the Application instance's events:

 <%@ Application Codebehind="Global.asax.cs" Inherits="MyProject.MvcApplication" Language="C#" %>

The only way to have two would be two separate web apps (one could be in a child folder, providing it is – in IIS – set as a web app of its own and thus running it its own AppDomain if in the same IIS App Pool or otherwise in its own process).



来源:https://stackoverflow.com/questions/59753445/why-is-one-of-my-global-asax-cs-file-in-my-asp-net-solution-with-two-startup-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!