Sunday, August 07, 2011

How to create list of anonymous types

I was writing a method and I needed to loop through a collection of objects, collecting a couple of fields of those objects based on some business conditions and store them into a temporary data structure. Anonymous type came into my mind right away. But how to store those anonymous types into a list? I googled without giving much thought and guess what – there is indeed a way and i liked it.

var listOfAnonymoustTypes = new[] { new { Id=1, Name="Name" }}.ToList();
That’s it. It is that easy and intuitive. Following contains the detailed discussion on this topic.
http://kirillosenkov.blogspot.com/2008/01/how-to-create-generic-list-of-anonymous.html

Tuesday, April 12, 2011

Firebird server (Fbserver) command line switches

Following table contains most common Firebird server command line switches.

-a run as application. Applicable only if Firebird runs as application.
-n no icon in the system tray. Applicable if Firebird runs as application.
-b run with high (boosted) priority.
-r run with normal priority. This is default priority.
-p specify port number/name or named pipe.
-i accept tcp/ip connections.
-l accept local connections
-w accept NETBEUI connections.

Reference - http://www.volny.cz/iprenosil/interbase/fbserver_switches.htm

Tuesday, April 05, 2011

How to use gfix on Windows

gfix -user SYSDBA -password masterkey localhost:C:\MyDatabase.fdb -v -f

gfix is Firebird’s command line tool for administration issues like data repair, sweeping etc. Above is the syntax for how to use this tool on a Windows machine.


  • gfix.exe is located in your Firebird installation directory.

  • -v option in the above example specifies that we want to validate the database.

  • -f options means full to mean all records and pages and release unassigned record fragments.

  • localhost specifies that database is located on this machine only.

Following page has details of all the options that can be used with gfix.


http://www.destructor.de/firebird/gfix.htm

Sunday, March 20, 2011

PresentationFontCache issue – IndexOutOfRangeException when running WPF application

A few days back, I came across this IndexOutOfRangeException with PresentationFontCache when running our WPF application on a Windows 7 machine. I tried disabling and enabling .NET framework from Add/Remove components but nothing seemed to fix this.

PresentationFontCache service optimizes performance of WPF applications by caching commonly used font data.

One of the following two fixes seem to work for different peoples.

  1. PresentationFontCache data is stored at [Windows]\ServiceProfiles\LocalService\AppData\Local\FontCache*.dat. Some of the guys have fixed this problem after deleting this file from here and restarting Windows Presentation Font Cache service.
  2. Disabling Windows Presentation Font Cache service worked for me. I don’t know why but just stopping the service did not work for me.

Refer to following thread for some more discussions -

http://social.msdn.microsoft.com/Forums/en/wpf/thread/2e47b80a-1423-466f-873a-8536a64ebe3c

Thursday, March 10, 2011

Large file transfer in WCF in IIS 7 - There was no endpoint listening at xxx that could accept the message

I had hosted a WCF web service on IIS 7 that needed to transfer large files (~100 MB) to and from the service. I had all of maxReceivedMessageSize, maxBufferSize, maxItemsInObjectGraph, maxRequestLength configured to sufficiently large values, but still i was getting above mentioned exception when transferring greater than 30 MB files.

In IIS 7, there is one more setting called maxAllowedContentLength which is by default 30000000 byte (just over 28 MB). You need to tweak this value according to your need. Following snippet shows where it should be placed in the web.config file.

<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="30000000" />
</requestFiltering>
</security>
</system.webServer>
</configuration>

 

This setting is not directly available from IIS manager, so if you need to change this using GUI tool you can download IIS 7.0 Admin Pack. You will find this setting from selecting your website and then going to Request Filtering –> Edit Feature Settings –> Maximum allowed content length (Bytes).

Sunday, March 06, 2011

ThreadAbortException:Thread was being aborted in IIS 7

Recently i was developing a WCF web service in IIS 7 on Windows Server 2008 and started getting this exception quite weirdly. Weirdly in the sense that the exception was not consistently being reported at the same call stack.

My web service was using App_Data to store some application specific data which was also being added/modified/deleted during runtime.

After doing a lot of searching and study, the behavior of writing to the App_Data turned out to be the culprit. It is really hard to guess what may be wrong. In fact, I learnt that heavily modifying application subdirectory shuts down the appdomain.

Following are two references that explain this thing -

http://blogs.msdn.com/b/toddca/archive/2005/12/01/499144.aspx

http://forums.iis.net/t/1121400.aspx

It seems that it has nothing to do with IIS 7 though and can happen even with IIS 6.

