29 June, 2006, 01:09
In a custom control, I wrote a general function to handle the object equality. I have used it several times without error. However today I sent a value type instead of a reference type, and some unexpected results occured.
The base class object have some basic methods which other classes override those methods. One of the methods is Equals. Although equals usually compares references of objects,many .Net framework internal classes implement that method like in System.String class it can compare the contents. When we go back to object and call equals method the object polymorphism occurs . This is not true for value types. When we box the value type as a reference type, it creates a new memory space for that. So for each boxing a separate memory block allocated, and the reference comparison normally returns false. Here is the sample how it behaves.
using System;
public class MyClass
{
public static void Main()
{
string s1 = "can";
string s2 = "can";
Console.WriteLine("s1== s2 {0}",s1==s2 );
object oo1 = new object();
object oo2 = new object();
Console.WriteLine("oo1== oo2 {0}",oo1==oo2 );
decimal d1 = 5;
decimal d2 = 5;
Console.WriteLine("d1== d2 {0}",d1==d2 );
object o1 = d1;
object o2 = d2;
Console.WriteLine("o1== o2 {0}", o1==o2);
Console.ReadLine();
}
}
Below is the output
s1== s2 True
oo1== oo2 False
d1== d2 True
o1== o2 False
_
24 June, 2006, 01:54
Invoking web services is not much different in windows communication foundation. We have a tool svcutil that generates the interface and the proxy class automatically, I think this will be included later on the IDE. For this sample call I just used google search service. We use the command svcutil with the wsdl adress of the web service, and the program generates a config file and a class file for the service to use.
C:\\Program Files\\Microsoft Visual Studio 8\\VC>svcutil d:\\api\\googleapi\\googlesea
rch.wsdl
Microsoft (R) Service Model Metadata Tool
[Microsoft? .NET Framework, Version 3.0.3906.22]
Copyright (c) Microsoft Corporation. All rights reserved.
Generating files...
C:\\Program Files\\Microsoft Visual Studio 8\\VC\\GoogleSearch.cs
C:\\Program Files\\Microsoft Visual Studio 8\\VC\\output.config
There is also a graphical utility for doing that operation called “Service Configuration Editor”, however as far as I can see this tool only takes the output of the config file. It provides easy to use property controls for modifying the file but the command line versions generated file has some more details on it.
Below is the snippets from the generated file.
The config file specifies the services ABCs ->
<client>
<endpoint address="http://api.google.com/search/beta2" binding="basicHttpBinding"
bindingConfiguration="GoogleSearchBinding" contract="GoogleSearchPort"
name="GoogleSearchPort" />
</client>
ServiceContract setting in the config file specifies the interface implemented the ServiceContractAttribute class. Here is the GoogleSearchPort interface is the serviceContract.
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace = "urn:GoogleSearch")]
public interface GoogleSearchPort
{
[System.ServiceModel.OperationContractAttribute(Action = "urn:GoogleSearchAction", ReplyAction = "*")]
[System.ServiceModel.XmlSerializerFormatAttribute(Style = System.ServiceModel.OperationFormatStyle.Rpc, Use = System.ServiceModel.OperationFormatUse.Encoded)]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(ResultElement))]
[return: System.ServiceModel.MessageParameterAttribute(Name = "return")]
GoogleSearchResult doGoogleSearch(string key, string q, int start, int maxResults, bool filter, string restrict, bool safeSearch, string lr, string ie, string oe);
I just created a new console application project and added these files to the project. Also added the reference “System.ServiceModel” for wcf to work. The rest is the same as previously calling web services, the generated proxy class includes all the information to get the data from the config file. So only thing I have to do is to instanciate and use it.
class Program
{
static void Main(string[] args)
{
using (GoogleSearchPortProxy proxy = new GoogleSearchPortProxy())
{
string key = "secretCode";
string query = "wcf";
GoogleSearchResult res = proxy.doGoogleSearch(key, query, 0, 10, false, "", false, "", "", "");
foreach (ResultElement r in res.resultElements)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(r.title);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(r.URL);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(r.snippet);
}
Console.ReadLine();
}
}
}
Here is the output of the web service results.
Windows Communication Foundation
http://msdn.microsoft.com/webservices/indigo/default.aspx
Explore "Indigo," a set of .NET technologies for building and managing
service-oriented systems.
Get Connected
http://msdn.microsoft.com/windowsvista/prodinfo/what/connected/default.aspx
The Windows Communication Foundation Web service APIs make it easy to bui
ld and consume secure, reliable, and transacted Web services.
Welcome to WCF
http://www.wcf.co.uk/
24 March, 2006, 14:40
Microsoft source implementation of CLI is on the microsoft site. That code looks really weird, it is really low level 
Nice to read indeed the implementation of the language itself.
You can download it from here:
Download SSCLI 2.0