13
Nov

WCF,WPF and WWF in .Net

Author: admin

What Is Windows Communication Foundation?

The global acceptance of Web services, which includes standard protocols for application-to-application communication, has changed software development. For example, the functions that Web services now provide include security, distributed transaction coordination, and reliable communication. The benefits of the changes in Web services should be reflected in the tools and technologies that developers use. Windows Communication Foundation (WCF)…

Read more…

Reference : http://source.witssquare.com/

VN:F [1.7.4_987]
Rating: 3.0/5 (2 votes cast)
VN:F [1.7.4_987]
Rating: +2 (from 2 votes)

30
Oct

While debugging a managed code batch process today I ran into VS throwing a ‘ContextSwitchDeadlock MDA’ error. The debugger was attached to several long running process’s and towards the end of the cycle VS was reporting this issue. As Mike Stall reports it seems to be caused by the thread running the managed code I am debugging timing out but the unmanaged thread seeing not seeing it as dead. Why the managed code I am running times out in the debugger thread I am not clear about. It seems like that the debugger has a fixed response to all break points that starts at execution time, as each step I am debugging goes off to the database, waits a while and then comes back, it eats into this time. Eventually the debugger thread times out and the ContextSwitchDeadlock error occurs. You can’t stop this kind of error (although I do wonder if the time out value on the debugger thread could be adjusted) but you can disable the warning.

Two tips I extracted from this thread is,

  • First you don’t already have it (Debug – Exceptions) obtain the debug exceptions window (right click tool bar – customize – debug and drag exceptions into the debug menu).
  • Select from Debug – Exceptions – Managed Debugging Assistants and disable ContextSwitchDeadlock.

I had no luck with trying to disable this via reg keys and config files but that is suggested in the MDA help files.

Also Reffer

VN:F [1.7.4_987]
Rating: 5.0/5 (1 vote cast)
VN:F [1.7.4_987]
Rating: +1 (from 1 vote)

3
Jul

Components using ASP.NET,C#

Just Create a class file for Components.cs in ASP.NET Application

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//Import Namespaces
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Collections.Generic;