Sunday, January 30, 2011

What Firebird assemblies to ship with embedded Firebird .NET App?

For a .NET application that uses embedded Firebird, I would suggest to ship all of the following with you .NET app.

  1. All the *.dll in the embedded Firebird directory. Downloaded Firebird embedded zip file from Firebirdsql.org if you have not already downloaded. To be precise, following are the assemblies with respect to v2.5 of Firebird.
    • fbembedd.dll
    • ib_util.dll
    • icudt30.dll
    • icuin30.dll
    • icuuc30.dll
    • msvcp80.dll
    • msvcr80.dll
  2. intl and udf directory. Both of these directory should be there in Firebird embedded zip file you downloaded.

Note that all of these files/folders mentioned above should be at the exe location of your .NET application. Some of these assemblies might not be used at all in the application, but i found issues when i only shipped fbembedd.dll. Shipping all of the stuff mentioned above has been quite stable.

Saturday, January 15, 2011

Adding custom display value to Enum fields

Most of the times, display value for our custom enums are different from actual field value. We usually want to have different display string than actual field of the enum.

Thanks to extension method, we can extend Enum to add functionality to it. You can now add methods to an enum which was previously not possible. Here i am going to show two approaches of extending Enums to support display value for Enum fields.

Following is the 1st approach.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleWinApplication
{
public enum LoginStatus
{
LoggedIn,
LoggedOut,
Idle,
Away
}

public static class LoginStatusExtensions
{
public static string GetDisplayName(this LoginStatus loginStatus)
{
switch (loginStatus)
{
case LoginStatus.Away:
return "Away";
case LoginStatus.Idle:
return "Idle";
case LoginStatus.LoggedIn:
return "Logged in";
case LoginStatus.LoggedOut:
return "Logged out";
}
return loginStatus.ToString();
}
}

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

// this will "Logged out"
MessageBox.Show(LoginStatus.LoggedOut.GetDisplayName());
}
}


}

Here we add an extension method GetDisplayName to enum LoginStatus. The method returns display string depending on the given value of LoginStatus. Note that here you have full control over what display value to have for the given LoginStatus. This also has the advantage that you have consistent display value for LoginStatus throughout you application.

There is one problem with this approach that you have to add an extension method for every enum that you want to add custom display string to.

Following approach is a more generic solution to this problem using Attributes and extension methods.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleWinApplication
{
[Serializable]
[AttributeUsage(AttributeTargets.Field, Inherited = false)]
public class EnumDisplayInfoAttribute : Attribute
{
public string DisplayString { get; private set; }

public EnumDisplayInfoAttribute(string displayString)
{
this.DisplayString = displayString;
}
}

public enum LoginStatus
{
[EnumDisplayInfo("Logged in")]
LoggedIn,

[EnumDisplayInfo("Logged out")]
LoggedOut,

[EnumDisplayInfo("Idle")]
Idle,

[EnumDisplayInfo("Away")]
Away
}

public static class EnumExtensions
{
public static string GetDisplayName(this Enum targetEnumValue)
{
object displayInfoAttribute = targetEnumValue.GetType().GetField(targetEnumValue.ToString()).GetCustomAttributes(typeof(EnumDisplayInfoAttribute), false).FirstOrDefault();
if (displayInfoAttribute != null)
{
return (displayInfoAttribute as EnumDisplayInfoAttribute).DisplayString;
}
return targetEnumValue.ToString();
}
}

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

// this will "Logged out"
MessageBox.Show(LoginStatus.LoggedOut.GetDisplayName());
}
}


}

Here we add extension method to the base Enum class and thus it is available to every Enum. We also define an Attribute EnumDisplayInfoAttribute that you need to apply to enum fields that you want to support display string for. Note that for any new enums that you add in your application, you just need to decorate its fields with EnumDisplayInfoAttribute and they will start supporting display string feature.

Also, if there is no attribute applied to given enum field, we just return the ToString().

You can use either of the two approaches. I personally find 2nd approach more elegant and readable because the display string is defined just where the enum is.

Friday, January 07, 2011

Get all the columns of a given table in Firebird database

Following sql script gives all the columns of a given table (‘Employee’ in this example) in a Firebird database.

SELECT Fields.RDB$FIELD_NAME "Column Name" FROM RDB$RELATION_FIELDS Fields
WHERE Fields.RDB$RELATION_NAME = 'EMPLOYEE' and Fields.RDB$SYSTEM_FLAG = 0
ORDER BY Fields.RDB$FIELD_POSITION