Can using server-side scripting delimiters (<% %>) cause compilations during runtime? - c#

I am occasionally getting "Application compilation is starting." event in my Event Log and I can't identify what's causing it. I think I may try this - http://blogs.msdn.com/b/tess/archive/2008/11/06/troubleshooting-appdomain-restarts-and-other-issues-with-etw-tracing.aspx - but before I do that I was curious if I can identify the problem without starting to mess with something unknown.
I have used <%= %> and <%# %> tags throughout the app so I am wondering if this is what's causing the problems. On couple spots I have embedded C# code (using ) so that may add to it?
Precompiling the app is also valid choice for me, I just don't want to end in position in which I need execute precompilation command on the server every time I upload some changes to the server. Currently on my dev machine I've followed advices from this link - http://mikehadlow.blogspot.com/2008/05/compiling-aspx-templates-using.html - and it does awesome job as it allows me to identify errors in C# code in .aspx pages during build in Visual Studio. However, I presume precompilation results are not stored in my website directory (and won't be published when I use Publish option).
Ideally, I want to stay in position I am with default Web Application model with addition of automatically running compilation as soon as I upload changed .aspx or .ascx over FTP (not waiting for user's http request). Am I asking too much, or is this possible to setup?

From my research it seems it can.
Because nobody responded I'll accept this as answer.

Related

Debug in Browser?

Asp.net app runs fine without issues on localhost. However, when published and I access via Chrome/Edge it does not work as intended (will not load the page when button is clicked). Is there a way to see what the live published version is doing in the code?
Tried replicating the issue in Visual Studio Debug, but could not recreate it
ASP.NET code is executed on the server, so there’s not a lot to see in the browser. You can use the Chrome/Edge developer tools (F12) to see what it is seeing, though. There might be helpful information in the console and especially in the network tab, where you can see what requests the app is making and what the server’s responses to them are in some detail. (I’ve had problems with web apps making assumptions about their path that didn’t apply once they were installed.) If you have sufficient access to the server, it might be helpful to look at its logs, especially if your app is returing 500 errors. It’s often a good idea to implement a lot of logging in your web app, using NLog, log4net, or similar libraries. Again, though, you have to have permission to write the logs and to look at them when you need to.
Please follow the normal debugging workflow:
Load a page in your browser.
Open the developer tools.
Select Sources tab; Open HTML file; Open JavaScript file.
Set a breakpoint in the JavaScript file.
Reload the browser page. Loading stops at the breakpoint.
Debug until you find the problem.
Fix the problem (in your usual code editor, not in the developer tools) and save the file.
Debugging tools are supported by most browsers and environments, which make debugging easier by stepping through the code so you can see what's going on.

Application getting slow when open the application for the first time

I have developed a application in ASP.NET using c#. The entire application working fine but the problem is when I open the application for first time it running very slow. i.e. its taking so much time to load a page like home page or any other page. But when I reopen that page then that page opens quickly as I expect. Even whenever application getting session expired and relogin into application its taking so much time again to load all the pages for the first time, where from 2nd time to opens that pages its not happening. So can anybody tell me what is the problem occuring here.
The application is compiled on the first request.
Read this article by Microsoft.
Because ASP.NET compiles your Web site on first user request, you can
simply copy your application's source code to the production Web
server. However, ASP.NET also provides precompilation options that
allow you to compile your Web site before it has been deployed, or to
compile it after it has been deployed but before a user requests it.
Precompilation has several advantages. It can improve the performance
of your Web site on first request because there will be no lag time
while ASP.NET compiles the site. Precompiling can also help you find
errors that might otherwise be found only when a user requests a page.
Finally, if you precompile the Web site before you deploy it, you can
deploy the assemblies instead of the source code.
You can precompile a Web site using the ASP.NET compiler tool (ASPNET_Compiler.exe). The tool that provides the following precompilation options:
In-place compilation This option performs the same compilation that occurs during dynamic compilation. Use this option to compile a Web site that has already been deployed to a production server.
Non-updateable full precompilation Use this to compile an application and then copy the compiled output to the production server. All application code, markup, and UI code is compiled into assemblies. Placeholder files such as .aspx pages still exist so that you can perform file-specific tasks such as configure permissions, but the files contain no updateable code. In order to update any page or any code you must precompile the Web site again and deploy it again.
Updateable precompilation This is similar to non-updateable full precompilation, except that UI elements such as .aspx pages and .ascx controls retain all their markup, UI code, and inline code, if any. You can update code in the file after it has been deployed; ASP.NET will detect changes to the file and recompile it. Note that code in a code-behind file (.vb or .cs file) built into assemblies during precompilation, and you therefore cannot change it without going through the precompilation and deployment steps again.
However, you mentioned that it's also slow if the session expired. Maybe you are loading too much into memory on session start. It's difficult to make a diagnosis without more informations.
Check your site in firebug in .net tab there you will find which part of your page take much time to load,
there might be some exceptions or some code errors in client side language.
Try to use less sessions
and debug your code to clarify no extra looping of code
(sorry for bad English but hopefully you have got my point)

ASP Page naming and Redirection

I have quite the odd scenario I have not run into before.
I simply have a hyper link, with the url hard coded to www.mywebsite.com/StoreLoc.aspx
My application has two pages that we are concerned about.
Store.aspx
StoreLoc.aspx
Even though the hyperlink is hardcoded to go to StoreLoc.aspx, SOMETIMES ( once out of tenish ) the hyperlink will direct to Store.aspx instead.
I am not the first person to work on this application, so the aspx files very well may have been renamed. Could this cause this issue?
Thank you all for your time.
You might have something in the OnSesssionStart function of the Global.asax file the does a Response.Redirect to Store.aspx.
You session might be getting killed after x minutes of inactivity. This would cause the session to get started again and then get redirected to the start page.
You may want to search for Respose.redirect or Server.Transfer in your visual studio solution.
Hope this helps.

InvalidCastException for Object of the same type - Custom Control Load

I have a very wired error, one of my custom controls seems that is create two compiled files, and when I try to load it dynamically with LoadControl() is just fail because can not cast the one to the other - even if they are exactly the same. I write the message to see that all is the same, is only change the compiled dll.
System.Web.HttpUnhandledException (0x80004005):
Exception of type 'System.Web.HttpUnhandledException' was thrown. --->
System.InvalidCastException:
[A]ASP.Modules_OneProduct_MedioumImage cannot be cast to
[B]ASP.Modules_OneProduct_MedioumImage.
Type A originates from 'App_Web_kg4bazz1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
in the context 'Default'
at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\80ed7513\10eb08d9\App_Web_kg4bazz1.dll'.
Type B originates from 'App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
in the context 'Default'
at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\80ed7513\10eb08d9\App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b.dll'.
The code
This is the code as it is right now after I have follow exactly what is written on MSDN:
foreach (int OneProductID in TheProductIdArrays)
{
// here is the throw.
ASP.Modules_OneProduct_MedioumImage OneProduct =
(ASP.Modules_OneProduct_MedioumImage)LoadControl(#"~/mod/OneProduct_MediumImage.ascx");
// do some work with
//OneProduct
}
Previously I have Load the control without the ASP. but after this bug appears and looking for solution, I strictly follow what is on MSDN. The bug is still here no matter what I do.
I have also try both of this methods, each one alone, and together (again fail)
<%# Register src="~/mod/OneProduct_MediumImage.ascx" tagname="OneProduct_MediumImage" tagprefix="uc1" %>
<%# Reference Control="~/mod/OneProduct_MediumImage.ascx" %>
Config
My web.config, I have try with maxBatchSize 20, 100, 1000, also with optimizeCompilations true or false, but the bug is appears again.
<compilation debug="false" defaultLanguage="C#" batch="true"
maxBatchSize="800" batchTimeout="10800" optimizeCompilations="false"
targetFramework="4.0">
Now some details about
The error is random, in some compile appears, in some other not.
The project is a big one, the pages are live with a lot of people in every minute that ask to see something, but also appears when there is no one inside.
Is run on 64bit dot.net 4, Intergrated
Run as web garden but also tested and one pool alone (and get the same issue)
The session is off on the full project.
The pages are run from 2007 but this issue is appears the last month, unfortunately I can not find where and how is started, or what is trigger it because I late some days to see it.
Appears only one one custom control loads, the one that have heavy call.
I have change 4 times the code making small changes, or big changes and still there.
I have try with optimizeCompilations true and false and the same issue.
I have try also by stopping the web, delete all temporary files, reopening, and there was again.
I have try to place a mutex on global.asax when the application starts to lock only one compile at the time, but this fails also.
From the moment that works, then all is good, but if not works is not auto corrected.
The code that I load this custom control is exist and called in more than one places on the code, on different pages.
Other custom controls, with similar load did not have any problems.
ViewState is disabled for this custom control.
I have also try relocate some code, change the full function call with micro optimizes, no again fail.
Is work fine on development computer. I place batch="true" on web.config and the bug appears right away.
There are no other issues like that, like a bug that we can not fix no matter what. The system is run for days, the pool is NOT recycle at all, the memory is stable, and there is more free to use. The program is run for years now, but we change is almost every day with updates.
Under the same core code runs more than one sites (something like stackexchange) and all have the same random problem.
The AutoEventWireup is false
Its appears and on other custom control that I load the same way.
What I do now as workaround when this bug appears: I just force the project to recompile with a small change, and the error go away, until the next update.
I have a bug that try to solve the last tree weeks with out find the reason. I have try almost anything I can thing of, but all fails, and the bug appears again. So I post here maybe some can help me out and find a way out of this.
Last word: This bug is crazy, the custom control is the same, I do anything on it I only load it dynamically and boom, the compiler is have it two different times for some reason that only he knows - randomly.
Update 1
I been able to reproduce the bug on the developer machine. There I discover that the two dll modules that contains this custom control have a different.
The one was a bundle of 4 custom controls together. The other module was the custom control alone.
Workaround
After tree weeks trying to fix this bug I end up that this bug is appears when the compiler make batch compile of a directory, and bundle many different custom controls, in the same dll. So when I try to load it alone is throw this exception.
So I move the problematic custom control in a different directory alone and seems that I avoid it for now.
Update 2
Appears again, even after I move some files to a different directory. Is random and can not find a clear connection with what is triggers its.
Update 3
Because we have spot that the main issue here is the batch compile (batch="true") that compiles on the same dll many custom controls, one way to say to the compiler to NOT do that, is the maxBatchGeneratedFileSize parameter. I use it with a value of 100, and the issue appears again, now I have lower it to 40 and test it.
maxBatchGeneratedFileSize="40"
This can happen when you have batching turned on and have some form of circular references at the directory level.
Please see this answer to see exactly what I mean by 'circular references' in this context, as the meaning is quite subtle.
If you manage to break the cycle (e.g. by moving a user control elsewhere), you will not hit this issue.
Update 1
I would think that in theory, this can only be caused by a cycle, but sometimes they can be hard to detect.
I'll give you an alternative solution which I think will work and is very easy to try (even though it is a bit of a hack). In the user control(s) that is giving you problems, add the following attribute in the directive:
<%# Control Language="C#" [...] CompilerOptions="/define:dummy1" %>
If you see this with some other controls, you can add the same thing but with dummy2, dummy3, etc...
This will have the effect of not batching this one user control, since it has different compilation needs from the others. Technically, you can add any piece of C# command line as the CompilerOptions, but a dummy /define is the simplest and most harmless.
But unlike turning off batching globally, the perf impact will be minimal, since only a very small subset of pages will not be batched.
BTW, it goes without saying that what you're seeing is a bug in ASP.NET, and that bug has been there for probably 10+ years! Maybe at some point it should get addressed :)
In order to track the cause of the problem I believe that's important to know how your control was created. Please refer to this reading: Turning an .ascx User Control into a Redistributable Custom Control.
Step 1: Authoring the User Control
To author the user control, it is best to start with an empty app that
contains nothing other than the ascx. While the authoring of the user
control uses "standard" techniques, there are some restrictions that
you need to be aware of in order for it to be successfully turned into
a standalone custom control.
The main restriction is that the user control needs to be self-contained. That is, it cannot be dependent on app global things
like App_Code or global.asax. The reason for this is that since the
goal is to turn the UserControl into a standalone DLL, it would
break in other apps if it relied on code that is not part of that
DLL. One exception to this rule is that the UserControl can be
dependent on assemblies that live in the bin directory (or in the
GAC). You just have to make sure that the other assemblies are always
available when you use your custom control in other apps.
and
Step 3: Use the Publish Command to Precompile the Site
(...) Select "Use fixed naming and single page assemblies". This
will guarantee that your user control will be compiled into a single
assembly that will have a name based on the ascx file. If you don't
check this option, your user control could be compiled together with
other pages and user controls (if you had some), and the assembly
would receive a random name that would be more difficult to work with.
In my opinion it's very likely that you have the user control compiled and registered in GAC as a separate assembly and also included in your web application DLL.
Note: Maybe this should have been a comment, but I wanted to include the quotes from the forementioned link. I hope it's helpful.
After a lot of debug on a upgraded ASP.NET website, my last bug was this one on runtime.
I just checked the Build/Publish option "use fixed naming and single page assemblies" and it solved my case :)
Here some useful links: https://msdn.microsoft.com/en-us/library/hh475319(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/aa479044.aspx
http://forums.asp.net/t/960707.aspx
I've recently encountered similar problem when i was compiling a modified version of asp.net MVC 4 and importing the new DLL into the project.
Somehow i was referencing the old versions of the DLLs in the web.config (including the web.config in the views folder)
The error in my case was thrown because the two DLLs were different versions. 4.0.0 and 4.1.0.
Maybe you should look into that.
Maybe specify a version of the compiled files (i'm guessing DLLs)
I hope this helps you fix the problem.
other tips: I'm guessing you have some sort of version control system? if yes , revert all the changes back before this started and look carefully at the code and which models/controls change and how.
if you are not using VCS... there isn't much you can do to revert the changes. And you should start using a VCS.
I have noticed sometimes the designer creates a second CodeBehind designer file, eg you would have:
OneProduct_MediumImage.ascx
OneProduct_MediumImage.ascx.cs
OneProduct_MediumImage.ascx.designer.cs
OneProduct_MediumImage.ascx.designer1.cs
You wont notice if you dont have "Show All Files" option in the Solution Explorer set, but for a web project the compiler will compile all files in the folder, not just the ones included in the project.
Second, if your project is a "Website Project", there are no namespaces, which can lead to many weird errors. Look at this SO question: Namespace problem .net
Finally, I managed to solve seeming random UserControl errors by setting the ClassName attribute on the control file, eg:
<%# Control Language="cs"
AutoEventWireup="false"
CodeBehind="OneProduct_MediumImage.ascx.cs"
Inherits="ASP.Modules_OneProduct_MedioumImage"
ClassName="OneProduct_MediumImageControl" %>

How do I compile and run an ashx file in visual studio?

I have
an ashx file,
Visual Studio 10,
no knowledge at all about C# ASP.NET
What is the proper way to compile and run this?
Context
The ashx file in question can be found in this zip, and is a demo application for a Tetris AI competition. It is a very enticing idea even if it depends a great deal on luck, and I thought I might use the occasion to learn a new language.
An ashx file is a just a generic HTTP handler, so the easiest way to get this working is to create a new Web Site in the File menu, and just add the Handler.ashx file to the website root directory.
Then, just run the site (F5) and browse to "YourSite/Handler.ashx".
An ASHX file is like an ASPX file, but it's a handler. That means it doesn't respond back with HTML by default, and can therefore "handle" otherwise unhandled file types, but it's not necessarily tied to that meaning. In this case, you'll only be presenting the response
position=8&degrees=180
...to a posted board and piece. So you don't need HTML, so you want an ASHX.
You can make .ashx files the startup page in your project, just the same as .aspx pages. If I were writing a HelloUser.ashx page, I might set it as the start page, with some parameters passed in as querystrings or something.
You're probably going to want a test harness that posts a board / piece to your service, and that could be any kind of project. Command line program, website, test class run through NUnit, whatever. There's a lot of logic to keep track of beyond the "player" logic.
If you need a more detailed answer than that, SO might not be the place for this question. But I wish you all kinds of luck with this - it's an interesting problem.
You need to deploy it to an IIS server that has the proper .NET framework installed and that should be it.
If you are trying to get it working locally, create a web site project in visual studio, go to "add existing items" in solution explorer, and locate your ashx. Then click the play button (or press F5) to compile and run it.
Good luck!
You're missing an some form (an ASPX file maybe) that goes with this handler. It looks like this thing probably handles some AJAX request from another page.
It's expecting 2 pieces of data with the request as well:
string board = context.Request.Form["board"];
string piece = context.Request["piece"];
You could reverse engineer the form that this is for, but it will probably take some time to get that board array right.

Categories