<feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US">
    <title>Michael Ballhaus (MCPD + SCJP)</title>
    <link rel="self" type="application/xml" href="http://geekswithblogs.net/ballhaus/Atom.aspx" />
    <subtitle type="html"> Java + .Net - Coding from the fence</subtitle>
    <id>http://geekswithblogs.net/ballhaus/Default.aspx</id>
    <author>
        <name>Michael Ballhaus</name>
        <uri>http://geekswithblogs.net/ballhaus/Default.aspx</uri>
    </author>
    <generator uri="http://subtextproject.com" version="Subtext Version 0.0.0.0">Subtext</generator>
    <updated>2010-02-10T19:01:11Z</updated>
    <entry>
        <title>LinqToBitTorrent - a custom .NET Linq provider for BitTorrents</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ballhaus/archive/2010/02/10/linqtobittorrent.aspx" />
        <id>http://geekswithblogs.net/ballhaus/archive/2010/02/10/linqtobittorrent.aspx</id>
        <published>2010-02-10T18:57:11-05:00:00</published>
        <updated>2010-02-10T19:01:11Z</updated>
        <summary type="html">LINQ to BitTorrent is a custom .NET LINQ query provider implementation that translates LINQ queries into HTTP requests that accesses various BitTorrent search engine APIs and RSS feeds.</summary>
        <content type="html">&lt;p&gt;LINQ to BitTorrent is a custom .NET LINQ query provider implementation that translates LINQ queries into HTTP requests that accesses various BitTorrent search engine APIs and RSS feeds.&lt;/p&gt;
&lt;p&gt;Full documentation will be posted here soon...&lt;/p&gt;
&lt;p&gt;You can freely download the source code and/or binaries from the &lt;a href="http://linqtobittorrent.codeplex.com/"&gt;Codeplex site&lt;/a&gt; as well as view a &lt;a href="http://aspspider.info/mballhaus/linqtobittorrent.aspx"&gt;live demo&lt;/a&gt; of the provider.&lt;/p&gt;&lt;img src="http://geekswithblogs.net/ballhaus/aggbug/137916.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ballhaus/comments/137916.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ballhaus/comments/commentRss/137916.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ballhaus/services/trackbacks/137916.aspx</trackback:ping>
    </entry>
    <entry>
        <title>LinqToWikipedia - A custom .NET Linq provider - Part II</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ballhaus/archive/2010/01/29/linqtowikipedia-part2.aspx" />
        <id>http://geekswithblogs.net/ballhaus/archive/2010/01/29/linqtowikipedia-part2.aspx</id>
        <published>2010-01-29T13:10:16-05:00:00</published>
        <updated>2010-01-30T11:48:51Z</updated>
        <summary type="html">Explore the inner workings of the LinqToWikipedia provider to give you an understanding of what it takes to create your own IQueryable provider.
</summary>
        <content type="html">&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;
&lt;p&gt;&lt;i&gt;This article is a two-part series regarding the LinqToWikipedia provider. The &lt;a href="http://geekswithblogs.net/ballhaus/archive/2010/01/16/linqtowikipedia-part1.aspx"&gt;first article&lt;/a&gt; covers the basic concepts of Linq as well as the client usage of this particular provider while the second article covers the inner workings of the LinqToWikipedia provider to give you an understanding of what it takes to create your own &lt;code&gt;IQueryable&lt;/code&gt; provider.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;b&gt;NOTE:&lt;/b&gt; You should download the latest build from &lt;a href="http://linqtowikipedia.codeplex.com"&gt;Codeplex&lt;/a&gt; so you can follow along with the code samples.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Creating your own &lt;code&gt;IQueryable&lt;/code&gt; provider&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
First off we have to ask ourselves the question..."Why would we want to create our own &lt;code&gt;IQueryable&lt;/code&gt; provider?" It certainly isn't the easiest coding task that you'll undertake, the internet isn't exactly overflowing with end to end simple examples of how to accomplish this. What benefits do we really get out of doing this? &lt;br /&gt;
&lt;br /&gt;
Well we already know the advantages of using Linq, we already covered that in my previous article; allowing for a consistent querying syntax, whether using Linq or Lambda expressions, no matter what the data source of the data gives us a pretty good first reason to build our own provider. Here are a couple more potential uses:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;You have several applications in your organization that need to verify addresses. You could write a Linq provider that would translate queries to consume the United States Post Service API's to verify physical address locations.&lt;/li&gt;
    &lt;li&gt;Your organization has employee demographic data in various systems such as PeopleSoft, Active Directory, etc. You could write a Linq provider that allows applications to query and return employee data from these various systems in one result set.&lt;/li&gt;
    &lt;li&gt;You maintain websites for Real Estate agents and want to give potential customers the ability to look at the value of nearby homes in a certain area without leaving the agent's website. You could write a Linq provider that would connect to the Zillo.com API and return a wealth of information for the user to gauage the value of a potential home.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So we discussed some reasons why you may want to create your own &lt;code&gt;IQueryable&lt;/code&gt; provider, let's walkthrough the LinqToWikipedia provider to show you how you can create you own provider. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; &lt;i&gt;I am only going to show code highlights of important concepts, but you can always download the full source code from CodePlex to view the code in it's entirety. Additionally, to keep the examples simple I will only focus on the &lt;code&gt;OpenSearch&lt;/code&gt; functionality of the provider&lt;/i&gt; &lt;br /&gt;
&lt;br /&gt;
At a high level, there are two interfaces that you will need to implement in order to create your own &lt;code&gt;IQueryable&lt;/code&gt; provider:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;code&gt;IQueryable&amp;lt;T&amp;gt;&lt;/code&gt;
    &lt;ul&gt;
        &lt;li&gt;We implement this interface so that our results will be able to be enumerated over&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;code&gt;IQueryProvider&lt;/code&gt;
    &lt;ul&gt;
        &lt;li&gt;We implement this interface so that we can examine the query (&lt;i&gt;expression tree&lt;/i&gt;) to perform any translations to create our actual query for whatever API we are calling into and then execute the query&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Before we get into the actual provider code, we will fist start at the client call so we can follow a path through the execution process: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;WikipediaContext datacontext = &lt;span class="kwrd"&gt;new&lt;/span&gt; WikipediaContext();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;var opensearch = (&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    from wikipedia &lt;span class="kwrd"&gt;in&lt;/span&gt; datacontext.OpenSearch&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;where&lt;/span&gt; wikipedia.Keyword == "Microsoft"&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    select wikipedia).Take(5);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Here you can see that we can created a new instance of the &lt;code&gt;WikipediaContext&lt;/code&gt; class and then called to the &lt;code&gt;datacontext.OpenSearch&lt;/code&gt; property. This allows the query to return as type &lt;code&gt;IWikipediaQueryable&amp;lt;WikipediaOpenSearchResult&amp;gt;&lt;/code&gt; (&lt;i&gt;this will be explained in the later sections&lt;/i&gt;). Then we are going to query the results to only inlude those where &lt;code&gt;Keyword == "Microsoft"&lt;/code&gt;. Finally, we are going to only want to return the first 5 records via the &lt;code&gt;Take()&lt;/code&gt; extension method. &lt;br /&gt;