public class Components
{
#region Member variables for Sql Connections
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da;
static int m, n, p;
string Day, Month;
string[,] temporary;
string[] t;
#endregion
#region Constructor
public Components()
{
this.OpenConnection();
}
#endregion
#region Destructor
~Components()
{
this.CloseConnection();
}
#endregion
#region Open the Connection
public void OpenConnection()
{
try
{
string s = ConfigurationManager.AppSettings.Get(“SqlConnection”);
this.con = new SqlConnection(s);
this.con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString() + “Exception Caught While Open the Connection”);
}
}
#endregion
#region Close the Connection
public void CloseConnection()
{
try
{
if (this.con.State != ConnectionState.Open)
this.con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString() + “Exception Caught While Closing the Connection”);
}
}
#endregion
#region InsertUpdateDelete
public Array InsertUpdateDelete(string[,] MyInParam, string MySpName)
{
try
{
cmd = this.con.CreateCommand();
cmd.CommandText = MySpName.ToString();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
for (int i = 0; i < MyInParam.Length / 2; i++)
{
cmd.Parameters.AddWithValue(MyInParam[i, 0], MyInParam[i, 1]);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

n = dt.Rows.Count;
m = dt.Columns.Count;
p = n * m;
int k = 0;
t = new string[p];
int j = 0;
if (p == 0)
{
return null;
}
else
{
for (int i = 0; (i <= n – 1) && (k < p); i++)
{
t[k] = dt.Rows[i][j].ToString();

if ((i == n – 1) && (k <= p))
{
j++;
i = -1;
}
k++;
}
return t;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + “Exception Caught while InsertUpdateDelete”);
return (t);
}
}
#endregion
#region DataSetSelect
public DataSet DataSetSelect(string[,] Myinparam, string Myspname)
{
try
{
cmd = this.con.CreateCommand();
cmd.CommandText = Myspname.ToString();
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Myinparam.Length / 2; i++)
{
cmd.Parameters.AddWithValue(Myinparam[i, 0], Myinparam[i, 1]);
}
da = new SqlDataAdapter(cmd);
da.Fill(ds);
return (ds);
}
catch (Exception ex)
{

string sss = ex.Message.ToString();
MessageBox.Show(ex.Message.ToString() + “Exception Caught while DataSetSelect”);
return (ds);
}
}
#endregion
#region DataTableSelect
public DataTable DataTableSelect(string[,] Myinparam, string Myspname)
{
try
{
cmd = this.con.CreateCommand();
cmd.CommandText = Myspname.ToString();
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Myinparam.Length / 2; i++)
{
cmd.Parameters.AddWithValue(Myinparam[i, 0], Myinparam[i, 1]);
}

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return (dt);
}
catch (Exception ex)
{

MessageBox.Show(ex.Message.ToString() + “Exception Caught while DataTableSelect”);
return (dt);
}
}
#endregion
#region DataReaderSelect
public SqlDataReader DataReaderSelect(string[,] Myinparam, string Myspname)
{
try
{
cmd = this.con.CreateCommand();
cmd.CommandText = Myspname.ToString();
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Myinparam.Length / 2; i++)
{
cmd.Parameters.AddWithValue(Myinparam[i, 0], Myinparam[i, 1]);
}
dr = cmd.ExecuteReader();
dr.Read();
return (dr);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + ” Page => Components => DataReaderSelect”);
return (dr);
}
}
#endregion
#region SelectInOutParam
public Array SelectInOutParam(string[,] MyInParam, string[,] MyOutParam, string MySpName)
{
try
{
string result;
cmd = this.con.CreateCommand();
cmd.CommandText = MySpName.ToString();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
for (int i = 0; i < MyInParam.Length / 2; i++)
{
cmd.Parameters.AddWithValue(MyInParam[i, 0], MyInParam[i, 1]);
}
for (int i = 0; i < MyOutParam.Length / 2; i++)
{
if (MyOutParam[i, 1] == “SqlDbType.VarChar,150″)
{
cmd.Parameters.Add(MyOutParam[i, 0], System.Data.SqlDbType.VarChar, 150);
}
else
{
cmd.Parameters.Add(MyOutParam[i, 0], System.Data.SqlDbType.Int, 4);
}
cmd.Parameters[MyOutParam[i, 0]].Direction = System.Data.ParameterDirection.Output;
}
cmd.ExecuteNonQuery();
temporary = new string[MyOutParam.Length / 2, 2];
for (int i = 0; i < MyOutParam.Length / 2; i++)
{
result = Convert.ToString(cmd.Parameters[MyOutParam[i, 0]].Value);
temporary[i, 0] = MyOutParam[i, 0];
temporary[i, 1] = result;
}
return temporary;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + “Exception Caught while SelectInOutParam”);
return temporary;
}
}
#endregion
#region Encrypt(PlainText)
public string Encrypt(string strPlainText)
{

string plainText = strPlainText;        // original plaintext
string passPhrase = “Pas5pr@se”;        // can be any string
string saltValue = “s@1tValue”;         // can be any string
string hashAlgorithm = “SHA1″;          // can be “MD5″
int passwordIterations = 2;             // can be any number
string initVector = “@1B2c3D4e5F6g7H8″; // must be 16 bytes
int keySize = 128;                      // can be 256 or 192 or 128

return Encrypt(plainText, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize);

}
#endregion
#region Encrypt(Set Key for PlainText)
public string Encrypt(string plainText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
{

byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
keyBytes,
initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
encryptor,
CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);

return cipherText;

}
#endregion
#region Decrypt(PlainText)
public string Decrypt(string strCryptedText)
{

string cipherText = strCryptedText;     // original plaintext
string passPhrase = “Pas5pr@se”;        // can be any string
string saltValue = “s@1tValue”;         // can be any string
string hashAlgorithm = “SHA1″;          // can be “MD5″
int passwordIterations = 2;             // can be any number
string initVector = “@1B2c3D4e5F6g7H8″; // must be 16 bytes
int keySize = 128;                      // can be 256 or 192 or 128

return Decrypt(cipherText, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize);

}
#endregion
#region Decrypt(Reset Key for CipherText)
public string Decrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
{

byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
keyBytes,
initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream,
decryptor,
CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes,
0,
plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
string plainText = Encoding.UTF8.GetString(plainTextBytes,
0,
decryptedByteCount);
return plainText;
}
#endregion
public string getDateTime()
{
switch (DateTime.Now.DayOfWeek.ToString())
{
case “Sunday”:
{
Day = “Sun”;
} break;
case “Monday”:
{
Day = “Mon”;
} break;
case “Tuesday”:
{
Day = “Tue”;
} break;
case “Wednesday”:
{
Day = “Wed”;
} break;
case “Thursday”:
{
Day = “Thu”;
} break;
case “Friday”:
{
Day = “Fri”;
} break;
case “Saturday”:
{
Day = “Sat”;
} break;
}
switch (Convert.ToInt32(DateTime.Now.Month.ToString()))
{
case 1:
{
Month = “Jan”;
} break;
case 2:
{
Month = “Mon”;
} break;
case 3:
{
Month = “Mar”;
} break;
case 4:
{
Month = “Apr”;
} break;
case 5:
{
Month = “May”;
} break;
case 6:
{
Month = “Jun”;
} break;
case 7:
{
Month = “Jul”;
} break;
case 8:
{
Month = “Aug”;
} break;
case 9:
{
Month = “Sep”;
} break;
case 10:
{
Month = “Oct”;
} break;
case 11:
{
Month = “Nov”;
} break;
case 12:
{
Month = “Dec”;
} break;
}
string GenDateTime = Day + ” ” + Month + ” ” + DateTime.Now.Day.ToString() + “, ” + DateTime.Now.Year.ToString() + ” ” + DateTime.Now.ToShortTimeString().ToString();
return (GenDateTime);
}
#region Simple Encoding and Decoding
public string Encode(string InputText)
{
int i;
char CurrentCharacter;
int CurrentCharacterCode;
string EncodedText = “”;

//Iterate through the length of the input parameter
for (i = 0; i < InputText.Length; i++)
{
//Convert the current character to a char
CurrentCharacter = System.Convert.ToChar(InputText.Substring(i, 1));

//Get the character code of the current character
CurrentCharacterCode = (int)CurrentCharacter;

//Modify the character code of the character, – this
//so that “a” becomes “n”, “z” becomes “m”, “N” becomes “Y” and so on
if (CurrentCharacterCode >= 97 && CurrentCharacterCode <= 109)
{
CurrentCharacterCode = CurrentCharacterCode + 13;
}
else

if (CurrentCharacterCode >= 110 && CurrentCharacterCode <= 122)
{
CurrentCharacterCode = CurrentCharacterCode – 13;
}
else

if (CurrentCharacterCode >= 65 && CurrentCharacterCode <= 77)
{
CurrentCharacterCode = CurrentCharacterCode + 13;
}
else

if (CurrentCharacterCode >= 78 && CurrentCharacterCode <= 90)
{
CurrentCharacterCode = CurrentCharacterCode – 13;
}

//Add the current character to the string to be returned
EncodedText = EncodedText + (char)CurrentCharacterCode;
}
return EncodedText;
}
#endregion
}

VN:F [1.7.4_987]
Rating: 0.0/5 (0 votes cast)
VN:F [1.7.4_987]
Rating: 0 (from 0 votes)