I have a situation where I am building a web service hosted on IIS that requires a license file to be on the filesystem before starting up successfully. I’m wondering where I should put the code that does the license check to prevent the Website from actually starting.
I have found there is a function Application_Start but this isn’t called until someone makes an initial request.
Ideally, the app would check the license and log an error in the Windows Event Log when the IIS Admin tries to start the website itself. Is this possible? If not, are there any best practices for this type of situation?
Bombing an entire website because a part of it that uses your component which isn’t registered is very bad form.
To that end, I you could put something in the static constructor(s) of your class(es) that checks the licensing and then throws an exception (any will do) if your licensing requirements are not found, like so:
If you really want to bomb the entire website, then you’ll more than likely have to do this outside of ASP.NET, and write an ISAPI filter (note, you have to do it in unmanaged code) and get it installed on the IIS server.
Or, you could implement the
IHttpModuleinterface. In theInitmethod implementation you could check for your licensing requirements. If the requirements are not set, then throw an exception, like so:(Note that you still have to register the
IHttpModuleimplementation)A better solution would be to implement
IHttpModule, but set a flag that your libraries/components can access as needed:The flag would subsequently be checked for in every constructor and/or static constructor to ensure compliance:
This would force installation of the module (without it, the flag is not set, which would cause an exception to the thrown), and should give you the opportunity to hook into every page request that uses your component and prevent usage if you wish.
If you do go this route, I have to reemphasize that it’s in very bad form to bomb the entire application, and you should really focus on your component when it is used; this isn’t really your call to make.
It’s very possible that the developer(s) have other pages that do not use your component, while using other paid components which should render just fine.
Also, if you hook into every request, you should make sure that you don’t do anything to incur a performance hit. That would also be in bad form as well. This is why you set the flag once and then do a simple check when you need to.