My last two posts (How to intercept all incoming requests with WebAPI, and How to store data per request with WebAPI) were actually components of this, a bit bigger, solution I was working on two days ago. Basically I had a need for intercepting and logging all of the unhandled exceptions. This is what I’ve […]
Tag: .net

How to store data per request with WebAPI
This is going to be a short one. If you need to store some data and make it available to methods the most logical place to me is HttpContext. More precisely HttpContext.Current.Items collection which should always be available during a request processing. If you are suffering from a sport lesion you may a need a […]

How to intercept all incoming requests with WebAPI
As a part of a bigger task, I had to intercept all of the incoming client requests to our WebAPI controllers. I considered several options, like fiddling with HttpModule, or using the Application_BeginRequest method in Global.asax.cs, but settled for whole another one. Basically what I suggest you do is create a public class which inherits […]

How to check for IIS version installed.
Most of us actually ran into this. I just did once again. There’s a support.microsoft.com instructions on how to obtain each particular IIS version. The page also lists default IIS versions for each individual MS Windows version (check out the table below). Version Obtained from Operating System 1.0 Included with Windows NT 3.51 SP 3 […]

Random DateTime in the range (C#)
So You need a random DateTime value in some range, and C# Random class does not provide Random.NextDateTime(from, to). Here’s a simple solution for this problem…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static DateTime NextDateTime(this Random rnd, DateTime dateTimeFrom, DateTime dateTimeTo) { //Calculate cumulative number of seconds between two DateTimes Int32 Days = (dateTimeTo – dateTimeFrom).Days * 60 * 60 *24; Int32 Hours = (dateTimeTo – dateTimeFrom).Hours * 60 * 60; Int32 Minutes = (dateTimeTo – dateTimeFrom).Minutes * 60; Int32 Seconds = (dateTimeTo – dateTimeFrom).Seconds; Int32 range = Days + Hours + Minutes + Seconds; //Add random number of seconds to dateTimeFrom Int32 NumberOfSecondsToAdd = rnd.Next(range); DateTime result = dateTimeFrom.AddSeconds(NumberOfSecondsToAdd); //Return the value return result; } |
As this method is written as an extension method to Random class, you’ll be able to use it just as a static Random class method (Random.NextDateTime(startDateTime, endDateTime). […]