I've got an interesting situation where I want to make sure I'm not running in the default AppDomain on an ASP.Net Web Api service (apparently RazorEngine has issues locking temporary files if running in the default AppDomain). From what I can see, by the time Application_Start runs, it's already not in the default AppDomain.
Checked using: AppDomain.CurrentDomain.IsDefaultAppDomain() returns false
Are there any situations where ASP.Net will run in the default AppDomain?
Hi There is no way for user code to have access to the default CLR AppDomain in ASP.NET. The ASP.NET runtime does however use the default AppDomain for some things but it creates a specific AppDomain for running user code. That's why IsDefaultAppDomain returns false and always will :)
Hope this helps!
No, application won't run in the default domain. In one w3wp process, multiple web applications can be hosted and each of them is running in its own appdomain. Asp.net runs some code in default domain to manage those appdomains. e.g. create/unload domain, monitor process memory pressure etc..
Related
ASP.NET Core cannot be run directly in IIS as it requires Kestrel.
This means it is not possible to update a website at runtime like in traditional ASP.NET sites, since the kestrel server have to be shut down during the update.
I want to avoid downtime without adding additional web servers and a load balancer. Is it possible to configure the ASP.NET Core module in IIS to connect to two different Kestrel servers? So if one of them is shut down all requests will go to the other one?
(I was thinking something like having two different folders on disk: C:\inetpub\wwwroot\mysite_instance1 and C:\inetpub\wwwroot\mysite_instance2, thus shutting one down will enable updates of that instance)
If it is possible, are there any considerations we need to be aware of? For example, do the anti forgery token need to be configured in some way? (I do not use sessions.)
I would also be interested in a neat load balancer mechanism, but don't know any way to do it. Yet, you can perfectly switch the directory targeted by a web application from a directory C:\inetpub\wwwroot\mysite_instance1 to a directory C:\inetpub\wwwroot\mysite_instance2, update instance1 and switch it back, and it should do the job.
This is not currently supported due to the way in which IIS and the AspNetCoreModule works.
IIS passes an IHttpContext object to the AspNetCoreModule. The AspNetCoreModule uses the IHttpContext object to query the physical application path. The module then reads the web.config file found in the directory of the application. Once this has been done the module maintains a 1:1 relationship between the IIS application and the dotnet process.
I've raised this as a question and included some additional details on your behalf.
Asp.net application takes an eternity to start as it tries to copy compiled binaries to:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
As I visit additional pages, IIS caches additional dlls.
How can I get IIS to cache all dlls, for all pages immediately on the first hit to the initial page?
You would have to add warmup calls into each module used in each DLL into your Global.Application_Start. .NET 4.x has lazy initialization (to reduce initialization time by not initializing anything that you don't explicitly use. If you use everything up front, the initialization will be done at that time. You will need to ensure that every static member that is used gets used during the initialization. That will require a careful examination of the decompiled code for the DLLs you are using, or actually making calls to the any functions you want to prewarm.
I believe you are looking for application warmup
Some options to improve website performance comes with IIS server modules
You can have a look at Application Initialization Module for IIS 7.5
http://www.iis.net/downloads/microsoft/application-initialization
for IIS 6.0
IIS: web applications warmup
I am writing an MVC webAPI that will be used to return values that will be bound to dropdown boxes or used as type-ahead textbox results on a website, and I want to cache values in memory so that I do not need to perform database requests every time the API is hit.
I am going to use the MemoryCache class and I know I can populate the cache when the first request comes in but I don't want the first request to the API to be slower than others. My question is: Is there a way for me to automatically populate the cache when the WebAPI first starts? I see there is an "App_Start" folder, maybe I just throw something in here?
After the initial population, I will probably run an hourly/daily request to update the cache as required.
MemoryCache:
http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx
UDPATE
Ela's answer below did the trick, basically I just needed to look at the abilities of Global.asax.
Thanks for the quick help here, this has spun up a separate question for me about the pros/cons of different caching types.
Pros/Cons of different ASP.NET Caching Options
You can use the global.asax appplication start method to initialize resources.
Resources which will be used application wide basically.
The following link should help you to find more information:
http://www.asp.net/web-forms/tutorials/data-access/caching-data/caching-data-at-application-startup-cs
Hint:
If you use in process caching (which is usually the case if you cache something within the web context / thread), keep in mind that your web application is controlled by IIS.
The standard IIS configuration will shut down your web application after 20 minutes if no user requests have to be served.
This means, that any resources you have in memory, will be freed.
After this happens, the next time a user accesses your web application, the global asax, application start will be excecuted again, because IIS reinitializes your web application.
If you want to prevent this behaviour, you either configure the application pool idle timeout to not time out after 20minutes. Or you use a different cache strategy (persistent cache, distributed cache...).
To configure IIS for this, here you can find more information:
http://brad.kingsleyblog.com/IIS7-Application-Pool-Idle-Time-out-Settings/
I have Quartz.Net integrated for Scheduler job in my ASP.Net application.
But it is not working automatically and seems that it got stopped when IIS is recycling Application Pool. But fires when we send a request to server.
After reading IIS app pool recycle + quartz scheduling I am trying to configure the same in IIS 7.5 server for resolving the same.
<serviceAutoStartProviders>
<add name="PreWarmMyCache" type="PreWarmCache, MyAssembly" />
</serviceAutoStartProviders>
However PreWarmCache class has been defined in my website and kept all logic, since it uses template from website pages.
How can I define this class from website in type? What would be the value for MyAssembly ?
I can use assembly name if my project is web application.
I created as website. So what could be the value or how should I configure that section?
Note: PreWarmCache is placed under App_Code directory
This has nothing to do with Quartz.Net, but is to do with the IIS server recycling the application pool after a period of inactivity.
I have the same problem, but all I am trying to do is cache data.
So I need to do the following in my applicationHost.config file:
<serviceAutoStartProviders>
<add name="PreWarmMyCache" type="PreWarmCache, MyAssembly" />
</serviceAutoStartProviders>
This then call a function that populates an XML document and stores it as a lookup table in the cache to be used as and when needed.
And the problem is that if I use the AssemblyQualifiedName attribute it returns the following for my class:
MyApp.PreWarmCache, App_Code.<#########>, Version=0.0.0.0, Culture=neutral, PublickKeyToken=null
Where the ######### are, is changed each time the code is compiled.
I do not want to separate this into a DLL, as that would incur having to replicate the code.
So the question is still the same.
Can I/we provide an explicit assembly name for a dynamically compiled ASP.NET website's App_Code classes?
Fixed it now, taken code out into a separate assembly, compiled added reference, now all complete.
It is highly recommended that you do not use Quartz.NET in a web application. The application pools can, and do, reset. While they can be scheduled to recycle at certain times it's still possible for them to recycle at any time. This creates unpredictable behaviour and will be difficult to track down.
I highly recommend creating a Windows Service to handle your Quartz.NET tasks. It will be more predictable, easier to debug, and decoupled from your web application. This will remove the complexity of trying to keep the application pool on all the time to run a service.
If you still want to use Quartz.NET in your web application then this SO question may help.
I have a httpmodule that contains a property.
The httpmodule is used in my web application. I want to set the property in the httpmodule when my application starts and not have the overhead of setting it everytime the module is called.
The value for the application is read from my app settings in the web.config.
The httpmodule resides in a seperate dll to the web application.
So I want to inject/set the property from my web application on application start.
Any tips as to how I can achieve this?
Well, the application controls the creation and collection of modules, and beyond that IIS maintains a pool of application instances, so I don't think you can just set it once and be done with it.
I think the way to go would be to derive a class from HttpApplication and put your property there. When constructed, the application can read the app setting (meaning the config file access only happens once), and then when modules need it they can get it very efficiently from the Application, without touching the file system.