WCF Timeouts: To send or to receive, that is the question!

You decide to write a nice WCF service and you know the operation could take up to 5 minutes to complete. Well, what to do? What to do?

It is possible you say, well I well manually set the operation timeouts in code. That works but is annoying. How about setting the binding timeouts? There you go, nice and easy right?

Let’s take a look at the following binding:

<binding name="MyBinding" closeTimeout="00:00:30" openTimeout="00:00:30" receiveTimeout="00:05:00" sendTimeout="00:0:30">

It looks pretty accurate right? The operation takes 5 minutes or less, so it should work fine with the receiveTimeout being set to 5 minutes. No worries.

Well, YOUR WRONG!. Surprise!

The retarded illogical answer to the above quandary is that WCF uses the sendTimeout to figure out how long to wait to RECEIVE data. I know, painful. You will get timeout errors out the wazoo with the simple mixup of this little fact.

So, next time you setup your bindings, make sure to know the client really cares about the sendTimeout. It doesn’t seem to really concern itself with the receiveTimeout so much.

Here is the right binding:

<binding name="MyBinding" closeTimeout="00:00:30" openTimeout="00:00:30" receiveTimeout="00:05:00" sendTimeout="00:05:00" maxBufferSize="500000000" maxReceivedMessageSize="500000000">
 </binding>

Enjoy this little trickery and hopefully you save some time.

Happy coding!

VSTO “Old format or invalid type library” when automating Excel

I am working on a project that automates Excel and is used in countries around the world.  One of the hurdles we have faced was encountering this beauty of an error:

Error: 0x80028018 (-2147647512)
Description: Old Format or Invalid Type  Library

I imagine that it might be quite the lovely error message for some, but for us it was a bit of a pain in the arse.

You have a few options when dealing with this error. You can set the current thread to English explicitly before calling Excel methods. This allows you force the 1033 language code on the interop which works fine.

What if you have legacy code? Or maybe you don’t want to change your code in every place that calls Excel to change the thread culture. Maybe you actually want your threads to respect the current culture because your not a lame VBA programmer who only knows how to program for English.

So, I worked with the other solution. Make a folder in the Excel folder with the LCID of 1033 and one for the LCID of the CURRENT region set in Windows.

NOTE: DO NOT OVERWRITE ANY FILES IF THEY ALREADY EXIST, YOU WILL NOT BE HAPPY!

Copy the Excel.EXE to the that folder and rename it to XLLEX.DLL.

Voila! You are done.

I wrote an application that does all this automatically for you that is easy to use. If there is any interest, I will post it up here.  I don’t feel like wasting my time though if no one cares.

If you want to read the kb, click here.

Happy Coding!

Obvious Tip: How to turn on Tracing in Asp.net

I was wanting to look at my session state variables and a few other tracing bits in my ASP.NET application. It’s fairly easy to turn on and it makes for a nice and nasty look page.

Either way, it proves quite helpful to get those really ugly issues solved fast.

Just add this to your configuration file:

<trace enabled="true" requestLimit="40" localOnly="false"/>

example:

<configuration>
<system.web>
<trace enabled="true" requestLimit="40" localOnly="false"/>
</system.web>
</configuration>

Notice, localOnly needs to be false if you are not actually on the physical server. Duh!

You can read much more about this over @ Microsoft.

Happy Coding!

Storing NULLS in Enterprise Library Caching Block, maybe I want my NULL..

I am retrieving data where a NULL is quite a valid value to store. I am doing database retrievals and if the records/objects don’t exist in the database, then I don’t want another roundtrip each time simply because I think the data expired.

So, it’s not often this is a valid scenario, but when you need to store a NULL as a value that is cached then there is an easy approach.
I created a class to handle the storage of my actual values here:

/// Class to help with caching a NULL value, and knowing its really null.
public  class NullCacheItem<>
{
     /// Gets or sets the value.
     public T Value { get; set; }
     /// Gets a value indicating whether this instance is null.
     public bool IsNull
     {
          get { return Value == null; }
     }
}

The class above is a generic/template class that allows you to pass in the actual object type you are using in your cache. This makes it much easier to consume.
NOTE: Why not use the Nullable Template i.e. DateTime? . Well, I designed this for reference objects. Not for primitives. ? doesn’t work for nullable objects silly!

So, now in your code you can easily use this model. Below is some sample code.

public ConfigurationSection GetSection(string sectionName)
{
     var cacheSection = sectionCache.GetData(sectionName) as NullCacheItem<ConfigurationSection>;

     if (cacheSection == null)
     {
          try
          {
               lock (cacheLock)
               {
                    cacheSection = sectionCache.GetData(sectionName) as NullCacheItem<ConfigurationSection>;
                    if (cacheSection == null)
                    {
                         cacheSection = new NullCacheItem<ConfigurationSection>()
                         {
                              Value = SqlConfigurationManager.GetSection(sectionName, data) as ConfigurationSection
                         };

                    sectionCache.Add(cacheSection,
                         new AbsoluteCacheExpiration(new TimeSpan(0, _sectionCacheTimeout, 0)),
                         sectionName);
                    }
               }
          }
          catch (Exception ex)
          {
               // handle internal exception
               ExceptionManager.HandleException(ex);
          }
     }

     if (cacheSection != null)
          if (!cacheSection.IsNull)
               return cacheSection.Value;
     return null;
}

It’s very easy to implement and adds a lot of value.

Note: The above is real world code using the object. This provides the spirit of the code, but is not just copy paste run code.

Happy Coding!

Visual Studio 2008 Designer crashes when editing WPF xaml…

I have been banging my head on the wall about my Visual Studio just disappearing from view whenever I open one of my WPF forms. As you can imagine, this puts a pretty big cramp in development efforts when you can never use the designer.

I looked in my event viewer and found this error hanging out.

Event Type:	Error
Event Source:	.NET Runtime
Event Category:	None
Event ID:	1023
Date:		3/10/2010
Time:		11:03:02 AM
User:		N/A
Computer:	BSEEKFORD00111
Description:
.NET Runtime version 2.0.50727.3603 - Fatal Execution Engine Error (7A2E10D2) (0)

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

The resolution is that it turns out your probably installed Power Commands for Visual Studio 2008. You can uninstall them, which sucks. Or you can apply a really easy patch.

Go to your devenv.exe.config file sitting in your Program FilesMicrosoft Visual Studio 9Common7IDE

Add this to the bottom of the dependent assemblies section:

<dependentAssembly>

<assemblyIdentity name="Microsoft.PowerCommands" publicKeyToken="null" culture="neutral"/>

<codeBase version="1.1.0.0" href="C:Program FilesPowerCommandsMicrosoft.PowerCommands.dll"/>

</dependentAssembly>

Start Visual Studio back up and rock on.

Note: Make sure the path you enter matches where everything is installed. DUH!

Credits to these guys for all their help.

Happy Coding!