Wednesday, June 17, 2009

Debug windows service

In the time, when you work on development a windows service not possible to debug it. A solution is a converting service to console application or creating additional service runner or anymore. I want to present very simple, effective and easy way how to resolve this. In debug mode will run application and in not debug mode will run service.

[RunInstaller(true)]
public partial class ServiceRunner : ServiceBase
{
[STAThread]
static void Main()
{
#if !DEBUG
//Run Windows Service
Run(new ServiceRunner());
#else
//Run Application
new YourServiceBody();
#endif
}
public ServiceRunner()
{
InitializeComponent();

}
protected override void OnStart(string[] args)
{
new YourServiceBody();
}

protected override void OnStop()
{}
}

Good luck!
kick it on DotNetKicks.com

Sunday, April 05, 2009

Clone an object in C# using reflection for system and generic types

public object Clone()
{
object newObject = Activator.CreateInstance(GetType());
PropertyInfo[] propertyInfos = GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
if (propertyInfo.PropertyType.IsGenericType)
{
if (propertyInfo.PropertyType.GetInterface("IList", true) != null)
{
IList oldList = propertyInfo.GetValue(this, null) as IList;
if (oldList != null && oldList.Count > 0 &&
oldList[0].GetType().GetInterface("ICloneable", true) != null)
{
IList newList = (IList)propertyInfo.GetValue(newObject, null);
foreach (object obj in oldList)
{
ICloneable clone = (ICloneable)obj;
newList.Add(clone.Clone());
}
}
else
{
propertyInfo.SetValue(newObject, oldList, null);
}
}
if (propertyInfo.PropertyType.GetInterface("IDictionary", true) != null)
{
IDictionary oldDic =
propertyInfo.GetValue(this, null) as IDictionary;
if (oldDic != null && oldDic.Count > 0 &&
oldDic[0].GetType().GetInterface("ICloneable", true) != null)
{
IDictionary newDic =
(IDictionary)propertyInfo.GetValue(newObject, null);
foreach (DictionaryEntry entry in oldDic)
{
ICloneable clone = (ICloneable)entry.Value;
newDic[entry.Key] = clone.Clone();
}
}
else
{
propertyInfo.SetValue(newObject, oldDic, null);
}
}
}
else
{
//Clone IClonable object
if (propertyInfo.GetType().GetInterface("ICloneable", true) != null)
{
ICloneable clone = (ICloneable)propertyInfo.GetValue(this, null);
propertyInfo.SetValue(newObject, clone.Clone(), null);
}
else
{
propertyInfo.SetValue(
newObject, propertyInfo.GetValue(this, null), null);
}
}
}
return newObject;
}



Thursday, September 25, 2008

Snippet Designer for Visual Studio 2008

The Snippet Designer is a plugin which enhances the Visual Studio IDE to allow a richer and more productive code snippet experience.

Home Page

Thursday, August 14, 2008

Multi Sorting array of objects

A method sort (asc and desc) array of objects by properties and return sorted array.
private static T[] MultiSortObjectArray<T>(T[] objects,
bool isReverseSort, params string[] propertyNames)
{
//Get array object type
PropertyInfo[] propertiesInfo = (typeof (T)).GetProperties();
//Define list of properties for sorting
List<PropertyInfo> lstProperties2Compare = new List<PropertyInfo>();

//Check sorting properties one by one
foreach (string sortPropertyName in propertyNames)
{
//Find sorting property in Object property
PropertyInfo sortProperty =
Array.Find(propertiesInfo,
delegate(PropertyInfo property)
{
return (
string.Compare(property.Name, sortPropertyName,
true) == 0
);
}
);

//Add existing property to sorting list
if (sortProperty != null)
lstProperties2Compare.Add(sortProperty);
}

if (lstProperties2Compare.Count == 0)
return null;

Array.Sort(objects,
delegate(T x, T y)
{
int result = -1;
foreach (PropertyInfo propInfo2Compare in
lstProperties2Compare)
{
result = (!isReverseSort)
? //Sort by asc
(new CaseInsensitiveComparer()).Compare(
propInfo2Compare.GetValue(x, null),
propInfo2Compare.GetValue(y, null)
)
: //Sort by desc
(new CaseInsensitiveComparer()).Compare(
propInfo2Compare.GetValue(y, null),
propInfo2Compare.GetValue(x, null)
);
//If x equals y continue compare process
if (result != 0)
break;
}

return result;
}
);

return objects;
}





