Windows Communication Foundation - Simple Web Service Call

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.

1
2
3
4
5
6
7
8
9
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.

Service Configuration Editor

Below is the snippets from the generated file.

The config file specifies the services ABCs ->

  • Adress

  • Binding

  • Contract

    1
    2
    3
    
    	<client>
    		<endpoint bindingconfiguration="GoogleSearchBinding" binding="basicHttpBinding" name="GoogleSearchPort" contract="GoogleSearchPort" address="http://api.google.com/search/beta2"></endpoint>
    	</client>
    

ServiceContract setting in the config file specifies the interface implemented the ServiceContractAttribute class. Here is the GoogleSearchPort interface is the serviceContract.

1
2
3
4
5
6
7
8
9
[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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 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.

1
2
3
4
5
6
7
8
9
10
<b>Windows Communication Foundation</b>
http://msdn.microsoft.com/webservices/indigo/default.aspx
Explore "Indigo," a set of .NET technologies for building and managing
 service-oriented<br>  systems.
Get Connected
http://msdn.microsoft.com/windowsvista/prodinfo/what/connected/default.aspx
The <b>Windows Communication Foundation</b> Web service APIs make it easy to bui
ld and<br>  consume secure, reliable, and transacted Web services.
Welcome to <b>WCF</b>
http://www.wcf.co.uk/