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.
Related
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..
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 am working on a project that is a website, a mobile app, and a desktop WPF app that all depend on a service. The mobile app works fine, but the desktop and website was having a problem with getting images from the database because of a MaxArrayLength property. We were able to change the web.config file's maxArrayLength property and the website now works, but the desktop application is still broken. We know we should change something in the App.config file, but can't figure out where the maxArrayLength property should be (what tag it's under, etc).
We currently have a direct reference through the desktop to the service, and a service reference through the website. Is there any way to do this without adding a service reference and just being able to keep the direct reference to the service?
Is there any way to do this without
adding a service reference and just
being able to keep the direct
reference to the service?
Why would you want to do that?
If you are referencing the WCF project directly, only hitting some included business logic, your solution might need some project refactoring. Ie., you should have business logic that is used by all your clients in a separate project in order to keep cohesion high.
If you need to call the WCF services to actually access the provided services (and not only call exposed business logic, which might be what you are doing, if my understanding is correct), then you will most likely want to do one of either options:
Option A
Use a service reference (and not a project reference) in order to call the WCF services via an auto-generated proxy.
Option B
Use a facility (with some configuration) and an IoC container to resolve dependencies on your WCF services. See this article for some clues on how to get started. This example uses Castle's very simple WCF Integration Facility.
I estimate implementing WSSE authentication by extending code similar to this custom authentication module.
That code will be hosted in ASP.NET and registered in <system.web><httpModules> section in web.config file of the site. An instance of the module will be created by some code inside IIS, passed the request, live for some time and then destroyed.
In order to implement WSSE I need to somehow keep track of issued challenges ("nonces") to be able to prevent replay attacks.
So I need to store recent challenges somewhere in such way that the collection will persist between incoming requests and will be accessible by all the module instances.
What is the most convenient (in terms of using it from C# and deploying it on a new server) and most typical solution for that?
Session["mySess"] can also help you