Aug 20, 2009

Service endpoint binding not using the HTTP protocol

If you are invoking a WCF service and if you are getting the error
"This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."
Then, most probably you have a problem with enums exposed via WCF. [Even otherwise, this post will be useful to you].

Either case, SvcTraceViewer is your life saver. It helps trace the internal method invocations, plumbing etc in the WCF calls.

Steps to run traceviewer:
1) Configure the WCF to enable trace
2) Run the SvcTraceViewer
3) Open the trace log per the settings in your WCF config file
4) Look for errors. Mostly highlighted in red color.
Info for the above steps available here in the microsoft site.
5) You are not done yet. If it's caused due to enums(as in my case), you must read this blog post that explains the deeper issue underneath using enums in WCF.
Reblog this post [with Zemanta]

Aug 18, 2009

Checking NULL in LINQ and SQL

I stepped on a bit field in my table which has NULL as default value.

Checking in my LINQ code for not true condition as below just failed:
p.IsValidLicense != true
The row in the table had the value NULL and therefore i expected my query to return true, but in vain.

Then i executed it as SQL query in SSMS as below. Again unexpected result.
Where IsValidLicense <> 1
Time for google. Seems, the NULL issue has caused lot of miseries in the lives of a lot of people. Everyone just jumped in to answer my question.

The correct way to construct the query..

In SQL:
(IsValidLicense IS NULL OR IsValidLicense = 0)
In LINQ:
(p.IsValidLicense == null || p.IsValidLicense == false)
Hope this helps!
Reblog this post [with Zemanta]

Tutorials on LINQ to Entities and Entity Framework

Came across a couple of very interesting articles on Entity Framework and LINQ to Entities. The concept explanation is simple and easy to understand even for a beginner. Samples are great. Must read for anyone interested in getting a better idea of Entities.

Checkout the links. The ones i found interesting:

ADO.NET Entity Framework & LINQ to Entities - part 1
ADO.NET Entity Framework & LINQ to Entities - part 3
Reblog this post [with Zemanta]

Multiple filters in LINQ

It all started with a need to have multiple filter parameters in a LINQ query. I didn't want to cascade results from one filter to another.

Following describes the approach to build multiple, dynamic, conditional filters into a LINQ query.
The approach uses LINQKIT to achieve this.

Problem definition:
I have a questions table with columns questionid, question, questiontype, questionrating etc. And, i would like to filter based on questiontype and questionrating. Also, i would like to choose either or none of the filters dynamically while executing the LINQ query. My front end UI has two drop downs one each for questiontype and questionrating. I may choose to apply or ignore one or more of the filters during run time.

One way to achieve this would be using Expression trees in Entity framework. Another easier approach(explained below) would be to use the LINQKIT and PredicateBuilder.

Solution:
Given the above problem definition, i have used LINQKIT and PredicateBuilder to easily solve the problem. I have used OR condition filter in my sample below.

var conditions = PredicateBuilder.False();

//check the applicability of filters
IDictionary filterDict = new Dictionary();
long temp = 0;

if (filter.QuestionType != questionTypeEnum.none)
{
temp = (long)filter.QuestionType;
conditions = conditions.Or(e => e.Qbank_type.typeIdInt == temp);
}
if (filter.QuestionRating != questionRatingEnum.none)
{
temp = (long)filter.QuestionRating;
conditions = conditions.Or(e => e.Qbank_questionRating.ratingIdInt == temp);
}

questionsResult = objectContext.questionsSet.AsExpandable().Where(conditions).OrderBy(e => e.questionId);
Related articles for an interesting read.
PredicateBuilder
LINQKIT (includes PredicateBuilder)

Reblog this post [with Zemanta]

Aug 16, 2009

Ordering unit tests in Visual Studio

Unit testing framework in Visual Studio is very handy and encourages test-driven development.

The most important thing about tests is the ordering and that's why a separate kind of test called "Ordered tests" are available in Visual Studio.

Steps:
1) Create your unit tests
2) Create an ordered test and select the unit tests to be executed and the order of execution

Advantages:
1) You can add the unit tests multiple times, which helps in let's say you want to add data, delete data and delete data again. The first delete is positive testing and second delete being negative testing.
2) You can decide whether to continue on failure of any of the tests, that way you don't have to execute tests unnecessarily if a dependent test has failed.

Refer to this MSDN link for steps to create ordered tests.
http://msdn.microsoft.com/en-us/library/ms182631.aspx

To learn more on Test Driven Development(TDD)
http://en.wikipedia.org/wiki/Test-driven_development
Reblog this post [with Zemanta]

Aug 11, 2009

UTC and Day light savings nightmare.

When it comes to using date and time information in a global software application, there is a crucial need to store the date and time as UTC. The reasons being:

- Storing time in UTC on the database allows the app to render localized data correctly. [using datetimeoffset also helps. the choice is purely situational and environmental factors]

- With the cloud computing era, we may never know the geographic location of our server and therefore assuming a particular timezone in the coding or data store logic could be catastrophic. UTC solves this problem by converting the local server timezone to a standard time value that is unique at a given point on time in history.

UTC is a panacea. There are serious problems associated with UTC with regard to the daylight savings. The below examples are excerpts from an exhaustive article on MSDN(link in the references section at the end) which explains the seriousness of the problem, when left unchecked.

- The harder-to-manage case is the ambiguity case associated with parsing user input that occurs during this magical hour of daylight savings in the spring and fall.

- Presently there is no way to parse a string that represents a user's view of time and have it accurately assigned a universal time value. The reason is that people who experience daylight savings time don't live in places where the time zone is Greenwich Mean Time. Thus, it is entirely possible that someone living on the east coast of the United States types in a value like "Oct 26, 2003 01:10:00 AM".

On this particular morning, at 2:00 AM, the local clock is reset to 1:00 AM, creating a 25-hour day. Since all values of clock time between 1:00 AM and 2:00 AM occur twice on that particular morning—at least in most of the United states and Canada. The computer really has no way to know which 1:10 AM was meant—the one that occurs prior to the switch, or the one that occurs 10 minutes after the daylight savings time switch.

Similarly, your programs have to deal with the problem that happens in the springtime when, on a particular morning, there is no such time as 2:10 AM. The reason is that at 2:00 on that particular morning, the time on local clocks suddenly changes to 3:00 AM. The entire 2:00 hour never happens on this 23-hour day.

Your programs have to deal with these cases, possibly by prompting the user when you detect the ambiguity. If you aren't collecting date-time strings from users and parsing them, then you probably don't have these issues.

Recommendations:
Programs that need to determine whether a particular time falls in daylight savings time can make use of the following(in .NET):

Timezone.CurrentTimeZone.IsDaylightSavingTime(DateTimeInstance)
or
DateTimeInstance.IsDaylightSavingTime

When you are coding and desire to store current time represented as universal time, avoid calling DateTime.Now() followed by a conversion to universal time. Instead, call the DateTime.UtcNow function directly.
The impact on an application, purely depends on the type of application. The impact will be very high for financial, ecommerce, healthcare, logistics etc where time value is crucial and controls business logic.

References:
Reblog this post [with Zemanta]