kick it on DotNetKicks.com

Wednesday, May 14, 2008

Sys.WebForms.PageRequestManagerParserError in MS AJAX

Thanks to Al Pascual to solution.

On top of the webform add:

enableEventValidation="false"



To full article lick Here

Monday, April 28, 2008

Get column info in MS SQL

Existing some ways how to get column information, but Joe Webb offered very simple and effective query:

SELECT 
ORDINAL_POSITION
,COLUMN_NAME
,DATA_TYPE
,CHARACTER_MAXIMUM_LENGTH
,IS_NULLABLE
,COLUMN_DEFAULT
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME ='TABLE_NAME'
ORDER BY
ORDINAL_POSITION ASC;

Tuesday, April 08, 2008

VS 2005 Intellisense in web.config files stop work

Now one annoying gotcha:

There is one gotcha to be aware of, though, that can sometimes cause intellisense for the web.config file to stop working in the IDE. This happens when a default namespace is added to the root <configuration> element. For example, like so:

<configuration xmlns=
"http://schemas.microsoft.com/.NetConfiguration/v2.0">





This doesn’t cause any runtime problems – but it does stop intellisense completion happening for the built-in .NET XML elements in the web.config file.

The bad news is that the built-in web admin tool (launched via the WebSite->ASP.NET Configuration menu item in VS 2005 and Visual Web Developer) always adds this xmlns namespace when it launches – so if you use this tool to manage users/roles you’ll end up having it added to your web.config file for you.

How to fix this gotcha:

To get intellisense back when you are editing the web.config file in the IDE, just delete the xmlns reference and have the root configuration element look like so:

<configuration>



Everything will then work fine again.


via


Thursday, March 27, 2008

HttpWebRequest over SSL

public static bool
AcceptAllCertificatePolicy(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}

The returned value determines whether the specified certificate is accepted for authentication.

Then set:

ServicePointManager.ServerCertificateValidationCallback
+= AcceptAllCertificatePolicy;

From here



Tuesday, March 18, 2008

Sunday, March 09, 2008

Singleton Factory in C#

    // this is the class for which
// I want to maintain a single instance
public class MyClass
{
private MyClass()
{
/* private constructor ensures that
callers cannot instantiate an
object using new() */
}
}

// Singleton factory implementation
public static class Singleton<T> where T : class
{
// static constructor,
//runtime ensures thread safety
static Singleton()
{
// create the single instance
// of the type T using reflection
Instance = (T)Activator.CreateInstance(
typeof(T),true);
}

// serve the single instance to callers
public static T Instance { private set; get; }
}

class Program
{
public static void Main()
{
// test
Console.WriteLine(
Object.ReferenceEquals(
Singleton<MyClass>.Instance,
Singleton<MyClass>.Instance));
}
}




via Cognitive Coding

Thursday, March 06, 2008

Monday, February 18, 2008

Enum Utilities

In this article I will discuss some classes I've written to simplify working with enumerations. The primary thrust of these classes is added functionality, but in some cases there are performance improvements as well.

Click here

Download source

The zip file contains:
EnumDefaultValueAttribute.cs
EnumTransmogrifier.cs
LibEnum.cs
build.bat
csc.rsp
EnumDemo1.cs
EnumDemo2.cs
EnumDump.cs
MonthEnum.cs
PolyglotAttribute.cs
WeekdayEnum.cs

Once you extract the files to a directory you should be able to execute build.bat to compile the demo programs. (They are console applications.)

To use the methods in your own projects, simply add the appropriate files.