Tuesday, May 24, 2011

Enable Session State in a WCF service

The concept of session in a WCF service is widely different from a typical ASP.NET application where the session is initiated and maintained on the server. Sessions in WCF are per call and are created in the context of the channel that received the service request. I will keep this simple. If you need access to Session data in a WCF service there are 2 things that need to happen. In the web.config of your service application, the aspnetcompatibility needs to be enabled like so....
1.
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibiltyEnabled="true"/>

2. Add the following line to the class that implements your service interface like so...

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

The AspNetCompatibilityRequirementsMode enum has three possible values, Allowed NotAllowed and Required. Set the value of this enum according to your needs.

System.Web.HttpContext unavailable in a service class library

I encountered this recently. I have a service that implements an aggregate interface. Each interface is implemented in a seperate class library. I needed access to System.Web.HttpContext in one of my projects. When I tried to add a reference to the System.Web namespace I did not see this namespace listed in the list of available namespaces. Intrigued I checked my project properties and saw that the project was referencing .Net Client Profile of .Netfrx40. I changed this to the full .Netfrx40 and tried adding the reference again and VOILA I was able to add the namespace reference to my project and now have access to HttpContext.

Happy Coding!


Monday, May 23, 2011

PFX also known as the Parallel framework

If you have ever written multi-threaded code and wished to take advantage of multi cores/processors on your machine without writing a lot of code to partition your data and managing concurrency then you gotta take a look at the new task parallelism constructs offered by .NetFrx40. Thread concurrency is when your sequential code executes simultaneously amongst the cores in parallel. Also known as parallel programming, this is achieved by using the new PFX library. Parallel LINQ, the parallel class, the task parallelism constructs and the concurrent collections are collectively known as the PFX. The parallel class together with the task construct is called the Task Parallel Library or TPL.

So, how can the Parallel class help? Imagine you have a list that represents a collection of objects that you want some db operation to be performed on. Moreover, you want to take advantage of your hardware that has two dual core processors. If your collection has say, for instance 4 items in it, your sequential code will iterate over this collection using the same thread.

 foreach(var m in list)                                                                                                                                      { do some db processing }

When you use the Parallel.Foreach construct you can then do the same work over 4 threads executing simultaneously, one on each core. Your code would look something like the following.

Parallel.Foreach(list, ()=>

{
Same code as above in sequential code.
});

The runtime manages the work and aggregates the result from the originating thread/processor.

 A few things to keep in mind. When using the task parallelism constructs remember you still have to manage access to critical sections within your thread, so you do have to lock access to critical sections within your threads.

I benchmarked the performance difference and it is in the order of magnitude in my project.                                                                               



Thursday, May 19, 2011

Entity Framework: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

I had a piece of sequential code that executed in its own task (PFX). This was a sequential foreach loop. I changed this foreach to Parallel.Foreach and my code bombed. I do not think that the issue had anything to do with me trying to execute the foreach loop on all the cores at once. The actual problem was that I was retuning a value from within a using statement and EF did not like it.

Once the removed the return value from my method outside the using statement all is well.

Monday, May 2, 2011

log4net: No appenders could be found for logger

This is my initial foray into Log4Net for use with my WCF service. Since Log4Net is configuration based, if you do not see anything logged to your destination (in my case it is a database) then you have missed something in your config (that should not be any surprise, my point is to reinforce the reader to double check their config settings). In my particular case I am using the AdoNetAppender. I configured my database, installed and referenced the Log4net dll's and set about to log my first exception. After messing with it for a couple of hours I realized that I was missing the <appender-ref ref="AdoNetAppender" /> key in my config. The exception that was being thrown by Log4Net was

log4net: Logger: No appenders could be found for logger [PSLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] repository [log4net-default-repository]
log4net: Logger: Please initialize the log4net system properly.
log4net: Logger:    Current AppDomain context information:
log4net: Logger:       BaseDirectory   : C:\$\1.0\PSAuditService\PSAuditService\
log4net: Logger:       FriendlyName    : dbd5742-18-129488220968600299
log4net: Logger:       DynamicDirectory: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\ec718ec8\d90fe912

After adding the afore mentioned key all seems to be well.

Hope this helps someone.