Posts

Showing posts from February, 2011

Merge the state of two objects in C# using reflection

if (fromObject == null || toObject == null) { return; }             PropertyInfo[] properties = typeof(T).GetProperties();             object value = null;             for (int i = 0; i < properties.Length; i++)             {                 if (!properties[i].CanWrite) { continue; }                 value = null;                 IList list = properties[i].GetValue(fromObject, null) as IList;                 if (list != null)                 {                     //Is a list.                     value = list;                 }                 else                 {                     value = properties[i].GetValue(fromObject, null);                 }                 if (doNotMergeIfValueNull && value == null)                 {                     //do nothing.                 }                 else                 {                     properties[i].SetValue(toObject, value, null);                 }             }

Check if something is listening at a port on a server using C#

IPAddress [] hostIPAddresses = Dns.GetHostAddresses(hostName);             IPAddress selectedIPAddress = null;             if (hostIPAddresses == null || hostIPAddresses.Length == 0) { return ListenerStatus.Unknown; }             for (int i = 0; i < hostIPAddresses.Length; i++)             {                 if (hostIPAddresses[i].AddressFamily == AddressFamily.InterNetwork)                 {                     selectedIPAddress = hostIPAddresses[i];                     break;                 }             }             if (selectedIPAddress == null) { return ListenerStatus.Unknown; }             Socket socketInstance = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);             try             {                 socketInstance.Connect(selectedIPAddress, port);                 if (socketInstance.Connected == true)                 {                     //Port is in use and connection is successful                               retu

Retrieving WCF Configuration from C#

WcfConfigDetails details = new WcfConfigDetails();             details.Populated = false;             Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);             ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);             BindingsSection bindings = serviceModel.Bindings;             ChannelEndpointElement element = (from ChannelEndpointElement p in serviceModel.Client.Endpoints                                               where p.Name == endPointName                                               select p).FirstOrDefault();             if (element == null) { return details; }             details.Port = element.Address.Port;             details.HostName = element.Address.Host.ToString();             details.Populated = true;             return details;

Compression & Decompression in C#

Many people search for this. Note I have XML serialized the object because if you do binary serialization, if there are breaking changes in the class, then it will not de-serialize. To compress, do this: CompressionOutput output = new CompressionOutput();             byte[] xmlBytes = Encoding.UTF8.GetBytes(largeString);             MemoryStream memoryStream = new MemoryStream();             try             {                 using (GZipStream zipStream = new GZipStream(memoryStream, CompressionMode.Compress))                 {                     try                     {                         zipStream.Write(xmlBytes, 0, xmlBytes.Length);                     }                     finally                     {                         zipStream.Flush();                         zipStream.Close();                     }                 }                 byte[] compressedData = memoryStream.ToArray();                               if (compressedData.Length < x

How to link Google Analytics to your RSS feed so that you can track how many people visited the feed in ASP.NET

I spent a few days puzzling over this without luck then it hit me! Just create a dummy page which has the Google Analytics JS embedded in it, in the OnInit of your RSS feed, use the System.Net.WebClient to load the dummy ASPX page dynamically: protected override void OnInit(EventArgs e)     {         base.OnInit(e);         try         {             using (WebClient objWebClient = new WebClient())             {                 objWebClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");                 Stream data = objWebClient.OpenRead(BASE_URL + "DummyPage.aspx");             }         }         catch { }     }

Distributed Transactions

I recently had a chat with a friend who thought that if we do ADO.NET transactions, those would be distributed transactions. That is obviously incorrect, so I thought let me clarify for everyone here. A Distributed transaction is a transaction which involves multiple resource managers. These could be MSMQ and SQL Server or 2 SQL Server Instances, or SQL Server and Oracle.. etc. Each of these resource managers support transactions within themselves, using that does not mean you are creating a Distributed Transaction, which is actually much more expensive, but necessary in some scenarios where you need transactional integrity. To implement this using .NET, you have to register the assembly with COM+ and use some attributes. It is very interesting and Juval Lowy had written an interesting book on this subject quite some time ago, which also discusses Queued Components interestingly...