&lt;br /&gt;
We have broken the query down so that you can see where everything more or less fits together as we review the code later. &lt;br /&gt;
&lt;br /&gt;
Let's now look at the &lt;code&gt;WikipediaContext&lt;/code&gt; class: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WikipediaContext&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; IWikipediaQueryable&amp;lt;WikipediaOpenSearchResult&amp;gt; OpenSearch&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; WikipediaQueryable&amp;lt;WikipediaOpenSearchResult&amp;gt;(); }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
Seems pretty simple. A class with one property &lt;code&gt;OpenSearch&lt;/code&gt; that returns type &lt;code&gt;IWikipediaQueryable&amp;lt;WikipediaOpenSearchResult&amp;gt;&lt;/code&gt;. In actuality, we are returning the concrete type &lt;code&gt;WikipediaQueryable&amp;lt;WikipediaOpenSearchResult&amp;gt;&lt;/code&gt; that implements the &lt;code&gt;IWikipediaQueryable&amp;lt;T&amp;gt;&lt;/code&gt; interface. This is important because that interface also implements &lt;code&gt;IQueryable&amp;lt;T&amp;gt;&lt;/code&gt; which is what allows our provider to return itself as an &lt;code&gt;IQueryable&lt;/code&gt;. &lt;br /&gt;
&lt;br /&gt;
Up to this point we will be returning an enumerable list of &lt;code&gt;WikipediaOpenSearchResult&lt;/code&gt;, which is the result class that we will be populating from the Wikipedia API. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WikipediaOpenSearchResult : IWikipediaOpenSearchResult&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Text { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Description { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Uri Url { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Uri ImageUrl { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Keyword { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
Since we are implementing the &lt;code&gt;IQueryable&amp;lt;T&amp;gt;&lt;/code&gt; interface, we are also implementing from &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;, IQueryable, and IEnumerable&lt;/code&gt; so we are obligated to provide implementations of the following:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;code&gt;IEnumerator IEnumerable.GetEnumerator()&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;IEnumerator&amp;lt;T&amp;gt; IEnumerable&amp;lt;T&amp;gt;.GetEnumerator()&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;Type IQueryable.ElementType&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;Expression IQueryable.Expression&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;IQueryProvider IQueryable.Provider&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;br /&gt;
For the purposes of this tutorial we will only focus on the &lt;code&gt;IEnumerator&amp;lt;T&amp;gt; IEnumerable&amp;lt;T&amp;gt;.GetEnumerator()&lt;/code&gt; method, this is where the acutal call to our custom query provider takes place. &lt;br /&gt;
&lt;br /&gt;
Here is the &lt;code&gt;WikipediaQueryable&lt;/code&gt; class: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WikipediaQueryable&amp;lt;T&amp;gt; : IWikipediaQueryable&amp;lt;T&amp;gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; Expression _expression;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; WikipediaQueryable()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        _expression = Expression.Constant(&lt;span class="kwrd"&gt;this&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; WikipediaQueryable(Expression expression)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        _expression = expression;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    IEnumerator&amp;lt;T&amp;gt; IEnumerable&amp;lt;T&amp;gt;.GetEnumerator()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        WikipediaQueryProvider provider = &lt;span class="kwrd"&gt;new&lt;/span&gt; WikipediaQueryProvider();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;        IEnumerable&amp;lt;T&amp;gt; sequence = provider.Execute&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;(_expression);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; sequence.GetEnumerator();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
Notice on lines 17 - 21 we are calling our customer query provider &lt;code&gt;WikipediaQueryProvider&lt;/code&gt;. This is where the provider is passed the lambda expression via it's &lt;code&gt;Execute&lt;/code&gt; method. Once the provider has translated and executed the query to the API, it will subsequently return an &lt;code&gt;IEnuberable&lt;/code&gt; of type &lt;code&gt;&amp;lt;T&amp;gt;&lt;/code&gt;, which is actually the &lt;code&gt;WikipediaOpenSearchResult&lt;/code&gt; type in this case. &lt;br /&gt;
&lt;br /&gt;
Since our custom query provider will implement &lt;code&gt;IQueryProvider&lt;/code&gt; we must provide implementations for the following:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;code&gt;IQueryable CreateQuery(Expression expression)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;IQueryable&amp;lt;TElement&amp;gt; CreateQuery&amp;lt;TElement&amp;gt;(Expression expression)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;object Execute(Expression expression)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;TResult Execute&amp;lt;TResult&amp;gt;(Expression expression)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For the purposes of this tutorial we will only focus on the &lt;code&gt;object Execute(Expression expression)&lt;/code&gt; method, this is where the HTTP send and get request calls are made to the Wikipedia API. &lt;br /&gt;
&lt;br /&gt;
Here is the &lt;code&gt;WikipediaQueryProvider&lt;/code&gt; class: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WikipediaQueryProvider : IQueryProvider&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; Execute(Expression expression)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; WikipediaOpenSearchResponse.Get(WikipediaOpenSearchRequest.Send(expression));&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
Line 5 is returning a string result from the &lt;code&gt;WikipediaOpenSearchRequest.Send&lt;/code&gt; method which is really just sending our formatted URL to the Wikipedia API and returning the results to the &lt;code&gt;WikipediaOpenSearchResponse.Get&lt;/code&gt; method which parses the results and returns them as type &lt;code&gt;IEnumerable&amp;lt;WikipediaOpenSearchResult&amp;gt;&lt;/code&gt;. &lt;br /&gt;
&lt;br /&gt;
We'll cover the &lt;code&gt;WikipediaOpenSearchResponse.Get&lt;/code&gt; method later, for now let's take a look at the &lt;code&gt;WikipediaOpenSearchRequest.Send&lt;/code&gt; method. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WikipediaOpenSearchRequest&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Send(Expression expression)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        WikipediaOpenSearchUriBuilder uriBuilder = &lt;span class="kwrd"&gt;new&lt;/span&gt; WikipediaOpenSearchUriBuilder();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        Uri uri = uriBuilder.BuildUri(expression);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; HttpRequest.Send(uri);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
This static method is building a URI based on the expression and then utlizing a helper method &lt;code&gt;HttpRequest.Send&lt;/code&gt; which sends the URI using &lt;code&gt;HttpWebRequest&lt;/code&gt;. The &lt;code&gt;WikipediaOpenSearchUriBuilder&lt;/code&gt; class contains the code that will parse the expression so that we can translate it into a usable URI. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; &lt;i&gt;Learning to parse lambda expressions is a topic worthy of being covered in a book. You will see some basic samples below on parsing expressions however the focus of this article is to demostrate how to create a Linq provider. I would suggest further exploration on learning the ins and outs of expression parsing to ensure that you have a good solid foundation on lambda expressions. Here are a few good starting points:&lt;/i&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li style="font-style: italic"&gt;&lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/bb397687.aspx"&gt;MSDN&lt;/a&gt;&lt;/li&gt;
    &lt;li style="font-style: italic"&gt;&lt;a target="_blank" href="http://blogs.msdn.com/charlie/archive/2008/01/31/expression-tree-basics.aspx"&gt;Charlie Calvert's Community Blog - Expression Tree Basics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;br /&gt;
Here is the &lt;code&gt;WikipediaOpenSearchUriBuilder&lt;/code&gt; class. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WikipediaOpenSearchUriBuilder : ExpressionVisitor&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; StringBuilder _urlBuilder;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; _queryString;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; query = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; WikipediaOpenSearchUriBuilder()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        _queryString = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        _queryString[&lt;span class="str"&gt;"format"&lt;/span&gt;] = &lt;span class="str"&gt;"xml"&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        _queryString[&lt;span class="str"&gt;"action"&lt;/span&gt;] = &lt;span class="str"&gt;"opensearch"&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        _urlBuilder = &lt;span class="kwrd"&gt;new&lt;/span&gt; StringBuilder();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        _urlBuilder.Append(&lt;span class="str"&gt;"http://en.wikipedia.org/w/api.php?"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Uri BuildUri(Expression expression)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (KeyValuePair&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; parameter &lt;span class="kwrd"&gt;in&lt;/span&gt; _queryString)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;            _urlBuilder.Append(parameter.Key);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;            _urlBuilder.Append(&lt;span class="str"&gt;"="&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;            _urlBuilder.Append(Uri.EscapeDataString(parameter.Value));&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;            _urlBuilder.Append(&lt;span class="str"&gt;"&amp;amp;"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;        &lt;span class="rem"&gt;// parse expression tree&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;        Visit((MethodCallExpression)expression);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(_urlBuilder.ToString());&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;    &lt;span class="rem"&gt;// override ExpressionVisitor method&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Expression VisitMethodCall(MethodCallExpression m)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; ((m.Method.DeclaringType == &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Queryable)) || (m.Method.DeclaringType == &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Enumerable)))&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (m.Method.Name.Equals(&lt;span class="str"&gt;"Where"&lt;/span&gt;))&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;            {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;                LambdaExpression lambda = (LambdaExpression)StripQuotes(m.Arguments[1]);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;                ParseQuery(lambda.Body);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  44:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  45:  &lt;/span&gt;                _urlBuilder.Append(&lt;span class="str"&gt;"search="&lt;/span&gt; + query);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  46:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  47:  &lt;/span&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; m;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  48:  &lt;/span&gt;            }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  49:  &lt;/span&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (m.Method.Name.Equals(&lt;span class="str"&gt;"Take"&lt;/span&gt;))&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  50:  &lt;/span&gt;            {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  51:  &lt;/span&gt;                Visit(m.Arguments[0]);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  52:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  53:  &lt;/span&gt;                ConstantExpression countExpression = (ConstantExpression)StripQuotes(m.Arguments[1]);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  54:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  55:  &lt;/span&gt;                _urlBuilder.Append(&lt;span class="str"&gt;"&amp;amp;limit="&lt;/span&gt; + ((&lt;span class="kwrd"&gt;int&lt;/span&gt;)countExpression.Value).ToString(CultureInfo.InvariantCulture));&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  56:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  57:  &lt;/span&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; m;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  58:  &lt;/span&gt;            }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  59:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  60:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  61:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; m;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  62:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  63:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  64:  &lt;/span&gt;    &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ParseQuery(Expression e)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  65:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  66:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (e &lt;span class="kwrd"&gt;is&lt;/span&gt; BinaryExpression)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  67:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  68:  &lt;/span&gt;            BinaryExpression c = e &lt;span class="kwrd"&gt;as&lt;/span&gt; BinaryExpression;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  69:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  70:  &lt;/span&gt;            &lt;span class="kwrd"&gt;switch&lt;/span&gt; (c.NodeType)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  71:  &lt;/span&gt;            {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  72:  &lt;/span&gt;                &lt;span class="kwrd"&gt;case&lt;/span&gt; ExpressionType.Equal:&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  73:  &lt;/span&gt;                    GetCondition(c);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  74:  &lt;/span&gt;                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  75:  &lt;/span&gt;                &lt;span class="kwrd"&gt;default&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  76:  &lt;/span&gt;                    &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotSupportedException(&lt;span class="str"&gt;"Only .Equal is supported for this query"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  77:  &lt;/span&gt;            }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  78:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  79:  &lt;/span&gt;        &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  80:  &lt;/span&gt;            &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotSupportedException(&lt;span class="str"&gt;"This querytype is not supported."&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  81:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  82:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  83:  &lt;/span&gt;    &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; GetCondition(BinaryExpression e)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  84:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  85:  &lt;/span&gt;        &lt;span class="kwrd"&gt;string&lt;/span&gt; val = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty, attrib = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  86:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  87:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (e.Left &lt;span class="kwrd"&gt;is&lt;/span&gt; MemberExpression)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  88:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  89:  &lt;/span&gt;            attrib = ((MemberExpression)e.Left).Member.Name;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  90:  &lt;/span&gt;            val = Expression.Lambda(e.Right).Compile().DynamicInvoke().ToString();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  91:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  92:  &lt;/span&gt;        &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (e.Right &lt;span class="kwrd"&gt;is&lt;/span&gt; MemberExpression)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  93:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  94:  &lt;/span&gt;            attrib = ((MemberExpression)e.Right).Member.Name;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  95:  &lt;/span&gt;            val = Expression.Lambda(e.Left).Compile().DynamicInvoke().ToString();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  96:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  97:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  98:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (attrib.Equals(&lt;span class="str"&gt;"Keyword"&lt;/span&gt;))&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  99:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 100:  &lt;/span&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (query.Length &amp;gt; 0)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 101:  &lt;/span&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotSupportedException(&lt;span class="str"&gt;"'WikipediaOpenSearchResult' Query expression can only contain one 'Keyword'"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 102:  &lt;/span&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 103:  &lt;/span&gt;                query = val;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 104:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 105:  &lt;/span&gt;        &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 106:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 107:  &lt;/span&gt;            &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotSupportedException(&lt;span class="str"&gt;"'WikipediaOpenSearchResult' Query expression can only contain a'Keyword' parameter"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 108:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 109:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 110:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
This class is doing most of the heavy lifting for our provider so let's break it down:&lt;br /&gt;
&lt;br /&gt;
First, on line 1 one we are inheriting from the abstract class &lt;code&gt;ExpressionVisitor&lt;/code&gt; which is a helper class that allows us to easily parse lambda expression trees. We will cover this a bit more later. &lt;br /&gt;
&lt;br /&gt;
Next, on line 7 in our contructor... &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;pre class="csharpcode"&gt;
_queryString = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();
_queryString[&lt;span class="str"&gt;"format"&lt;/span&gt;] = &lt;span class="str"&gt;"xml"&lt;/span&gt;;
_queryString[&lt;span class="str"&gt;"action"&lt;/span&gt;] = &lt;span class="str"&gt;"opensearch"&lt;/span&gt;;&lt;/pre&gt;
&lt;p&gt;br /&amp;gt; We begin the process of building our querystring variable that will be sent to the Wikipedia API. Throughout the class we will be appending new items to the querystring as we begin parsing the expression tree. &lt;br /&gt;
&lt;br /&gt;
Next, on line 18 we define our method which then takes over in building our URL from the lamda expression...&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Uri BuildUri(Expression expression)&lt;/pre&gt;
&lt;p&gt;and the actual parsing of the expression tree starts on line 29: &lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;Visit((MethodCallExpression)expression);&lt;/code&gt; &lt;br /&gt;
&lt;br /&gt;
This method in the &lt;code&gt;ExpressionVisitor&lt;/code&gt; class begins evaluating the expression and subsequently takes us back to our overridden method on line 35: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Expression VisitMethodCall(MethodCallExpression m)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
This method is where we can check the values contained in the expression, specifically in the &lt;code&gt;Where()&lt;/code&gt; and &lt;code&gt;Take()&lt;/code&gt; extension methods so that we can parse the expression and extract the values that we are looking for. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (m.Method.Name.Equals(&lt;span class="str"&gt;"Where"&lt;/span&gt;))&lt;/pre&gt;
&lt;p&gt;and..&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (m.Method.Name.Equals(&lt;span class="str"&gt;"Take"&lt;/span&gt;))&lt;/pre&gt;
&lt;p&gt;Lines 39-48 are where we parse the lambda expression and look for the keyword that was supplied to the &lt;code&gt;Where()&lt;/code&gt; extension method. This value is then appended to the querystring as the "search" value passed to the Wikipedia API. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;
_urlBuilder.Append(&lt;span class="str"&gt;"search="&lt;/span&gt; + query);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
Next on lines 49-58 we look for the value that was supplied to the &lt;code&gt;Take()&lt;/code&gt; extension method. This value is then appended to the querystring as the "limit" value passed to the Wikipedia API. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;
_urlBuilder.Append(&lt;span class="str"&gt;"&amp;amp;limit="&lt;/span&gt; + ((&lt;span class="kwrd"&gt;int&lt;/span&gt;)countExpression.Value).ToString(CultureInfo.InvariantCulture));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Recap:&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
So up to this point, we have translated the lambda expression into a URL that is ready to be passed to the Wikipedia API. We now need to get ready to accept the returned data (xml) from the Wikipedia API and then populate our &lt;code&gt;WikipediaOpenSearchResult&lt;/code&gt; class with the data. So let's move on to the &lt;code&gt;WikipediaOpenSearchResponse.Get&lt;/code&gt; method. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WikipediaOpenSearchResponse&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IEnumerable&amp;lt;WikipediaOpenSearchResult&amp;gt; Get(&lt;span class="kwrd"&gt;string&lt;/span&gt; xml)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        List&amp;lt;WikipediaOpenSearchResult&amp;gt; resultList = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;WikipediaOpenSearchResult&amp;gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        var descendants = from i &lt;span class="kwrd"&gt;in&lt;/span&gt; XDocument.Parse(xml).Descendants() select i;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (XElement element &lt;span class="kwrd"&gt;in&lt;/span&gt; descendants)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (element.Name.LocalName.ToString().Equals(&lt;span class="str"&gt;"Item"&lt;/span&gt;))&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;                WikipediaOpenSearchResult wsr = &lt;span class="kwrd"&gt;new&lt;/span&gt; WikipediaOpenSearchResult();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;                var items = from x &lt;span class="kwrd"&gt;in&lt;/span&gt; element.Nodes()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;                            select x;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;                &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (XElement item &lt;span class="kwrd"&gt;in&lt;/span&gt; items)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;                {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;                    &lt;span class="kwrd"&gt;switch&lt;/span&gt; (item.Name.LocalName.ToString())&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;                    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;                        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;"Text"&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;                            wsr.Text = item.Value;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;                            &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;                        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;"Description"&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;                            wsr.Description = item.Value;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;                            &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;                        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;"Url"&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;                            wsr.Url = &lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(item.Value);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;                            &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;                        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;"Image"&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;                            wsr.ImageUrl = &lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(item.Attribute(&lt;span class="str"&gt;"source"&lt;/span&gt;).Value);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;                            &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;                    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;                }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;                resultList.Add(wsr);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;            }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; resultList;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
This class has one static method that accepts a string of xml and returns a a generic &lt;code&gt;List&amp;lt;&amp;gt;&lt;/code&gt; of type &lt;code&gt;WikipediaOpenSearchResult&lt;/code&gt; as an &lt;code&gt;IEnumerable&lt;/code&gt;. We are using LinqtoXml to parse out the data and return the elements that we need to populate our &lt;code&gt;WikipediaOpenSearchResult&lt;/code&gt; class. &lt;br /&gt;
&lt;br /&gt;
From here the &lt;code&gt;IEnumerable&lt;/code&gt; is passed back on up the stack to the &lt;code&gt;WikipediaQueryProvider&lt;/code&gt; and returned to the client as type &lt;code&gt;WikipediaQueryable&amp;lt;WikipediaKeywordSearchResult&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;img src="http://geekswithblogs.net/ballhaus/aggbug/137707.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ballhaus/comments/137707.aspx</wfw:comment>
        <slash:comments>5</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ballhaus/comments/commentRss/137707.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ballhaus/services/trackbacks/137707.aspx</trackback:ping>
    </entry>
    <entry>
        <title>LinqToWikipedia - A custom .NET Linq provider - Part I</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ballhaus/archive/2010/01/16/linqtowikipedia-part1.aspx" />
        <id>http://geekswithblogs.net/ballhaus/archive/2010/01/16/linqtowikipedia-part1.aspx</id>
        <published>2010-01-16T11:21:56-05:00:00</published>
        <updated>2010-01-29T13:36:38Z</updated>
        <summary type="html">This article covers the basic concepts of Linq as well as the client usage of the LinqToWikipedia provider.</summary>
        <content type="html">
&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;
&lt;p&gt;&lt;i&gt;This article is a two-part series regarding the LinqToWikipedia provider. The first article will cover the basic concepts of Linq as well as the client usage of this particular provider while the &lt;a href="http://geekswithblogs.net/ballhaus/archive/2010/01/29/linqtowikipedia-part2.aspx"&gt;second article&lt;/a&gt; will explore the inner workings of the LinqToWikipedia provider to give you an understanding of what it takes to create your own &lt;code&gt;IQueryable&lt;/code&gt; provider.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;b&gt;NOTE:&lt;/b&gt; You should download the latest build from &lt;a href="http://linqtowikipedia.codeplex.com"&gt;Codeplex&lt;/a&gt; so you can follow along with the code samples.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is Linq?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Let's spend a moment and talk about what Linq is all about from a high-level. &lt;br /&gt;
&lt;br /&gt;
Linq (&lt;i&gt;Language Integrated Query&lt;/i&gt;) is a .NET programming model that allows for a consistent SQL-like querying syntax (&lt;i&gt;called query expressions&lt;/i&gt;) against various data sources. These data sources can be anything from SQL server, Oracle, XML, in-memory objects, web services... just about anything. There are many Linq providers that are provided with .NET such as Linq to SQL, Linq to XML, Linq to Objects, while there are many 3rd party Linq providers out there that are available such as Linq to Oracle, Linq to Amazon, Linq to MySql, etc. &lt;br /&gt;
&lt;br /&gt;
The following code is an example of a Linq query that searches for customers that start with the letter "M" and return the records to an ASP.NET DataGrid.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Page_Load(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, EventArgs e)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    var customers =&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;        from c &lt;span class="kwrd"&gt;in&lt;/span&gt; Customers.GetCustomers()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        &lt;span class="kwrd"&gt;where&lt;/span&gt; c.Name.StartsWith(&lt;span class="str"&gt;"M"&lt;/span&gt;) &amp;amp;&amp;amp; c.Age &amp;gt; 11&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        orderby c.Age descending&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        select c;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    dg_customers.DataSource = customers;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    dg_customers.DataBind();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Customers&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Age { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; List&amp;lt;Customers&amp;gt; GetCustomers()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        List&amp;lt;Customers&amp;gt; customerds = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Customers&amp;gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;        customerds.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Customers() { Name = &lt;span class="str"&gt;"Elo"&lt;/span&gt;, Age = 6 });&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;        customerds.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Customers() { Name = &lt;span class="str"&gt;"Myranda"&lt;/span&gt;, Age = 11 });&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;        customerds.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Customers() { Name = &lt;span class="str"&gt;"Mikayli"&lt;/span&gt;, Age = 12 });&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;        customerds.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Customers() { Name = &lt;span class="str"&gt;"Elias"&lt;/span&gt;, Age = 13 });&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;        customerds.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Customers() { Name = &lt;span class="str"&gt;"Memori"&lt;/span&gt;, Age = 15 });&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; customerds;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div&gt;
&lt;table id="dg_customers" cellspacing="0" cellpadding="3" border="1" style="font-size: 10pt; font-family: Arial"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;Name&lt;/td&gt;
            &lt;td&gt;Age&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Memori&lt;/td&gt;
            &lt;td&gt;15&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Mikayli&lt;/td&gt;
            &lt;td&gt;12&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
The compiler will actually translate the Linq query into a &lt;b&gt;lambda expression&lt;/b&gt; so if you wanted to you could write this query as a lambda expression directly: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;
IEnumerable&amp;lt;Customers&amp;gt; customers = Customers.GetCustomers().Where(c =&amp;gt; c.Name.StartsWith(&lt;span class="str"&gt;"M"&lt;/span&gt;) &amp;amp;&amp;amp; c.Age &amp;gt; 11).OrderByDescending(y =&amp;gt; y.Age);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
But as you can see, the Linq code is much more intuitive and especially if you have multiple statements in your &lt;code&gt;Where&lt;/code&gt; clause. Either way, this code will work for any Linq provider that exposes a &lt;code&gt;Customers&lt;/code&gt; entity whether they are stored in a database, XML files, web service lookup, etc. The &lt;code&gt;Customers&lt;/code&gt; entity happens to be in-memory objects in this case. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;LinqToWikipedia search options&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
Before we dive into actually building the Linq query, let's review the two search options provided by LinqToWikipedia. There are two different types of searches that the provider offers via the MediaWiki API:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Open Search&lt;/li&gt;
    &lt;li&gt;Keyword Search&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Open Search&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
OpenSearch is a standardized collection of simple formats for the sharing of search results. OpenSearch was created by A9.com, an Amazon.com company, and the OpenSearch format is now in use by hundreds of search engines and search applications around the Internet. LinqToWikipedia can format the returned data so that it adheres to this standard. For more information on the OpenSearch standard visit &lt;a target="_blank" href="http://www.opensearch.org/"&gt;OpenSearch.org&lt;/a&gt;. &lt;br /&gt;
&lt;br /&gt;
Open Search will return the following data elements from a Wikipedia search when querying by a single search term:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Text - &lt;i&gt;Title of the page&lt;/i&gt;&lt;/li&gt;
    &lt;li&gt;Description - &lt;i&gt;Short description of the page&lt;/i&gt;&lt;/li&gt;
    &lt;li&gt;Url - &lt;i&gt;The absolute Url to the page&lt;/i&gt;&lt;/li&gt;
    &lt;li&gt;ImageUrl - &lt;i&gt;The absolute Url to the main image of the page (if it exists)&lt;/i&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;See a &lt;a target="_blank" href="http://aspspider.info/mballhaus/WikipediaOpenSearch.aspx"&gt;live demo&lt;/a&gt; of the Open Search function. &lt;br /&gt;
&lt;br /&gt;
This search option will return up to 15 records at a time using the &lt;code&gt;.Take()&lt;/code&gt; query extension. &lt;i&gt;You will see an example of this code later on.&lt;/i&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Keyword Search&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
This query option allows you to search the Wikipedia database using &lt;b&gt;multiple&lt;/b&gt; keywords and returns data in the following format: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Title - &lt;i&gt;Title of the page&lt;/i&gt;&lt;/li&gt;
    &lt;li&gt;Description - &lt;i&gt;Short description of the page&lt;/i&gt;&lt;/li&gt;
    &lt;li&gt;Url - &lt;i&gt;The absolute Url to the page&lt;/i&gt;&lt;/li&gt;
    &lt;li&gt;WordCount - &lt;i&gt;Total count of words&lt;/i&gt;&lt;/li&gt;
    &lt;li&gt;TimeStamp - &lt;i&gt;The date/time the page was last updated)&lt;/i&gt;&lt;/li&gt;
    &lt;li&gt;RecordCount - &lt;i&gt;The total number of records in the result set (this is used for paging)&lt;/i&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;See a &lt;a target="_blank" href="http://aspspider.info/mballhaus/WikipediaKeywordSearch.aspx"&gt;live demo&lt;/a&gt; of the Keyword search function. &lt;br /&gt;
&lt;br /&gt;
This query option will return up to 100 records at a time and also supports paging through the data by using a combination of &lt;code&gt;.Skip()&lt;/code&gt; and &lt;code&gt;.Take()&lt;/code&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Using the LinqToWikipedia provider&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
Now let's move on to using the LinqToWikipedia provider. You have two options:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Download the source code and add to an existing project and make a project reference or...&lt;/li&gt;
    &lt;li&gt;Download the source code and just copy/reference the &lt;code&gt;linqtowikipedia.dll&lt;/code&gt; within your project&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now that you have a reference, you need to add a &lt;code&gt;using&lt;/code&gt; statement to your code to add the namespace: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; LinqToWikipedia;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Next you need to instantiate the LinqToWikipedia object via the &lt;code&gt;WikipediaConext&lt;/code&gt; class. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;
WikipediaContext datacontext = &lt;span class="kwrd"&gt;new&lt;/span&gt; WikipediaContext();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="rem"&gt;//WikipediaContext datacontext = new WikipediaContext(new WebProxy("yourproxy", 80), new NetworkCredential("username", "password", "domain"));&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;b&gt;Note:&lt;/b&gt; &lt;i&gt;If your application is running behind a firewall/proxy, you can alternatively instansiate the provider passing in a &lt;/i&gt;&lt;code&gt;new WebProxy&lt;/code&gt; &lt;i&gt;and&lt;/i&gt; &lt;code&gt;NetworkCredential&lt;/code&gt; &lt;i&gt;to the constructor.&lt;/i&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Now we will create our Linq code to query Wikipedia using the &lt;code&gt;OpenSearch&lt;/code&gt; method.&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
We will query using &lt;i&gt;soccer&lt;/i&gt; as our keyword and request that the provider return 10 records by calling the &lt;code&gt;.Take(10)&lt;/code&gt; query extension method: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;var opensearch = (&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    from wikipedia &lt;span class="kwrd"&gt;in&lt;/span&gt; datacontext.OpenSearch&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;where&lt;/span&gt; wikipedia.Keyword == "soccer"&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    select wikipedia).Take(10);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
The result set is of type &lt;code&gt;IWikipediaQueryable&amp;lt;WikipediaOpenSearchResult&amp;gt;&lt;/code&gt; and since this type implements the &lt;code&gt;IQueryable&amp;lt;T&amp;gt;&lt;/code&gt; interface, we can iterate over the results with a &lt;code&gt;foreach&lt;/code&gt; loop. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;StringBuilder sb = &lt;span class="kwrd"&gt;new&lt;/span&gt; StringBuilder();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (WikipediaOpenSearchResult result &lt;span class="kwrd"&gt;in&lt;/span&gt; opensearch)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    sb.Append(&lt;span class="str"&gt;"Text = "&lt;/span&gt; + result.Text + &lt;span class="str"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    sb.Append(&lt;span class="str"&gt;"Description = "&lt;/span&gt; + result.Description + &lt;span class="str"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    sb.Append(&lt;span class="str"&gt;"Url = "&lt;/span&gt; + result.Url + &lt;span class="str"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    sb.Append(&lt;span class="str"&gt;"ImageUrl = "&lt;/span&gt; + result.ImageUrl + &lt;span class="str"&gt;"&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&lt;span class="rem"&gt;//ASP.NET Label control&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;lbl_diplay.Text = sb.ToString();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Here is a sample of the results: &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: 8pt; font-family: Arial"&gt;Text = Association football&lt;br /&gt;
Description = Association football, more commonly known as football or soccer, is a team sport played between two teams of eleven players using a spherical ball. &lt;br /&gt;
Url = http://en.wikipedia.org/wiki/Association_football&lt;br /&gt;
ImageUrl = http://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Football_header.JPG/50px-Football_header.JPG&lt;br /&gt;
&lt;br /&gt;
Text = Racing Post&lt;br /&gt;
Description = The Racing Post is a British daily horse racing, greyhound racing and sports betting newspaper, currently the only one appearing in print form. &lt;br /&gt;
Url = http://en.wikipedia.org/wiki/Racing_Post&lt;br /&gt;
ImageUrl = &lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
See a &lt;a target="_blank" href="http://aspspider.info/mballhaus/WikipediaOpenSearch.aspx"&gt;live demo&lt;/a&gt; of the Open Search function. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Now we will create our Linq code to query Wikipedia using the &lt;code&gt;KeywordSearch&lt;/code&gt; method.&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
We will query using &lt;i&gt;soccer&lt;/i&gt;, &lt;i&gt;los angeles&lt;/i&gt;, &lt;i&gt;galaxy&lt;/i&gt;, &lt;i&gt;donovan&lt;/i&gt; as our keywords and request that the provider return 10 records by calling the &lt;code&gt;.Take(10)&lt;/code&gt; query extension method but we also want to skip the first 10 records in the result set by using the &lt;code&gt;.Skip(10)&lt;/code&gt; query extension method. By using &lt;code&gt;.Skip()&lt;/code&gt; and &lt;code&gt;.Take()&lt;/code&gt; in tandem we are effectively enabling recordset paging. To determine the total number of pages in the recordset, we can divide the &lt;code&gt;RecordCount&lt;/code&gt; property by the number of &lt;code&gt;.Take()&lt;/code&gt; records. Then for each new query we increase the &lt;code&gt;.Skip()&lt;/code&gt; number to move to the next recordset until we are at the last page of records. Finally, we will have the results displayed to an ASP.NET DataList. &lt;br /&gt;
&lt;br /&gt;
First we need to declare a local variable that will hold the total number of records in the result set. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; totalrecords = 0;&lt;/pre&gt;
&lt;p&gt;Now we write our Linq query and set the results as the &lt;code&gt;DataSource&lt;/code&gt; of our &lt;code&gt;DataList&lt;/code&gt;. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;WikipediaContext datacontext = &lt;span class="kwrd"&gt;new&lt;/span&gt; WikipediaContext();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;var query = (&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    from wikipedia &lt;span class="kwrd"&gt;in&lt;/span&gt; datacontext.KeywordSearch&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;where&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        wikipedia.Keyword == &lt;span class="str"&gt;"soccer"&lt;/span&gt; &amp;amp;&amp;amp;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        wikipedia.Keyword == &lt;span class="str"&gt;"los angeles"&lt;/span&gt; &amp;amp;&amp;amp;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        wikipedia.Keyword == &lt;span class="str"&gt;"galaxy"&lt;/span&gt; &amp;amp;&amp;amp;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        wikipedia.Keyword == &lt;span class="str"&gt;"donovan"&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    select wikipedia).Skip(10).Take(10);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;dl_results.DataSource = query;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;dl_results.DataBind();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; &lt;i&gt;To use paging, you would want to set the &lt;code&gt;.Skip()&lt;/code&gt; number to a variable that you can increment with each subsequent lookup.&lt;/i&gt; &lt;br /&gt;
&lt;br /&gt;
Since we are not looping through the results (&lt;i&gt;like we did in the OpenSearch example&lt;/i&gt;) we need to find a way to capture the &lt;code&gt;RecordCount&lt;/code&gt; value and set it to our local &lt;code&gt;totalrecords&lt;/code&gt; variable. To solve this we can use the ASP.NET DataList &lt;code&gt;ItemDataBound&lt;/code&gt; event. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; dl_results_ItemDataBound(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, DataListItemEventArgs e)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;this&lt;/span&gt;.totalrecords == 0)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.totalrecords = ((WikipediaKeywordSearchResult)e.Item.DataItem).RecordCount;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
See a &lt;a target="_blank" href="http://aspspider.info/mballhaus/WikipediaKeywordSearch.aspx"&gt;live demo&lt;/a&gt; of the Keyword search function. &lt;br /&gt;
&lt;br /&gt;
Part II - Explore the inner workings of the LinqToWikipedia provider to create your own &lt;code&gt;IQueryable&lt;/code&gt; provider. (&lt;i&gt;coming soon&lt;/i&gt;)&lt;/p&gt;&lt;img src="http://geekswithblogs.net/ballhaus/aggbug/137512.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ballhaus/comments/137512.aspx</wfw:comment>
        <slash:comments>1</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ballhaus/comments/commentRss/137512.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ballhaus/services/trackbacks/137512.aspx</trackback:ping>
    </entry>
    <entry>
        <title>.NET WCF services and Java Web Services Interoperability - Part 3 of 3</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ballhaus/archive/2009/12/30/net-wcf-and-java-ws-interoperability-part3.aspx" />
        <id>http://geekswithblogs.net/ballhaus/archive/2009/12/30/net-wcf-and-java-ws-interoperability-part3.aspx</id>
        <published>2009-12-30T09:32:43-05:00:00</published>
        <updated>2009-12-30T09:33:28Z</updated>
        <summary type="html">Part 3 - This article will demonstrate how you can have interoperability between JWS and WCF using custom data types, generic lists, and enums</summary>
        <content type="html">&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #cc6633; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.inlinecode
        {
            font-family: Consolas, Courier New, Courier, Monospace;
            color: #2b91af;
            font-size: small;
        }
.csharpcode .lnum { color: #606060; }
        .style1
        {
            color: #2B91AF;
        }
    ]]&gt;&lt;/style&gt;&lt;b&gt;Part III - Create the Java Web Service consumed by a .NET client&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
This article is the third in a three part series demonstrating the interoperability between .NET WCF services and Java Web Services. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; &lt;i&gt;If you haven't read &lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/28/net-wcf-and-java-ws-interoperability-part1.aspx"&gt;Part I&lt;/a&gt; or &lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/29/net-wcf-and-java-ws-interoperability-part2.aspx"&gt;Part II&lt;/a&gt;, you should review them before continuing on.&lt;/i&gt; &lt;br /&gt;
&lt;br /&gt;
In Part I we reviewed the class diagram of the &lt;code&gt;Team&lt;/code&gt; class and in Part II we covered creating a WCF service that exposed that class and consumed it with a Java client. Now we will be creating the JWS and consume it with a .NET client. &lt;br /&gt;
&lt;br /&gt;
We will start with creating the &lt;code&gt;Player&lt;/code&gt; class: &lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;font color="#808080"&gt;01&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;jws;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;02&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;03&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;Player &lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;04&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;05&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;String _name;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;06&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/font&gt;&lt;font color="#c00000"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;_age;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;07&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;08&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;String getName&lt;/font&gt;&lt;font color="#000000"&gt;() {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;09&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;_name;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;10&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;11&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;12&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#c00000"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;setName&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;String name&lt;/font&gt;&lt;font color="#000000"&gt;) {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;13&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;_name = name;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;14&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;15&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;16&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#c00000"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;getAge&lt;/font&gt;&lt;font color="#000000"&gt;() {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;17&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;_age;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;18&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;19&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;20&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#c00000"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;setAge&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#c00000"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;age&lt;/font&gt;&lt;font color="#000000"&gt;) {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;21&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;_age = age;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;22&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;23&lt;/font&gt; &lt;font color="#000000"&gt;}&lt;/font&gt; &lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Here we have the &lt;code&gt;Player&lt;/code&gt; class that contains &lt;code&gt;Name&lt;/code&gt; and &lt;code&gt;Age&lt;/code&gt; properties that are exposed using the Java Bean "getter/setter" convention. &lt;br /&gt;
&lt;br /&gt;
Now we will create the &lt;code&gt;TeamType&lt;/code&gt; enumerator: &lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;font color="#808080"&gt;1&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;jws;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;2&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;3&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;public enum &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;TeamType &lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;4&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;Soccer,&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;5&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;Football&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;6&lt;/font&gt; &lt;font color="#000000"&gt;}&lt;/font&gt; &lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
This allows us to limit the types of teams returned to only &lt;code&gt;Soccer&lt;/code&gt; and &lt;code&gt;Football&lt;/code&gt;. &lt;br /&gt;
&lt;br /&gt;
The next step is to create the &lt;code&gt;Team&lt;/code&gt; class: &lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;font color="#808080"&gt;01&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;jws;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;02&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;03&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;java.util.*;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;04&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;05&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;Team &lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;06&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;07&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;String _name;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;08&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;TeamType _teamtype;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;09&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;List&amp;lt;Player&amp;gt; _players;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;10&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;11&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;String getName&lt;/font&gt;&lt;font color="#000000"&gt;() {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;12&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;_name;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;13&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;14&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;15&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#c00000"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;setName&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;String name&lt;/font&gt;&lt;font color="#000000"&gt;) {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;16&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;_name = name;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;17&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;18&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;19&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;TeamType getTeamType&lt;/font&gt;&lt;font color="#000000"&gt;() {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;20&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;_teamtype;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;21&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;22&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;23&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#c00000"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;setTeamType&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;TeamType teamtype&lt;/font&gt;&lt;font color="#000000"&gt;) {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;24&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;_teamtype = teamtype;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;25&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;26&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;27&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;List&amp;lt;Player&amp;gt; getPlayers&lt;/font&gt;&lt;font color="#000000"&gt;() {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;28&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;_players;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;29&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;30&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;31&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#c00000"&gt;&lt;b&gt;void &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;setPlayers&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;List&amp;lt;Player&amp;gt; players&lt;/font&gt;&lt;font color="#000000"&gt;) {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;32&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;_players = players;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;33&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;34&lt;/font&gt; &lt;font color="#000000"&gt;}&lt;/font&gt; &lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Here you can see now that we have included in the &lt;code&gt;Team&lt;/code&gt; class a &lt;code&gt;Name&lt;/code&gt; property, a &lt;code&gt;TeamType&lt;/code&gt; property based off our &lt;code&gt;enum&lt;/code&gt; above, and finally a generic list of &lt;code&gt;Player&lt;/code&gt; objects. Again these are all exposed using the getter/setter convention. &lt;br /&gt;
&lt;br /&gt;
Now that our &lt;code&gt;Team&lt;/code&gt; class is complete, our next step is to create the Java Web Service: &lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;font color="#808080"&gt;01&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;package &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;jws;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;02&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;03&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;javax.jws.WebService;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;04&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;java.util.*;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;05&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;import &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;java.util.ArrayList;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;06&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;07&lt;/font&gt; &lt;font color="#646464"&gt;@WebService&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;08&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;public class &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;HelloWorldJWS &lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;09&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;10&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;public &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;List&amp;lt;Team&amp;gt; getTeams&lt;/font&gt;&lt;font color="#000000"&gt;() {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;11&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;GetTeamInfo&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;12&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;13&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;14&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;List&amp;lt;Team&amp;gt; GetTeamInfo&lt;/font&gt;&lt;font color="#000000"&gt;() {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;15&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;16&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;Player Memori = CreatePlayer&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"Memori"&lt;/font&gt;&lt;font color="#000000"&gt;, &lt;/font&gt;&lt;font color="#990000"&gt;15&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;17&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;Player Mikayli = CreatePlayer&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"Mikayli"&lt;/font&gt;&lt;font color="#000000"&gt;, &lt;/font&gt;&lt;font color="#990000"&gt;12&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;18&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;Player Myranda = CreatePlayer&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"Myranda"&lt;/font&gt;&lt;font color="#000000"&gt;, &lt;/font&gt;&lt;font color="#990000"&gt;11&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;19&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;Player Elias = CreatePlayer&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"Elias"&lt;/font&gt;&lt;font color="#000000"&gt;, &lt;/font&gt;&lt;font color="#990000"&gt;13&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;20&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;Player Elogan = CreatePlayer&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"Elogan"&lt;/font&gt;&lt;font color="#000000"&gt;, &lt;/font&gt;&lt;font color="#990000"&gt;6&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;21&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;22&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;List&amp;lt;Player&amp;gt; girlplayers = &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;ArrayList&amp;lt;Player&amp;gt;&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;23&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;List&amp;lt;Player&amp;gt; boyplayers = &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;ArrayList&amp;lt;Player&amp;gt;&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;24&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;25&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;girlplayers.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;Memori&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;26&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;girlplayers.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;Mikayli&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;27&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;girlplayers.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;Myranda&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;28&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;boyplayers.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;Elias&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;29&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;boyplayers.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;Elogan&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;30&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;31&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;Team dagirls = &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;Team&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;32&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;33&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;dagirls.setName&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"DaGirls"&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;34&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;dagirls.setTeamType&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;TeamType.Soccer&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;35&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;dagirls.setPlayers&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;girlplayers&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;36&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;37&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;Team daboys = &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;Team&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;38&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;39&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;daboys.setName&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"DaBoys"&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;40&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;daboys.setTeamType&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;TeamType.Football&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;41&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;daboys.setPlayers&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;boyplayers&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;42&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;43&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;List&amp;lt;Team&amp;gt; teams = &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;ArrayList&amp;lt;Team&amp;gt;&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;44&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;45&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;teams.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;dagirls&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;46&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;teams.add&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;daboys&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;47&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;48&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;teams;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;49&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;50&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;51&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;private &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;Player CreatePlayer&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;String name, &lt;/font&gt;&lt;font color="#c00000"&gt;&lt;b&gt;int &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;age&lt;/font&gt;&lt;font color="#000000"&gt;) {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;52&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;53&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;Player newplayer = &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;Player&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;54&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;55&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;newplayer.setName&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;name&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;56&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;newplayer.setAge&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;age&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;57&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;58&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;return &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;newplayer;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;59&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;60&lt;/font&gt; &lt;font color="#000000"&gt;}&lt;/font&gt; &lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Like our WCF service, our JWS returns one method &lt;code&gt;getTeams()&lt;/code&gt; that returns a generic list of &lt;code&gt;Team&lt;/code&gt; objects. The &lt;code&gt;GetTeamInfo()&lt;/code&gt; method is used to create some test teams and players. &lt;br /&gt;
&lt;br /&gt;
The final step is to create the .NET client. You can either use the &lt;code&gt;svcutil.exe&lt;/code&gt; utility or Visual Studio (&lt;i&gt;add reference wizard&lt;/i&gt;) to create the proxy based on the JWS WSDL. &lt;br /&gt;
&lt;br /&gt;
 
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;JWS.&lt;span class="inlinecode"&gt;HelloWorldJWSClient&lt;/span&gt; webclient = &lt;span class="kwrd"&gt;new&lt;/span&gt; JWS.&lt;span class="inlinecode"&gt;HelloWorldJWSClient&lt;/span&gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;JWS.&lt;span class="inlinecode"&gt;team&lt;/span&gt;[] Teams = webclient.getTeams();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (JWS.&lt;span class="inlinecode"&gt;team&lt;/span&gt; team &lt;span class="kwrd"&gt;in&lt;/span&gt; Teams)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;   jws.Text += &lt;span class="str"&gt;"&amp;lt;br /&amp;gt;&amp;lt;b&amp;gt;Team name&amp;lt;/b&amp;gt;: "&lt;/span&gt; + team.name + &lt;span class="str"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;   jws.Text += &lt;span class="str"&gt;"&amp;lt;b&amp;gt;Team type&amp;lt;/b&amp;gt;: "&lt;/span&gt; + team.teamType + &lt;span class="str"&gt;"&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;"&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;   jws.Text += &lt;span class="str"&gt;"Players:&amp;lt;br /&amp;gt;"&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;   &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (JWS.&lt;span class="inlincode"&gt;player&lt;/span&gt; player &lt;span class="kwrd"&gt;in&lt;/span&gt; team.players)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;   {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;       jws.Text += &lt;span class="str"&gt;"&amp;amp;nbsp;&amp;amp;nbsp;Name: &amp;lt;i&amp;gt;"&lt;/span&gt; + player.name + &lt;span class="str"&gt;"&amp;lt;/i&amp;gt;&amp;lt;br /&amp;gt;"&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;       jws.Text += &lt;span class="str"&gt;"&amp;amp;nbsp;&amp;amp;nbsp;Age: &amp;lt;i&amp;gt;"&lt;/span&gt; + player.age.ToString() + &lt;span class="str"&gt;"&amp;lt;/i&amp;gt;&amp;lt;br /&amp;gt;"&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;   }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Here is the sample output from the browser: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;table cellpadding="15" border="1" style="font-size: 8pt; font-family: Arial"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;b&gt;Team name&lt;/b&gt;: DaGirls&lt;br /&gt;
            &lt;b&gt;Team type&lt;/b&gt;: Soccer&lt;br /&gt;
            &lt;br /&gt;
            Players:&lt;br /&gt;
              Name: &lt;i&gt;Memori&lt;/i&gt;&lt;br /&gt;
              Age: &lt;i&gt;15&lt;/i&gt;&lt;br /&gt;
              Name: &lt;i&gt;Mikayli&lt;/i&gt;&lt;br /&gt;
              Age: &lt;i&gt;12&lt;/i&gt;&lt;br /&gt;
              Name: &lt;i&gt;Myranda&lt;/i&gt;&lt;br /&gt;
              Age: &lt;i&gt;11&lt;/i&gt;&lt;br /&gt;
            &lt;br /&gt;
            &lt;b&gt;Team name&lt;/b&gt;: DaBoys&lt;br /&gt;
            &lt;b&gt;Team type&lt;/b&gt;: Football&lt;br /&gt;
            &lt;br /&gt;
            Players:&lt;br /&gt;
              Name: &lt;i&gt;Elias&lt;/i&gt;&lt;br /&gt;
              Age: &lt;i&gt;13&lt;/i&gt;&lt;br /&gt;
              Name: &lt;i&gt;Elogan&lt;/i&gt;&lt;br /&gt;
              Age: &lt;i&gt;6&lt;/i&gt;&lt;br /&gt;
             &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;
&lt;i&gt;This article is the third in a three part series demonstrating the interoperability between .NET WCF services and Java Web Services. &lt;br /&gt;
&lt;br /&gt;
Article Series:&lt;/i&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/28/net-wcf-and-java-ws-interoperability-part1.aspx"&gt;Part I - An overview of the design approach&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/29/net-wcf-and-java-ws-interoperability-part2.aspx"&gt;Part II - Create the WCF service consumed by a Java client&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://geekswithblogs.net/ballhaus/aggbug/137262.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ballhaus/comments/137262.aspx</wfw:comment>
        <slash:comments>1</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ballhaus/comments/commentRss/137262.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ballhaus/services/trackbacks/137262.aspx</trackback:ping>
    </entry>
    <entry>
        <title>.NET WCF services and Java Web Services Interoperability - Part 2 of 3</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ballhaus/archive/2009/12/29/net-wcf-and-java-ws-interoperability-part2.aspx" />
        <id>http://geekswithblogs.net/ballhaus/archive/2009/12/29/net-wcf-and-java-ws-interoperability-part2.aspx</id>
        <published>2009-12-29T15:50:55-05:00:00</published>
        <updated>2009-12-30T09:31:07Z</updated>
        <summary type="html">Part 2 - This article will demonstrate how you can have interoperability between JWS and WCF using custom data types, generic lists, and enums</summary>
        <content type="html">&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #cc6633; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.inlinecode
        {
            font-family: Consolas, Courier New, Courier, Monospace;
            color: #2b91af;
            font-size: small;
        }
.csharpcode .lnum { color: #606060; }
        .style1
        {
            color: #2B91AF;
        }
    ]]&gt;&lt;/style&gt;&lt;b&gt;Part II - Create the WCF "Team" service consumed by a Java client&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
This article is the second in a three part series demonstrating the interoperability between .NET WCF services and Java Web Services. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; &lt;i&gt;If you haven't read Part I you should &lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/28/net-wcf-and-java-ws-interoperability-part1.aspx"&gt;review that article&lt;/a&gt; before continuing on.&lt;/i&gt; &lt;br /&gt;
&lt;br /&gt;
In Part I we reviewed the class diagram of the &lt;span class="inlinecode"&gt;Team&lt;/span&gt; class, so our first step is to create the .NET classes in support of that model. We will start by creating the &lt;span class="inlinecode"&gt;Player&lt;/span&gt; class: &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[&lt;span class="inlinecode"&gt;DataContractAttribute&lt;/span&gt;(Name = &lt;span class="str"&gt;"Player"&lt;/span&gt;, Namespace = &lt;span class="str"&gt;"HelloWorldWCFs"&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; &lt;span class="inlinecode"&gt;Player&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;DataMemberAttribute&lt;/span&gt;(Name=&lt;span class="str"&gt;"Name"&lt;/span&gt;,IsRequired=&lt;span class="kwrd"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;DataMemberAttribute&lt;/span&gt;(Name = &lt;span class="str"&gt;"Age"&lt;/span&gt;, IsRequired = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Age { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
With WCF, all classes must have a data contract established so that they can be serialized to XML and sent down the wire. Here we created a simple &lt;span class="inlinecode"&gt;Player&lt;/span&gt; class decorated with the &lt;span class="inlinecode"&gt;DataContractAttribute&lt;/span&gt;. This class contains &lt;i&gt;Name&lt;/i&gt; and &lt;i&gt;Age&lt;/i&gt; properties for each player and are decorated them with &lt;span class="inlinecode"&gt;DataMemberAttribute&lt;/span&gt; attribute. &lt;br /&gt;
&lt;br /&gt;
Next we will create the &lt;span class="inlinecode"&gt;TeamType&lt;/span&gt; enumeration: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[&lt;span class="inlinecode"&gt;DataContractAttribute&lt;/span&gt;(Name = &lt;span class="str"&gt;"TeamType"&lt;/span&gt;, Namespace = &lt;span class="str"&gt;"HelloWorldWCFs"&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;enum&lt;/span&gt; &lt;span class="inlinecode"&gt;TeamType&lt;/span&gt; : &lt;span class="kwrd"&gt;int&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;EnumMemberAttribute&lt;/span&gt;(Value=&lt;span class="str"&gt;"Soccer"&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    Soccer = 0,&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;EnumMemberAttribute&lt;/span&gt;(Value = &lt;span class="str"&gt;"Football"&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    Football = 1&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
With this enumeration, we simply want the user to be able to either choose &lt;i&gt;Soccer&lt;/i&gt; or &lt;i&gt;Football&lt;/i&gt; as an option for a team type. &lt;br /&gt;
&lt;br /&gt;
The next step is to create the Team class: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[&lt;span class="inlinecode"&gt;DataContractAttribute&lt;/span&gt;(Name = &lt;span class="str"&gt;"Team"&lt;/span&gt;, Namespace = &lt;span class="str"&gt;"HelloWorldWCFs"&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;[&lt;span class="inlinecode"&gt;XmlInclude&lt;/span&gt;(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="inlinecode"&gt;TeamType&lt;/span&gt;))]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;[&lt;span class="inlinecode"&gt;KnownType&lt;/span&gt;(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="inlinecode"&gt;TeamType&lt;/span&gt;))]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;[&lt;span class="inlinecode"&gt;XmlInclude&lt;/span&gt;(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="inlinecode"&gt;Player&lt;/span&gt;))]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;[&lt;span class="inlinecode"&gt;KnownType&lt;/span&gt;(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="inlinecode"&gt;Player&lt;/span&gt;))]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; &lt;span class="inlinecode"&gt;Team&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;DataMemberAttribute&lt;/span&gt;(Name = &lt;span class="str"&gt;"Name"&lt;/span&gt;, IsRequired = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;DataMemberAttribute&lt;/span&gt;(Name = &lt;span class="str"&gt;"teamtype"&lt;/span&gt;, IsRequired = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="inlinecode"&gt;TeamType&lt;/span&gt; teamtype { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;DataMemberAttribute&lt;/span&gt;(Name = &lt;span class="str"&gt;"Players"&lt;/span&gt;, IsRequired = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="inlinecode"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="inlinecode"&gt;Player&lt;/span&gt;&amp;gt; Players { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
The &lt;span class="inlinecode"&gt;XmlInclude&lt;/span&gt; and &lt;span class="inlinecode"&gt;KnownType&lt;/span&gt; decorations force the &lt;span class="inlinecode"&gt;Player&lt;/span&gt; and &lt;span class="inlinecode"&gt;TeamType&lt;/span&gt; classes to be included as part of the &lt;span class="inlinecode"&gt;Team&lt;/span&gt; class in the serialization process. &lt;br /&gt;
&lt;br /&gt;
Now the next step is to create the interface for our service: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[&lt;span class="inlinecode"&gt;ServiceContract&lt;/span&gt;(Name=&lt;span class="str"&gt;"IHelloWorldWCF"&lt;/span&gt;, Namespace = &lt;span class="str"&gt;"HelloWorldWCFs"&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; &lt;span class="inlinecode"&gt;IHelloWorldWCF&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;OperationContract&lt;/span&gt;]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="inlinecode"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="inlinecode"&gt;Team&lt;/span&gt;&amp;gt; getTeams();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
As we can see, our service interface has one method &lt;i&gt;getTeams()&lt;/i&gt; that will return a generic &lt;span class="inlinecode"&gt;List&lt;/span&gt; of &lt;span class="inlinecode"&gt;Team&lt;/span&gt; objects. &lt;br /&gt;
&lt;br /&gt;
Finally we need to create the class that implements our new &lt;span class="inlinecode"&gt;ServiceContract&lt;/span&gt; interface we just created: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; &lt;span class="inlinecode"&gt;HelloWorldWCF&lt;/span&gt; : &lt;span class="inlinecode"&gt;IHelloWorldWCF&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{    &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="inlinecode"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="inlinecode"&gt;Team&lt;/span&gt;&amp;gt; getTeams()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; GetTeamInfo();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="inlinecode"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="inlinecode"&gt;Team&lt;/span&gt;&amp;gt; GetTeamInfo()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        var Memori = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;Player&lt;/span&gt;() { Name = &lt;span class="str"&gt;"Memori"&lt;/span&gt;, Age = 15 };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        var Mikayli = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;Player&lt;/span&gt;() { Name = &lt;span class="str"&gt;"Mikayli"&lt;/span&gt;, Age = 12 };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        var Myranda = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;Player&lt;/span&gt;() { Name = &lt;span class="str"&gt;"Myranda"&lt;/span&gt;, Age = 11 };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        var Elias = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;Player&lt;/span&gt;() { Name = &lt;span class="str"&gt;"Elias"&lt;/span&gt;, Age = 13 };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        var Elogan = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;Player&lt;/span&gt;() { Name = &lt;span class="str"&gt;"Elogan"&lt;/span&gt;, Age = 6 };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        var girlsteam = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;Team&lt;/span&gt;() { &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;            Name = &lt;span class="str"&gt;"DaGirls"&lt;/span&gt;, &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;            teamtype = &lt;span class="inlinecode"&gt;TeamType&lt;/span&gt;.Soccer, &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;            Players = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="inlinecode"&gt;Player&lt;/span&gt;&amp;gt;() { Memori, Mikayli, Myranda } &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;        var boysteam = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;Team&lt;/span&gt;() { &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;            Name = &lt;span class="str"&gt;"DaBoys"&lt;/span&gt;, &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;            teamtype = &lt;span class="inlinecode"&gt;TeamType&lt;/span&gt;.Football, &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;            Players = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="inlinecode"&gt;Player&lt;/span&gt;&amp;gt;() { Elias, Elogan } &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;        };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Team&amp;gt;() { girlsteam, boysteam };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Here we are implementing the &lt;i&gt;getTeams()&lt;/i&gt; method and subsequently calling another method &lt;i&gt;getTeamInfo()&lt;/i&gt; to actually populate our class with team and player information to return to the client. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; Once you have built the service, you will need to make one modification to the configuration file. In the &lt;i&gt;&amp;lt;&lt;code&gt;system.serviceModel&lt;/code&gt;&amp;gt;&lt;/i&gt; section you will need to change the endpoint binding from the default &lt;i&gt;wsBinding&lt;/i&gt; to &lt;i&gt;basicHttpBinding&lt;/i&gt;. This allows the service endpoint to be exposed as WS-1 BP 1.1 compliant and use HTTP as the transport for sending SOAP 1.1 messages. &lt;br /&gt;
&lt;br /&gt;
Our next task is to create the Java client. &lt;br /&gt;
&lt;br /&gt;
We can create a new client in NetBeans by selecting &lt;i&gt;New&lt;/i&gt; -&amp;gt; &lt;i&gt;Web Service Client&lt;/i&gt; from an existing project such as a &lt;i&gt;web application project&lt;/i&gt;. When prompted, you select to create the new project by entering a URL to an existing WSDL. Here you enter the URL path to the WSDL that was generated from the WCF service we just created and NetBeans will then create the required proxy code for you. This proxy code will handle all of the data type mappings for you as well as define the generic lists and enums based on the WSDL. &lt;br /&gt;
&lt;br /&gt;
Hint: &lt;i&gt;You will want to enter a package name for the proxy code so that it is encapsulated, otherwise there will be multiple packages the classes based on the WSDL naming schema.&lt;/i&gt; &lt;br /&gt;
&lt;br /&gt;
With the proxy code created, we can now write some code in our JSP file to loop through the data returned from the service and display in a browser: &lt;br /&gt;
&lt;br /&gt;
&lt;!-- ======================================================== --&gt;&lt;!-- = Java Sourcecode to HTML automatically converted code = --&gt;&lt;!-- =   Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard  markus@jave.de   = --&gt;&lt;code&gt;&lt;font color="#808080"&gt;01&lt;/font&gt; &lt;font color="#000000"&gt;&amp;lt;%&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;02&lt;/font&gt; &lt;font color="#000000"&gt;wcfservice.HelloWorldWCF service = &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;new &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;wcfservice.HelloWorldWCF&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;03&lt;/font&gt; &lt;font color="#000000"&gt;wcfservice.IHelloWorldWCF client = service.getBasicHttpBindingIHelloWorldWCF&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;04&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;05&lt;/font&gt; &lt;font color="#0000c0"&gt;&lt;b&gt;for &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;wcfservice.Team team : client.getTeams&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;.getTeam&lt;/font&gt;&lt;font color="#000000"&gt;()) {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;06&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;out.println&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"&amp;lt;br /&amp;gt;&amp;lt;b&amp;gt;Team name&amp;lt;/b&amp;gt;: " &lt;/font&gt;&lt;font color="#000000"&gt;+ team.getName&lt;/font&gt;&lt;font color="#000000"&gt;() &lt;/font&gt;&lt;font color="#000000"&gt;+ &lt;/font&gt;&lt;font color="#990000"&gt;"&amp;lt;br /&amp;gt;"&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;07&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;out.println&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"&amp;lt;b&amp;gt;Team type&amp;lt;/b&amp;gt;: " &lt;/font&gt;&lt;font color="#000000"&gt;+ team.getTeamtype&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;.value&lt;/font&gt;&lt;font color="#000000"&gt;() &lt;/font&gt;&lt;font color="#000000"&gt;+ &lt;/font&gt;&lt;font color="#990000"&gt;"&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;"&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;08&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;out.println&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"Players:&amp;lt;br /&amp;gt;"&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;09&lt;/font&gt; &lt;br /&gt;
&lt;font color="#808080"&gt;10&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#0000c0"&gt;&lt;b&gt;for &lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;wcfservice.Player player : team.getPlayers&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;.getPlayer&lt;/font&gt;&lt;font color="#000000"&gt;()) {&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;11&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;out.println&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"&amp;amp;nbsp;&amp;amp;nbsp;Name: &amp;lt;i&amp;gt;" &lt;/font&gt;&lt;font color="#000000"&gt;+ player.getName&lt;/font&gt;&lt;font color="#000000"&gt;() &lt;/font&gt;&lt;font color="#000000"&gt;+ &lt;/font&gt;&lt;font color="#990000"&gt;"&amp;lt;/i&amp;gt;&amp;lt;br /&amp;gt;"&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;12&lt;/font&gt; &lt;font color="#ffffff"&gt;        &lt;/font&gt;&lt;font color="#000000"&gt;out.println&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#990000"&gt;"&amp;amp;nbsp;&amp;amp;nbsp;Age: &amp;lt;i&amp;gt;" &lt;/font&gt;&lt;font color="#000000"&gt;+ player.getAge&lt;/font&gt;&lt;font color="#000000"&gt;() &lt;/font&gt;&lt;font color="#000000"&gt;+ &lt;/font&gt;&lt;font color="#990000"&gt;"&amp;lt;/i&amp;gt;&amp;lt;br /&amp;gt;"&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;13&lt;/font&gt; &lt;font color="#ffffff"&gt;    &lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;14&lt;/font&gt; &lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;
&lt;font color="#808080"&gt;15&lt;/font&gt; &lt;font color="#000000"&gt;%&amp;gt;&lt;/font&gt; &lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Here is the sample output from the browser: &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;table cellpadding="15" border="1" style="font-size: 8pt; font-family: Arial"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;b&gt;Team name&lt;/b&gt;: DaGirls&lt;br /&gt;
            &lt;b&gt;Team type&lt;/b&gt;: Soccer&lt;br /&gt;
            &lt;br /&gt;
            Players:&lt;br /&gt;
              Name: &lt;i&gt;Memori&lt;/i&gt;&lt;br /&gt;
              Age: &lt;i&gt;15&lt;/i&gt;&lt;br /&gt;
              Name: &lt;i&gt;Mikayli&lt;/i&gt;&lt;br /&gt;
              Age: &lt;i&gt;12&lt;/i&gt;&lt;br /&gt;
              Name: &lt;i&gt;Myranda&lt;/i&gt;&lt;br /&gt;
              Age: &lt;i&gt;11&lt;/i&gt;&lt;br /&gt;
            &lt;br /&gt;
            &lt;b&gt;Team name&lt;/b&gt;: DaBoys&lt;br /&gt;
            &lt;b&gt;Team type&lt;/b&gt;: Football&lt;br /&gt;
            &lt;br /&gt;
            Players:&lt;br /&gt;
              Name: &lt;i&gt;Elias&lt;/i&gt;&lt;br /&gt;
              Age: &lt;i&gt;13&lt;/i&gt;&lt;br /&gt;
              Name: &lt;i&gt;Elogan&lt;/i&gt;&lt;br /&gt;
              Age: &lt;i&gt;6&lt;/i&gt;&lt;br /&gt;
             &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br /&gt;
&lt;i&gt;This article is the second in a three part series demonstrating the interoperability between .NET WCF services and Java Web Services. &lt;br /&gt;
&lt;br /&gt;
Article Series:&lt;/i&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/28/net-wcf-and-java-ws-interoperability-part1.aspx"&gt;Part I - An overview of the design approach&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/29/net-wcf-and-java-ws-interoperability-part3.aspx"&gt;Part III - Create the Java Web Service consumed by a .NET client&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://geekswithblogs.net/ballhaus/aggbug/137253.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ballhaus/comments/137253.aspx</wfw:comment>
        <slash:comments>3</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ballhaus/comments/commentRss/137253.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ballhaus/services/trackbacks/137253.aspx</trackback:ping>
    </entry>
    <entry>
        <title>.NET WCF services and Java Web Services Interoperability - Part 1 of 3</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ballhaus/archive/2009/12/28/net-wcf-and-java-ws-interoperability-part1.aspx" />
        <id>http://geekswithblogs.net/ballhaus/archive/2009/12/28/net-wcf-and-java-ws-interoperability-part1.aspx</id>
        <published>2009-12-28T15:25:40-05:00:00</published>
        <updated>2009-12-30T09:28:12Z</updated>
        <summary type="html">Part 1 - This article will demonstrate how you can have interoperability between JWS and WCF using custom data types, generic lists, and enums</summary>
        <content type="html">&lt;p&gt;Recently I was involved in a discussion around the concept of interoperability with WCF services. Half of our I.T. shop develops in the Java world and the other half in .NET, so as we were designing our next WCF service we asked ourselves the question, " Will this service ever need to be consumed by a Java client?" The answer to that question would make a difference in our design approach since we were told by our consultants friends that if we were to use custom data types in .NET, we were foregoing the opportunity for our Java teams from ever utilizing the service. We decided not worry about interoperability drawbacks and go ahead and design the service with custom .NET data types, generic lists, enums, etc. &lt;br /&gt;
&lt;br /&gt;
This article is designed to show you how you CAN actually have pretty solid interoperability between JWS and WCF using custom data types, generic lists, and enums both ways; a JWS service consumed by a .NET client as well as a WCF service consumed by a Java client. We'll break it down into three postings:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;An overview of our design approach&lt;/li&gt;
    &lt;li&gt;Create the WCF service consumed by a Java client&lt;/li&gt;
    &lt;li&gt;Create the Java Web Service consumed by a .NET client&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Both the WCF and the JWS services will have the same class structure. Below you will find a class diagram that demonstrates the class layout: &lt;br /&gt;
&lt;br /&gt;
&lt;img alt="Team.jpg" src="/images/geekswithblogs_net/ballhaus/Team.jpg" /&gt; &lt;br /&gt;&lt;br /&gt;
The services will return a list of "Teams" that contain some basic information for each including:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Team Name&lt;/li&gt;
    &lt;li&gt;Team Type
    &lt;ul&gt;
        &lt;li&gt;&lt;i&gt;This will be an enumeration value of either "Soccer" or "Football"&lt;/i&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;Players
    &lt;ul&gt;
        &lt;li&gt;&lt;i&gt;This will be a generic list of "Player"(s) that contain a Name and Age&lt;/i&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you will see the code for both platforms is very similar and there are only a couple of caveats to getting the solutions to work "out of the box", which we will discuss later. &lt;br /&gt;
&lt;br /&gt;
For the Java development, we will be using NetBeans IDE version 6.8 and for the .NET development, we will be using Visual Studio 2008. &lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;This article is the first in a three part series demonstrating the interoperability between .NET WCF services and Java Web Services. &lt;br /&gt;
&lt;br /&gt;
Article Series:&lt;/i&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/28/net-wcf-and-java-ws-interoperability-part2.aspx"&gt;Part II - Create the WCF service consumed by a Java client&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/29/net-wcf-and-java-ws-interoperability-part3.aspx"&gt;Part III - Create the Java Web Service consumed by a .NET client&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
 &lt;/p&gt;&lt;img src="http://geekswithblogs.net/ballhaus/aggbug/137240.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ballhaus/comments/137240.aspx</wfw:comment>
        <slash:comments>1</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ballhaus/comments/commentRss/137240.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ballhaus/services/trackbacks/137240.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Integration Testing WCF services with Unity</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ballhaus/archive/2009/12/21/integration-testing-wcf-services-with-unity.aspx" />
        <id>http://geekswithblogs.net/ballhaus/archive/2009/12/21/integration-testing-wcf-services-with-unity.aspx</id>
        <published>2009-12-21T12:46:29-05:00:00</published>
        <updated>2009-12-21T12:49:24Z</updated>
        <summary type="html">Integration testing your WCF service with Unity</summary>
        <content type="html">&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .cus { color: #2b91af; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.inlinecode { font-family: Consolas, Courier New, Courier, Monospace;color:#2b91af;font-size:small; }

]]&gt;&lt;/style&gt;Previously I wrote an article regarding &lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/12/unit-testing-your-wcf-service.aspx"&gt;Unit Testing WCF with Unity&lt;/a&gt;. Unit testing allows you to test a single component of your application, completely independent of any other components or systems. Once you have successfully created your unit tests, the logical next step is to see how your service works when connected to other components. These integration tests allow you, for example, to run tests against your service and see how it behaves when writing to a database or consuming another service. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Integration Testing WCF services&lt;/b&gt; &lt;br /&gt;
To perform integration testing against a service, you have a few different options to go with; each with their own advantages and disadvantages:
&lt;ol&gt;
    &lt;li&gt;Utilize the Asp.Net web server (Cassini) within Visual Studio&lt;/li&gt;
    &lt;li&gt;Deploy your service to IIS&lt;/li&gt;
    &lt;li&gt;Setting up your service host and starting it from within the integration test environment&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;b&gt;1. Use the Asp.Net web server in Visual Studio&lt;/b&gt; &lt;br /&gt;
This is the most convenient approach during the development phase, however you will have to remember to keep re-starting the development web server whenever it times out. You will also need to create a proxy class (&lt;i&gt;using svcutil.exe or the service reference wizard in VS&lt;/i&gt;) as well as maintain the endpoint configuration for the service in your web.config or app.config file. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;2. Deploy to IIS&lt;/b&gt; &lt;br /&gt;
This option allows you to test your service against IIS. A web application directory will need to be created (&lt;i&gt;VS can do this for you&lt;/i&gt;) but unlike Cassini, you won't have to worry about the server timing out. This option isn't the best choice if you frequently test different services and will also leave you having to handle multiple web application directories lounging around IIS. Again, you will need to create a proxy class as well as have the service endpoint in your configuration file. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;3. Create/start your service host "&lt;i&gt;on the fly&lt;/i&gt;"&lt;/b&gt; &lt;br /&gt;
This is the "&lt;i&gt;cleanest&lt;/i&gt;" approach and does not have any of the drawbacks of the first two options. The service is automatically created/started/stopped from within the test class setup/teardown code whenever you run your tests. Another advantage is that you can run your unit/integration tests automatically and unattended using the Build Server within TFS (&lt;i&gt;Team Foundation Server&lt;/i&gt;). &lt;br /&gt;
&lt;br /&gt;
Below is an example of how you would accomplish this: &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[&lt;span class="inlinecode"&gt;TestClass]&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; &lt;span class="inlinecode"&gt;WcfIntegrationTests&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="inlinecode"&gt;ServiceHost&lt;/span&gt; svcHost;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;ClassInitialize&lt;/span&gt;]&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestClassInitialize(&lt;span class="inlinecode"&gt;TestContext&lt;/span&gt; testContext)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        svcHost = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;ServiceHost&lt;/span&gt;(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="inlinecode"&gt;WcfService&lt;/span&gt;), &lt;span class="kwrd"&gt;new&lt;/span&gt;[] { &lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(&lt;span class="attr"&gt;"http://localhost/"&lt;/span&gt;) });&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        svcHost.AddServiceEndpoint(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="inlinecode"&gt;IWcfService&lt;/span&gt;), &lt;span class="kwrd"&gt;new&lt;/span&gt; BasicHttpBinding(), &lt;span class="attr"&gt;"WcfService"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        svcHost.Open();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;ClassCleanup&lt;/span&gt;]&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestClassCleanup()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        svcHost.Close();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;    [&lt;span class="inlinecode"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; GetUsers()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;        var endptaddr = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;EndpointAddress&lt;/span&gt;(&lt;span class="attr"&gt;"http://localhost/WcfService"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;        var Wcfproxy = ChannelFactory&amp;lt;&lt;span class="inlinecode"&gt;IWcfService&lt;/span&gt;&amp;gt;.CreateChannel(&lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;BasicHttpBinding&lt;/span&gt;(), endptaddr);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;        var result = Wcfproxy.GetUsers();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;        &lt;span class="inlinecode"&gt;Assert&lt;/span&gt;.IsTrue(result.Count &amp;gt; 0, &lt;span class="attr"&gt;"No users found"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Now if your WCF service is utilizing an IoC container for unit testing by way of WCF extension points (&lt;i&gt;see my previous post on &lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/12/unit-testing-your-wcf-service.aspx"&gt;Unit Testing WCF with Unity&lt;/a&gt;&lt;/i&gt;), you will notice right away that you receive the following error...&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: red"&gt;&lt;i&gt;System.InvalidOperationException: The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor&lt;/i&gt;&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
The reason for this error is because of our &lt;i&gt;ServiceHostFactory&lt;/i&gt; we created for our unit tests. The Factory doesn't have any available public methods to be able to create the service host from a given service type. What we will need to do is create a wrapper class for the factory and define a public method to create the service host and allow for a type to be passed in as a parameter. &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; &lt;span class="inlinecode"&gt;WcfServiceHostFactoryWrapper&lt;/span&gt; : &lt;span class="inlinecode"&gt;WcfSvcServiceHostFactory&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="inlinecode"&gt;ServiceHost&lt;/span&gt; CreateServiceHost(&lt;span class="inlinecode"&gt;Type&lt;/span&gt; serviceType, &lt;span class="kwrd"&gt;string&lt;/span&gt; baseAddress)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; CreateServiceHost(serviceType, &lt;span class="kwrd"&gt;new&lt;/span&gt;[] { &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;Uri&lt;/span&gt;(baseAddress) });&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;[&lt;span class="inlinecode"&gt;ClassInitialize&lt;/span&gt;]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestClassInitialize(&lt;span class="inlinecode"&gt;TestContext&lt;/span&gt; testContext)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    var serviceHostFactoryHarness = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;WcfServiceHostFactoryWrapper&lt;/span&gt;();&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    svcHost = serviceHostFactoryHarness.CreateServiceHost(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="inlinecode"&gt;WcfService&lt;/span&gt;), &lt;span class="attr"&gt;"http://127.0.0.1/"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    svcHost.AddServiceEndpoint(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="inlinecode"&gt;IWcfService&lt;/span&gt;), &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="inlinecode"&gt;BasicHttpBinding()&lt;/span&gt;, &lt;span class="attr"&gt;"WcfService"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    svcHost.Open();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Now you can just select the test to run from the Visual Studio Test List Editor, or run unattended from the TFS Build Server, with no dependencies on IIS or configuration. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;&lt;img src="http://geekswithblogs.net/ballhaus/aggbug/137158.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ballhaus/comments/137158.aspx</wfw:comment>
        <slash:comments>1</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ballhaus/comments/commentRss/137158.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ballhaus/services/trackbacks/137158.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Unit Testing your WCF Service</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ballhaus/archive/2009/12/12/unit-testing-your-wcf-service.aspx" />
        <id>http://geekswithblogs.net/ballhaus/archive/2009/12/12/unit-testing-your-wcf-service.aspx</id>
        <published>2009-12-12T23:11:14-05:00:00</published>
        <updated>2009-12-21T12:54:29Z</updated>
        <summary type="html">Properly unit testing your WCF services utilizing IoC dependency injection and WCF extension points.</summary>
        <content type="html">&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .cus { color: #2b91af; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.inlinecode { font-family: Consolas, Courier New, Courier, Monospace;color:#2b91af;font-size:small; }]]&gt;&lt;/style&gt;So you're familiar with TDD (&lt;i&gt;test-driven development&lt;/i&gt;)... Failing your tests first, then building up your classes to satisfy your tests so they're all "&lt;font style="font-weight: bold; color: green"&gt;Green&lt;/font&gt;"...&lt;br /&gt;
&lt;br /&gt;
This is a typical scenario and is pretty straightfoward with .NET.&lt;br /&gt;
&lt;br /&gt;
Well now you have to create a WCF service and implement unit testing to ensure you don't break your service when you build out a new version. &lt;br /&gt;
&lt;br /&gt;
This is a bit more challenging but you do have some different options:
&lt;ol&gt;
    &lt;li&gt;Separate out your service code into a library and run your tests against the library code...
    &lt;ul&gt;
        &lt;li&gt;&lt;i&gt;This really only tests your library code and doesn't actually allow you to instate the service itself to perform a realistic test. For example, you might inadvertently forget to decorate a field with a [DataMember] attribute. This wouldn't affect your unit test if you're just testing your library code but could break your service if a client is expecting that field to be serialized.&lt;/i&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;Use an IoC (&lt;i&gt;Inversion of Control&lt;/i&gt;) container to inject your dependencies from within your tests
    &lt;ul&gt;
        &lt;li&gt;&lt;i&gt;This is the most accurate way to effectively unit test your service. Granted, there is a bit more setup involved up front, however this approach is defintely worth the effort once you wrap your head around the concept of WCF extension points&lt;/i&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We are going to be focusing on option #2 since we want our tests to be as realistic and thorough as possible.&lt;br /&gt;
&lt;br /&gt;
To help us get started, let's ask ourselves the following questions:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Why do we inject a dependency into our service?&lt;/li&gt;
    &lt;li&gt;What type of dependency do we inject into our service?&lt;/li&gt;
    &lt;li&gt;How do we actually inject a dependency into our service?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;NOTE:&lt;/b&gt; &lt;i&gt;For our examples we will be utilizing Microsoft's Unity application block, however you can just as easily use any IoC container that you are comfortable with such as StructureMap, etc. The actual IoC code is found within our WCF extension points, which you will see described later on.&lt;/i&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Why do we inject a dependency into our service?&lt;/b&gt;&lt;br /&gt;
Dependency injection allows one object's configuration (fields, properties, etc) to be set and/or modified by another external object. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What type of dependency do we inject into our service?&lt;/b&gt;&lt;br /&gt;
For our purpose, we will be sending in a depency to allow the service to read from a "mock" datastore (XML file) to return a collection of users rather than from the service's default SQL datastore. This way, we will know exactly what the data will look like going into and coming out of the service. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How do we actually inject a dependency into our service?&lt;/b&gt;&lt;br /&gt;
We will employ dependency injection by passing a data provider interface to the service contructor. &lt;i&gt;How does the service know what concrete implementation of the data provider to use?&lt;/i&gt; That's where the IoC container, Unity in our case, comes into play. &lt;br /&gt;
&lt;br /&gt;
Here is a checklist of the tasks we need to employ to allow the service to accept the DI (dependency injection).&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Create an interface for our data provider&lt;/li&gt;
    &lt;li&gt;Modify our service to accept the interface in the contructor&lt;/li&gt;
    &lt;li&gt;Create our WCF extension points to allow our IoC container to instatiate a concrete implementation of our data provider
    &lt;ul&gt;
        &lt;li&gt;Create our own instance provider that inherits from &lt;font class="inlinecode"&gt;IInstanceProvider&lt;/font&gt;&lt;/li&gt;
        &lt;li&gt;Create a custom service behavior that inherits from &lt;font class="inlinecode"&gt;IServiceBehavior&lt;/font&gt; which will allow us to configure the service to use our new instance provider rather than the default WCF instance provider&lt;/li&gt;
        &lt;li&gt;We then extend the &lt;font class="inlinecode"&gt;ServiceHost&lt;/font&gt; class and override the OnOpening() method to add our new service behavior to the ServiceDescription Behaviors collection&lt;/li&gt;
        &lt;li&gt;Finally we need to extend the &lt;font class="inlinecode"&gt;ServiceHostFactory&lt;/font&gt; class and override the CreateServiceHost method to use our new service host&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;Now that our extension points are all setup, we need to configure the service to use the new service host factory&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;b&gt;1.) Create an interface for our data provider&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
This interface contains one method, AllUserData(), that returns a collection of Users. &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; &lt;span class="cus"&gt;IRepository&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="cus"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="cus"&gt;User&lt;/span&gt;&amp;gt; AllUserData();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;b&gt;2. Modify our service to accept the interface in the contructor&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
Now we have to modify our service so that we can pass in a rerence to &lt;font class="inlinecode"&gt;IRepository&lt;/font&gt; in the constructor. Now when the GetUsers() method is called, the concrete implementation of &lt;font class="inlinecode"&gt;IRepository&lt;/font&gt; will return it's version of the AllUserData() method. &lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; &lt;span class="cus"&gt;UserService&lt;/span&gt; : &lt;span class="cus"&gt;IUserService&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; &lt;span class="cus"&gt;IRepository&lt;/span&gt; userrepository;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; UserService(&lt;span class="cus"&gt;IRepository&lt;/span&gt; userrepository)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.userrepository = userrepository;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="cus"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="cus"&gt;User&lt;/span&gt;&amp;gt; GetUsers()&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; userrepository.AllUserData();&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
Are we done? Not quite...&lt;br /&gt;
&lt;br /&gt;
Trying to run this "as-is" will generate an error since WCF can only instantiate our service using a constructor with no arguments. That is why we need to create our own extension points so that we can control the instatiation of the service without having to create a no args constructor. The beauty in this is that when a client uses the service, they only see the default "no args" constructor, however when we instantiate the service through our unit tests, we will be able to see the second constructor so that we can pass in our own implementation of &lt;font class="inlinecode"&gt;IRepository&lt;/font&gt; to "fake out" the service. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;3a. Create our instance provider&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
This is the class where we are setting up our IoC container to resolve the &lt;font class="inlinecode"&gt;IRepository&lt;/font&gt; interface to the correct concrete implemenation, which you'll see described later when we create the service host factory class. Notice line 19 below, this is where Unity is resolving the passed in serviceType. &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; &lt;span class="cus"&gt;WcfSvcInstanceProvider&lt;/span&gt; : &lt;span class="cus"&gt;IInstanceProvider&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; &lt;span class="cus"&gt;IUnityContainer&lt;/span&gt; container;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Type _serviceType;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; WcfSvcInstanceProvider(&lt;span class="cus"&gt;IUnityContainer&lt;/span&gt; container, Type serviceType)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.container = container;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        _serviceType = serviceType;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; GetInstance(&lt;span class="cus"&gt;InstanceContext&lt;/span&gt; instanceContext)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; GetInstance(instanceContext, &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; GetInstance(&lt;span class="cus"&gt;InstanceContext&lt;/span&gt; instanceContext, &lt;span class="cus"&gt;Message&lt;/span&gt; message)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; container.Resolve(_serviceType);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ReleaseInstance(&lt;span class="cus"&gt;InstanceContext&lt;/span&gt; instanceContext, &lt;span class="kwrd"&gt;object&lt;/span&gt; instance)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;        container.Teardown(instance);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;b&gt;3b. Create our service behavior&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
Once we have setup our instance provider, we need to incorporate that into a custom service behavior. Starting at line 12 you can see how we search for all endpoints in all channels and set their respective instance providers to our newly created provider. &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; &lt;span class="cus"&gt;WcfSvcServiceBehavior&lt;/span&gt; : &lt;span class="cus"&gt;IServiceBehavior&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; &lt;span class="cus"&gt;IUnityContainer&lt;/span&gt; container;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; WcfSvcServiceBehavior(&lt;span class="cus"&gt;IUnityContainer&lt;/span&gt; container)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.container = container;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ApplyDispatchBehavior(&lt;span class="cus"&gt;ServiceDescription&lt;/span&gt; serviceDescription, &lt;span class="cus"&gt;ServiceHostBase&lt;/span&gt; serviceHostBase)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="cus"&gt;ChannelDispatcherBase&lt;/span&gt; cdb &lt;span class="kwrd"&gt;in&lt;/span&gt; serviceHostBase.ChannelDispatchers)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            &lt;span class="cus"&gt;ChannelDispatcher&lt;/span&gt; cd = cdb &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="cus"&gt;ChannelDispatcher&lt;/span&gt;;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (cd != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;                &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="cus"&gt;EndpointDispatcher&lt;/span&gt; ed &lt;span class="kwrd"&gt;in&lt;/span&gt; cd.Endpoints)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;                {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;                    ed.DispatchRuntime.InstanceProvider = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="cus"&gt;WcfSvcInstanceProvider&lt;/span&gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt;.container, serviceDescription.ServiceType);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;                }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;            }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddBindingParameters(&lt;span class="cus"&gt;ServiceDescription&lt;/span&gt; serviceDescription, &lt;span class="cus"&gt;ServiceHostBase&lt;/span&gt; serviceHostBase, &lt;span class="cus"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span class="cus"&gt;ServiceEndpoint&lt;/span&gt;&amp;gt; endpoints, &lt;span class="cus"&gt;BindingParameterCollection&lt;/span&gt; bindingParameters)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Validate(&lt;span class="cus"&gt;ServiceDescription&lt;/span&gt; serviceDescription, &lt;span class="cus"&gt;ServiceHostBase&lt;/span&gt; serviceHostBase)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;b&gt;3c. Create our service host&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
Here we are creating our own service host and overriding the OnOpening() method to add our custom service behavior to the ServiceDescription behaviors collection. &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; &lt;span class="cus"&gt;WcfSvcServiceHost&lt;/span&gt; : &lt;span class="cus"&gt;ServiceHost&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="cus"&gt;IUnityContainer&lt;/span&gt; unityContainer;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; WcfSvcServiceHost(&lt;span class="cus"&gt;IUnityContainer&lt;/span&gt; unityContainer, &lt;span class="cus"&gt;Type&lt;/span&gt; serviceType, &lt;span class="kwrd"&gt;params&lt;/span&gt; &lt;span class="cus"&gt;Uri[]&lt;/span&gt; baseAddresses)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(serviceType, baseAddresses)&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.unityContainer = unityContainer;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnOpening()&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        &lt;span class="kwrd"&gt;base&lt;/span&gt;.OnOpening();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;this&lt;/span&gt;.Description.Behaviors.Find&amp;lt;&lt;span class="cus"&gt;WcfSvcServiceBehavior&lt;/span&gt;&amp;gt;() == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.Description.Behaviors.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="cus"&gt;WcfSvcServiceBehavior&lt;/span&gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt;.unityContainer));&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;b&gt;3d. Create our service host factory&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
Now we create our service host factory to instantiate our service host. This is also where we are defining our concrete implementation of the &lt;font class="inlinecode"&gt;IRepository&lt;/font&gt; interface to be of type &lt;font class="inlinecode"&gt;SqlRepository&lt;/font&gt;. This concrete type will always be used when the service is instantiated with the no-args constructor. &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; &lt;span class="cus"&gt;WcfSvcServiceHostFactory&lt;/span&gt; : &lt;span class="cus"&gt;ServiceHostFactory&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="cus"&gt;ServiceHost&lt;/span&gt; CreateServiceHost(&lt;span class="cus"&gt;Type&lt;/span&gt; serviceType, &lt;span class="cus"&gt;Uri[]&lt;/span&gt; baseAddresses)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        &lt;span class="cus"&gt;UnityContainer&lt;/span&gt; unityContainer = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="cus"&gt;UnityContainer&lt;/span&gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        unityContainer.RegisterType&amp;lt;&lt;span class="cus"&gt;IRepository&lt;/span&gt;, &lt;span class="cus"&gt;SqlRepository&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="cus"&gt;WcfSvcServiceHost&lt;/span&gt;(unityContainer, serviceType, baseAddresses);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;b&gt;4. Configure the service to use our service host factory&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
Since we are hosting the service in IIS, we simply need to add one line of code to the .svc file to use our service host factory &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="asp"&gt;&amp;lt;%@&lt;/span&gt; &lt;span class="preproc"&gt;ServiceHost&lt;/span&gt; &lt;span class="attr"&gt;Language&lt;/span&gt;=&lt;span class="str"&gt;"C#"&lt;/span&gt;
        &lt;span class="attr"&gt;Service&lt;/span&gt;=&lt;span class="str"&gt;"UserService"&lt;/span&gt;
        &lt;span class="attr"&gt;CodeBehind&lt;/span&gt;=&lt;span class="str"&gt;"UserService.svc.cs"&lt;/span&gt; 
        &lt;span class="attr"&gt;Factory&lt;/span&gt;=&lt;span class="str"&gt;"WcfSvcServiceHostFactory"&lt;/span&gt;
&lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;You should now be able to successfully run your "unit test ready" WCF service. &lt;br /&gt;
&lt;br /&gt;
Now you can just include your service in your unit test project and create your tests like this: &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="cus"&gt;[TestClass]&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; UserServiceTests&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="cus"&gt;UnityContainer&lt;/span&gt; unityContainer = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="cus"&gt;UnityContainer&lt;/span&gt;();&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; UserServiceTests()&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        unityContainer.RegisterType&amp;lt;&lt;span class="cus"&gt;IRepository&lt;/span&gt;, &lt;span class="cus"&gt;TestRepository&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    &lt;span class="cus"&gt;[TestMethod]&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; GetMockedUsers()&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        &lt;span class="cus"&gt;UserService&lt;/span&gt; usr_svc = new &lt;span class="cus"&gt;UserService&lt;/span&gt;(unityContainer.Resolve&amp;lt;&lt;span class="cus"&gt;IRepository&lt;/span&gt;&amp;gt;());&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        &lt;span class="cus"&gt;List&lt;/span&gt;&amp;lt;&lt;span class="cus"&gt;User&lt;/span&gt;&amp;gt; testuserdata = usr_svc.GetUsers();&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;        &lt;span class="cus"&gt;Assert&lt;/span&gt;.IsTrue(MockDatafromXmlFile(), testuserdata));&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
First we are telling Unity to register the &lt;font class="inlinecode"&gt;TestRepository&lt;/font&gt; class as the concrete implementation of the &lt;font class="inlinecode"&gt;IRepository&lt;/font&gt; interface in the test class constructor. &lt;font class="inlinecode"&gt;TestRepository&lt;/font&gt; is simply our class that returns data from an XML file that contains a list of users. &lt;br /&gt;
&lt;br /&gt;
Then in our test we simply pass in the the &lt;font class="inlinecode"&gt;IRepository&lt;/font&gt; interface to the service constructor and test away!&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
Related post: &lt;a href="http://geekswithblogs.net/ballhaus/archive/2009/12/21/integration-testing-wcf-services-with-unity.aspx"&gt;Integration testing your WCF service with Unity&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
 &lt;/p&gt;&lt;img src="http://geekswithblogs.net/ballhaus/aggbug/136948.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ballhaus/comments/136948.aspx</wfw:comment>
        <slash:comments>1</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ballhaus/comments/commentRss/136948.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ballhaus/services/trackbacks/136948.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Publisher/Subscriber... practical benefits from event-driven patterns</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ballhaus/archive/2009/05/22/publishersubscriber.-practical-benefits-from-event-driven-patterns.aspx" />
        <id>http://geekswithblogs.net/ballhaus/archive/2009/05/22/publishersubscriber.-practical-benefits-from-event-driven-patterns.aspx</id>
        <published>2009-05-22T23:00:41-04:00:00</published>
        <updated>2009-12-14T09:21:02Z</updated>
        <summary type="html">Events are a critical component in Object Oriented programming. Whether they originate from user interaction or code logic, events are at the heart of what makes your application dynamic.</summary>
        <content type="html">&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;Events are a critical component in Object Oriented programming. Whether they originate from user interaction or code logic, events are at the heart of what makes your application dynamic. Thanks to the advancement in both Java and .Net over the years, the event-driven paradigm has become possible on the web with the advent of JSF (&lt;em&gt;Java Server Faces&lt;/em&gt;) and ASP.NET. Both of these technologies support the event-driven model allowing developers to take adantage of the "publisher/subscriber" pattern.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;There are generally two types of events in Java and .NET; &lt;em&gt;low-level&lt;/em&gt; events and &lt;em&gt;semantic&lt;/em&gt; events.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;&lt;u&gt;Low-level&lt;/u&gt; events typically involve OS (GUI) type events such as Mouseclicks (onMouseClick, onMouseDown, etc) or keyboard actions (onKeyPress, onKeyDown, etc) whereas &lt;u&gt;Semantic&lt;/u&gt; events are programmatic and involve the developer to create their own events, subsequently creating listeners to take custom actions based on those events. This article is geared toward utilizing semantic events.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;&lt;em&gt;What's the benefit of coding for semantic events&lt;/em&gt;?&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;That's a good question... let's take a typical application scenario and see how we can use events to develop a "real-world" solution.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;Let's suppose we have an website that allows new users to register themselves (&lt;em&gt;the event publisher&lt;/em&gt;). There are certain actions that need to happen when a new user has successfully completed the registration form (&lt;em&gt;the event subscribers&lt;/em&gt;):&lt;/font&gt;&lt;/p&gt;
&lt;div&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;font face="Tahoma"&gt;The database needs to be updated with the user's information. &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font face="Tahoma"&gt;The website owner needs to be notified that a user has registered and will need to approve the user before they can actually use the site. &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font face="Tahoma"&gt;The new user will need to be sent a "&lt;em&gt;welcome to our site&lt;/em&gt;" type message. &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font face="Tahoma"&gt;The site will need to set a cookie in the user's browser so they will be "recognized" the next time they visit the site. &lt;/font&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;There are several ways to architect a suitable coding solution to meet these requirements, however by utilizing a publisher/subscriber design pattern we can create a scalable solution that will be flexible enough to handle any new actions that may be required at a later time. This is accomplished by firing an event when the user has registered. This will send out a notification to all subscribers of the "new user registration" event by way of a "callback" method. Each callback method would then be executed to perform their own specific tasks related to the new user registration. With this design in place, it becomes very easy to add new actions that may be required in the future by simply registering new callback methods to the "new user registration" event.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;&lt;strong&gt;Java + .Net Demo&lt;/strong&gt; &lt;br /&gt;
&lt;br /&gt;
Let's walk through a simple "Team Roster" demo to see the publisher/subscriber pattern in action. Here are the requirements for our application:&lt;/font&gt;&lt;/p&gt;
&lt;div&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;font face="Tahoma"&gt;The application must be able to add new players to a team roster. &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font face="Tahoma"&gt;When a new player is added, we need to notify the coaching staff and the media &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;font face="Tahoma"&gt;The application also must allow for new entities to be able to subscribe to new player notifications. &lt;/font&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;When reviewing the Java and C# code below you will notice that the code is very similar (&lt;em&gt;same total lines of code in fact&lt;/em&gt;). One large difference is in the way the callback methods are handled. Java uses a single interface that all subscribing classes implement whereas .Net utilizes "Delegates." &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;NOTE: &lt;em&gt;Delegates are similar to a "type-safe" function pointer in C/C++.&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;The "Team" class is considered the publisher in our demo. It contains the logic to actually "add" the player to the roster and then raises the event to notify the subscribers. The two listener classes (CoachListener and MediaListener) are then added to the event notfications. The "AddPlayerEvent" object is the actual class that is passed to the callback methods (&lt;em&gt;with the new playername value&lt;/em&gt;).&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;font face="Tahoma"&gt;C#:&lt;/font&gt; &lt;br /&gt;
&lt;/strong&gt;&lt;br /&gt;
 &lt;/p&gt;
&lt;!-- 
     C# code example
--&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; TeamRoster{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; AddPlayerEventArgs : EventArgs{ &lt;span class="rem"&gt;//create Event object&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; PlayerName { get; set; }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; CoachListener{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        &lt;span class="rem"&gt;// subscribe callback method "NotifyCoachingStaff" to AddPlayerEvent (event) &lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        &lt;span class="rem"&gt;// using delegate "AddPlayerHandler" &lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Subscribe(Team team){&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            team.AddPlayerEvent += &lt;span class="kwrd"&gt;new&lt;/span&gt; Team.AddPlayerHandler(NotifyCoachingStaff);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        &lt;span class="rem"&gt;// create "Coach" Listener callback method&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; NotifyCoachingStaff(Team team, AddPlayerEventArgs e){&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            System.Console.WriteLine(&lt;span class="str"&gt;"To Coaching Staff: ({0}) was added to the team"&lt;/span&gt;, e.PlayerName);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; MediaListener{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        &lt;span class="rem"&gt;// subscribe callback method "NotifyMedia" to AddPlayerEvent (event) &lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;        &lt;span class="rem"&gt;// using delegate "AddPlayerHandler" &lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Subscribe(Team team){&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;            team.AddPlayerEvent += &lt;span class="kwrd"&gt;new&lt;/span&gt; Team.AddPlayerHandler(NotifyMedia);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;        &lt;span class="rem"&gt;// create "Media" Listener callback method&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; NotifyMedia(Team team, AddPlayerEventArgs e){&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;            System.Console.WriteLine(&lt;span class="str"&gt;"To Media Outlet: ({0}) was added to the team"&lt;/span&gt;, e.PlayerName);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;    &lt;span class="rem"&gt;//publisher&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; Team{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;        &lt;span class="rem"&gt;// delegate will hold a type-safe "pointer" to the callback &lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;        &lt;span class="rem"&gt;// "Listener" callback methods&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddPlayerHandler(Team team, AddPlayerEventArgs e);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; AddPlayerHandler AddPlayerEvent; &lt;span class="rem"&gt;// event that callback methods will subscribe to&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddPlayer(&lt;span class="kwrd"&gt;string&lt;/span&gt; playername){ &lt;span class="rem"&gt;// method to add new player to roster&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (AddPlayerEvent != &lt;span class="kwrd"&gt;null&lt;/span&gt;){&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;                AddPlayerEventArgs e = &lt;span class="kwrd"&gt;new&lt;/span&gt; AddPlayerEventArgs();&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;                e.PlayerName = playername;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;                System.Console.WriteLine(&lt;span class="str"&gt;"\nNew player added to the Roster, notifying all subscribers..."&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;                AddPlayerEvent(&lt;span class="kwrd"&gt;this&lt;/span&gt;, e); &lt;span class="rem"&gt;// call method to notify subscribers of new player&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  44:  &lt;/span&gt;            }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  45:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  46:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  47:  &lt;/span&gt;    &lt;span class="rem"&gt;//main class&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  48:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; AddTeamPlayer{&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  49:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(){&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  50:  &lt;/span&gt;            &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  51:  &lt;/span&gt;            Team team = &lt;span class="kwrd"&gt;new&lt;/span&gt; Team();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  52:  &lt;/span&gt;            CoachListener coachlistener = &lt;span class="kwrd"&gt;new&lt;/span&gt; CoachListener();&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  53:  &lt;/span&gt;            MediaListener medialistener = &lt;span class="kwrd"&gt;new&lt;/span&gt; MediaListener();&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  54:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  55:  &lt;/span&gt;            coachlistener.Subscribe(team); &lt;span class="rem"&gt;// subscribe to event&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  56:  &lt;/span&gt;            medialistener.Subscribe(team); &lt;span class="rem"&gt;// subscribe to event&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  57:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  58:  &lt;/span&gt;            Console.Write(&lt;span class="str"&gt;"-Team Roster-\n"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  59:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  60:  &lt;/span&gt;            team.AddPlayer(&lt;span class="str"&gt;"Memori"&lt;/span&gt;); &lt;span class="rem"&gt;// add player to roster&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  61:  &lt;/span&gt;            team.AddPlayer(&lt;span class="str"&gt;"Elias"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  62:  &lt;/span&gt;            team.AddPlayer(&lt;span class="str"&gt;"Mikayli"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  63:  &lt;/span&gt;            team.AddPlayer(&lt;span class="str"&gt;"Myranda"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  64:  &lt;/span&gt;            team.AddPlayer(&lt;span class="str"&gt;"Elo"&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  65:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  66:  &lt;/span&gt;            Console.ReadLine();&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  67:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  68:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  69:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;strong&gt;Java:&lt;/strong&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;!--
     Java code example
--&gt;&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;a name="line1"&gt; 1&lt;/a&gt; &lt;font color="#0000ff"&gt;&lt;strong&gt;package TeamRoster;&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line2"&gt; 2&lt;/a&gt; 
&lt;a name="line3"&gt; 3&lt;/a&gt; &lt;font color="#0000ff"&gt;&lt;strong&gt;import java.util.*;&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line4"&gt; 4&lt;/a&gt; 
&lt;a name="line5"&gt; 5&lt;/a&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;AddPlayerEvent&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;EventObject&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;font color="#444444"&gt;// Create event object&lt;/font&gt;
&lt;a name="line6"&gt; 6&lt;/a&gt; 	&lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;playername&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line7"&gt; 7&lt;/a&gt; 	&lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;getPlayerName&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;font color="#444444"&gt;// getter method to return playername&lt;/font&gt;
&lt;a name="line8"&gt; 8&lt;/a&gt; 		&lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;playername&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line9"&gt; 9&lt;/a&gt; 	&lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line10"&gt;10&lt;/a&gt; 	&lt;font color="#444444"&gt;// event constructor to set instance property&lt;/font&gt;
&lt;a name="line11"&gt;11&lt;/a&gt; 	&lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;AddPlayerEvent&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="#2040a0"&gt;source&lt;/font&gt;, &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;playername&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line12"&gt;12&lt;/a&gt; 		&lt;strong&gt;super&lt;/strong&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;source&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line13"&gt;13&lt;/a&gt; 		&lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;playername&lt;/font&gt; &lt;font color="#4444ff"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;playername&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line14"&gt;14&lt;/a&gt; 	&lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line15"&gt;15&lt;/a&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line16"&gt;16&lt;/a&gt; &lt;font color="#444444"&gt;// All listener classes will implement this interface&lt;/font&gt;
&lt;a name="line17"&gt;17&lt;/a&gt; &lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;PlayerListener&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line18"&gt;18&lt;/a&gt; 	&lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;addPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;AddPlayerEvent&lt;/font&gt; &lt;font color="#2040a0"&gt;addplayerevent&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line19"&gt;19&lt;/a&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line20"&gt;20&lt;/a&gt; &lt;font color="#444444"&gt;// publisher&lt;/font&gt;
&lt;a name="line21"&gt;21&lt;/a&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Team&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line22"&gt;22&lt;/a&gt; 	&lt;font color="#444444"&gt;// method to add new player to roster&lt;/font&gt;
&lt;a name="line23"&gt;23&lt;/a&gt; 	&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;AddPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;playername&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line24"&gt;24&lt;/a&gt; 		&lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;"&lt;font color="#77dd77"&gt;\n&lt;/font&gt;New player added to the Roster, notify all subscribers..."&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line25"&gt;25&lt;/a&gt; 		&lt;font color="#444444"&gt;// call method to notify subscribers of new player&lt;/font&gt;
&lt;a name="line26"&gt;26&lt;/a&gt; 		&lt;font color="#2040a0"&gt;OnAddPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;playername&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line27"&gt;27&lt;/a&gt; 	&lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line28"&gt;28&lt;/a&gt; 	&lt;font color="#444444"&gt;// method to notify all subscribers when a new player has been added&lt;/font&gt;
&lt;a name="line29"&gt;29&lt;/a&gt; 	&lt;font color="#444444"&gt;// to the roster&lt;/font&gt;
&lt;a name="line30"&gt;30&lt;/a&gt; 	&lt;strong&gt;private&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;OnAddPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;playername&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line31"&gt;31&lt;/a&gt; 		&lt;strong&gt;for&lt;/strong&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;i&lt;/font&gt; &lt;font color="#4444ff"&gt;=&lt;/font&gt; &lt;font color="#ff0000"&gt;0&lt;/font&gt;, &lt;font color="#2040a0"&gt;size&lt;/font&gt; &lt;font color="#4444ff"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;subscribers&lt;/font&gt;.&lt;font color="#2040a0"&gt;size&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt; &lt;font color="#2040a0"&gt;i&lt;/font&gt; &lt;font color="#4444ff"&gt;&amp;lt;&lt;/font&gt; &lt;font color="#2040a0"&gt;size&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt; &lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="#4444ff"&gt;+&lt;/font&gt;&lt;font color="#4444ff"&gt;+&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line32"&gt;32&lt;/a&gt; 			&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;PlayerListener&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;subscribers&lt;/font&gt;.&lt;font color="#2040a0"&gt;get&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;addPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;AddPlayerEvent&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;this&lt;/strong&gt;, &lt;font color="#2040a0"&gt;playername&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line33"&gt;33&lt;/a&gt; 	&lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line34"&gt;34&lt;/a&gt; 	&lt;font color="#444444"&gt;// type-safe ArrayList container to hold subscribers to the event&lt;/font&gt;
&lt;a name="line35"&gt;35&lt;/a&gt; 	&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="#4444ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;PlayerListener&lt;/font&gt;&lt;font color="#4444ff"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;subscribers&lt;/font&gt; &lt;font color="#4444ff"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ArrayList&lt;/font&gt;&lt;font color="#4444ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;PlayerListener&lt;/font&gt;&lt;font color="#4444ff"&gt;&amp;gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line36"&gt;36&lt;/a&gt; 	&lt;font color="#444444"&gt;// method to add new subscribers to the ArrayList&lt;/font&gt;
&lt;a name="line37"&gt;37&lt;/a&gt; 	&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;addPlayerListener&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;PlayerListener&lt;/font&gt; &lt;font color="#2040a0"&gt;plistener&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line38"&gt;38&lt;/a&gt; 		&lt;font color="#2040a0"&gt;subscribers&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;plistener&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line39"&gt;39&lt;/a&gt; 	&lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line40"&gt;40&lt;/a&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line41"&gt;41&lt;/a&gt; &lt;font color="#444444"&gt;// main class&lt;/font&gt;
&lt;a name="line42"&gt;42&lt;/a&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;AddTeamPlayer&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line43"&gt;43&lt;/a&gt; 	&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;main&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;args&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line44"&gt;44&lt;/a&gt; 		&lt;font color="#2040a0"&gt;Team&lt;/font&gt; &lt;font color="#2040a0"&gt;team&lt;/font&gt; &lt;font color="#4444ff"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Team&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line45"&gt;45&lt;/a&gt; 		&lt;font color="#444444"&gt;// create "Coach" PlayerListener callback class/method (Anonymous class)&lt;/font&gt;
&lt;a name="line46"&gt;46&lt;/a&gt; 		&lt;font color="#444444"&gt;// approach&lt;/font&gt;
&lt;a name="line47"&gt;47&lt;/a&gt; 		&lt;font color="#2040a0"&gt;team&lt;/font&gt;.&lt;font color="#2040a0"&gt;addPlayerListener&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;PlayerListener&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line48"&gt;48&lt;/a&gt; 			&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;addPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;AddPlayerEvent&lt;/font&gt; &lt;font color="#2040a0"&gt;e&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line49"&gt;49&lt;/a&gt; 				&lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;"To Coaching Staff: ("&lt;/font&gt; &lt;font color="#4444ff"&gt;+&lt;/font&gt; &lt;font color="#2040a0"&gt;e&lt;/font&gt;.&lt;font color="#2040a0"&gt;getPlayerName&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;+&lt;/font&gt; &lt;font color="#008000"&gt;") was added to the team"&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line50"&gt;50&lt;/a&gt; 			&lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line51"&gt;51&lt;/a&gt; 		&lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line52"&gt;52&lt;/a&gt; 		&lt;font color="#444444"&gt;// create "Media" PlayerListener callback class/method (Inner class)&lt;/font&gt;
&lt;a name="line53"&gt;53&lt;/a&gt; 		&lt;font color="#444444"&gt;// approach&lt;/font&gt;
&lt;a name="line54"&gt;54&lt;/a&gt; 		&lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;MediaListener&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;PlayerListener&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line55"&gt;55&lt;/a&gt; 			&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;addPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;AddPlayerEvent&lt;/font&gt; &lt;font color="#2040a0"&gt;e&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line56"&gt;56&lt;/a&gt; 				&lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;"To Media Outlet: ("&lt;/font&gt; &lt;font color="#4444ff"&gt;+&lt;/font&gt; &lt;font color="#2040a0"&gt;e&lt;/font&gt;.&lt;font color="#2040a0"&gt;getPlayerName&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#4444ff"&gt;+&lt;/font&gt; &lt;font color="#008000"&gt;") was added to the team"&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line57"&gt;57&lt;/a&gt; 			&lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line58"&gt;58&lt;/a&gt; 		&lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line59"&gt;59&lt;/a&gt; 		&lt;font color="#2040a0"&gt;team&lt;/font&gt;.&lt;font color="#2040a0"&gt;addPlayerListener&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;MediaListener&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line60"&gt;60&lt;/a&gt; 
&lt;a name="line61"&gt;61&lt;/a&gt; 		&lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;print&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;"-Team Roster-&lt;font color="#77dd77"&gt;\n&lt;/font&gt;"&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line62"&gt;62&lt;/a&gt; 
&lt;a name="line63"&gt;63&lt;/a&gt; 		&lt;font color="#2040a0"&gt;team&lt;/font&gt;.&lt;font color="#2040a0"&gt;AddPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;"Memori"&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// add player to roster&lt;/font&gt;
&lt;a name="line64"&gt;64&lt;/a&gt; 		&lt;font color="#2040a0"&gt;team&lt;/font&gt;.&lt;font color="#2040a0"&gt;AddPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;"Elias"&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line65"&gt;65&lt;/a&gt; 		&lt;font color="#2040a0"&gt;team&lt;/font&gt;.&lt;font color="#2040a0"&gt;AddPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;"Mikayli"&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line66"&gt;66&lt;/a&gt; 		&lt;font color="#2040a0"&gt;team&lt;/font&gt;.&lt;font color="#2040a0"&gt;AddPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;"Myranda"&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line67"&gt;67&lt;/a&gt; 		&lt;font color="#2040a0"&gt;team&lt;/font&gt;.&lt;font color="#2040a0"&gt;AddPlayer&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;"Elo"&lt;/font&gt;&lt;font color="#4444ff"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#4444ff"&gt;;&lt;/font&gt;
&lt;a name="line68"&gt;68&lt;/a&gt; 	&lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;
&lt;a name="line69"&gt;69&lt;/a&gt; &lt;font color="#4444ff"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;

&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;Here is the Console output from the .Net code:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img height="335" alt="" width="556" src="/images/geekswithblogs_net/ballhaus/5_20_2009/eventsconsole_net.gif" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Tahoma"&gt;Here is the Console output from the Java code:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img height="456" alt="" width="521" src="/images/geekswithblogs_net/ballhaus/5_20_2009/eventsconsole_java.gif" /&gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;/p&gt;&lt;img src="http://geekswithblogs.net/ballhaus/aggbug/132382.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ballhaus/comments/132382.aspx</wfw:comment>
        <slash:comments>2</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ballhaus/comments/commentRss/132382.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ballhaus/services/trackbacks/132382.aspx</trackback:ping>
    </entry>
</feed>
