Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, September 29, 2009

LINQ / Lambda Examples : Convert list of one type to another


///////////////////////////////////////////////////////////////////////////////
//
// Copyright © Schakra Inc. 2009. All rights reserved.
//

///////////////////////////////////////////////////////////////////////////////

using System.Collections.Generic;
using System.Linq;

namespace Schakra.Samples.DotNet.Linq
{
public class LinqSample_01
{
List OutlookContacts { get; set; }

public void ConvertListFromOneTypeToAnother()
{
List winLiveContacts =
OutlookContacts.ConvertAll(
contact => contact.ToWindowsLiveContact());

List outlookContacts =
winLiveContacts.ConvertAll(
contact => new OutlookContact()
{
Name = contact.DisplayName,
Phone = contact.MobilePhone
});

List outlookContacts2 =
(from contact in winLiveContacts
select new OutlookContact()
{
Name = contact.DisplayName,
Phone = contact.MobilePhone
}).ToList();
}
}

public class OutlookContact
{
public string Name { get; set; }

public string Phone { get; set; }

public WindowsLiveContact ToWindowsLiveContact()
{
return
new WindowsLiveContact()
{
DisplayName = this.Name,
MobilePhone = this.Phone
};
}
}

public class WindowsLiveContact
{
public string DisplayName { get; set; }

public string MobilePhone { get; set; }
}
}

Sunday, July 12, 2009

Overriding ServerCertificateValidation

I found the following code very useful, when trying to debug https calls through fiddler. During this proxying, fiddler creates its own certificates, which will be failed by the programs. In order to override the behavior, while doing debugging, the following code could be used.


//using System.Net;
//using System.Security.Cryptography.X509Certificates;
//using System.Net.Security;

ServicePointManager.ServerCertificateValidationCallback +=
delegate(object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};

Wednesday, March 25, 2009

Debugging using Fiddler

I have been using Fiddler for a long time. I use fiddler to trace http/https calls to see what is the actual http messages (request - response).

From C#, I typically use the following configuration.


<system.net>
<defaultproxy>
<proxy bypassonlocal="False" usesystemdefault="True" />
</defaultproxy>
</system.net>

Sunday, March 22, 2009

APM pattern with Anonymous methods

Jeffrey Ritcher has explained the APM pattern with anonymous methods in one of the concurrent affairs article in msdn magazine. Here, one of the key things to notice is that anonymous delegate method could be executed on another thread at a later point of time (after executing the statements below the BeginXYZ method).

Consider the following APM code.

static void Main(string[] args)
{
// Create instnace of LongTask that is executed asynchronously
// Ref: http://msdn.microsoft.com/en-us/magazine/cc163467.aspx
LongTask task = new LongTask(15); // Task should run for 15 seconds
Console.WriteLine(
string.Format("Thread-{0} : {1}",
Thread.CurrentThread.ManagedThreadId, "Starting Task"));

// APM pattern with anonymous method
task.BeginDoTask(
delegate(IAsyncResult ar)
{
Console.WriteLine(
string.Format("Thread-{0} : {1}",
Thread.CurrentThread.ManagedThreadId, "Delegate called"));
},
null);

Console.WriteLine(
string.Format("Thread-{0} : {1}",
Thread.CurrentThread.ManagedThreadId, "Control came back after BeginDoTask"));

Thread.Sleep(25 * 1000); // Sleep for 25 seconds
}
When I run the above code, output is coming as given below.

Thread-1 : Starting Task
Thread-1 : Control came back after BeginDoTask
Thread-3 : Delegate called
This means that the anonymous method could be called from another thread at a later point of time. So the below given code is very very wrong.

// Following code is very very wrong.
// Never use this approach
int returnData = 0;
asyncObj.BeginXYZ(
delegate(IAsyncResult ar)
{
returnData = asyncObj.EndXYZ(ar);
},
null);

// It is most likely that returnData is 0
return returnData;