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: c#

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. So basically, in order to add the item you’ll need later during the […]

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 […]

Solution to the gcc error: undefined reference to `ilogb’ (C/C++)
The ilogb( ) functions return the exponent of their floating-point argument as a signed integer. If the argument is not normalized, ilogb( ) returns the exponent of its normalized value. Here’s simple program that uses ilogb()
1 2 3 4 5 6 7 8 |
#include <math.h> int main(int argc, char*argv[]){ double x = 1234.5678; int retval = ilogb(x); return 0; } |
Now, if You use this function and get the following gcc linker error: undefined reference to `ilogb’ […]

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). […]