<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Janne Mattila's blog : .NET General</title><link>http://blogs.msdn.com/jannemattila/archive/tags/.NET+General/default.aspx</link><description>Tags: .NET General</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Web Services and namespaces (or WCF?)</title><link>http://blogs.msdn.com/jannemattila/archive/2008/10/15/web-services-and-namespaces-or-wcf.aspx</link><pubDate>Wed, 15 Oct 2008 10:27:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9000423</guid><dc:creator>jannemattila</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9000423.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9000423</wfw:commentRss><description>&lt;p&gt;You might have encountered following situation:    &lt;br /&gt;1. You have created class library “MyLibrary” and it contains following class:     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;   &lt;table cellspacing="10"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" align="right"&gt;           &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;namespace&lt;/font&gt; MyLibrary
{
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;class&lt;/font&gt; &lt;font color="#2b91af"&gt;Employee
&lt;/font&gt;  {
    &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;string&lt;/font&gt; FirstName;
    &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;string&lt;/font&gt; LastName;
  }
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;2. You have created Web Service “MyWeb” using following VS template: 
  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/Web%20Service%20project_2.png"&gt;&lt;img title="Web Service project" border="0" alt="Web Service project" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/Web%20Service%20project_thumb.png" width="128" height="20" /&gt;&lt;/a&gt; 

  &lt;br /&gt;&amp;#160; - It references “MyLibrary” 

  &lt;br /&gt;&amp;#160; - It contains following method: &lt;/p&gt;

&lt;p&gt;
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;class&lt;/font&gt; &lt;font color="#2b91af"&gt;MyWeb&lt;/font&gt; : System.Web.Services.&lt;font color="#2b91af"&gt;WebService
&lt;/font&gt;{
  [&lt;font color="#2b91af"&gt;WebMethod&lt;/font&gt;]
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; AddNewEmployee(MyLibrary.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee)
  {
    &lt;font color="#008000"&gt;// TODO: implement
&lt;/font&gt;  }
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;3. Finally you create Windows Forms application “My Win App”: 
  &lt;br /&gt;&amp;#160; - You “Add Web Reference” to “MyWeb” and you name it “WebServices” 

  &lt;br /&gt;&amp;#160; - You write following code to use that web service: &lt;/p&gt;

&lt;p&gt;
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;WebServices.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee = &lt;font color="#0000ff"&gt;new&lt;/font&gt; WebServices.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt;();
employee.FirstName = &lt;font color="#a31515"&gt;&amp;quot;John&amp;quot;&lt;/font&gt;;
employee.LastName = &lt;font color="#a31515"&gt;&amp;quot;Doe&amp;quot;&lt;/font&gt;;
WebServices.&lt;font color="#2b91af"&gt;MyWeb&lt;/font&gt; myWeb = &lt;font color="#0000ff"&gt;new&lt;/font&gt; WebServices.&lt;font color="#2b91af"&gt;MyWeb&lt;/font&gt;();
myWeb.AddNewEmployee(employee);&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;4. You run your application and all is fine. 
  &lt;br /&gt;5. Later you notice that you need to add reference to your “MyLibrary” into your “My Win App” 

  &lt;br /&gt;(There could be many reasons for this. For example you want to use same business logic that web service uses etc.) 

  &lt;br /&gt;6. You add following code to you “My Win App”: &lt;/p&gt;

&lt;p&gt;
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;MyLibrary.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee2 = &lt;font color="#0000ff"&gt;new&lt;/font&gt; MyLibrary.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt;();
MyLibrary.&lt;font color="#2b91af"&gt;EmployeeManager&lt;/font&gt; employeeManager = &lt;font color="#0000ff"&gt;new&lt;/font&gt; MyLibrary.&lt;font color="#2b91af"&gt;EmployeeManager&lt;/font&gt;();
employeeManager.AddNewEmployee(employee2);&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;7. Code works fine but then you notice that you actually have the same class from two different namespaces. What if you try to mix and match them (and sometimes you just have to do that)? Let’s see what happens:&lt;/p&gt;

&lt;p&gt;
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;WebServices.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee = &lt;font color="#0000ff"&gt;new&lt;/font&gt; WebServices.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt;();
MyLibrary.&lt;font color="#2b91af"&gt;EmployeeManager&lt;/font&gt; employeeManager = &lt;font color="#0000ff"&gt;new&lt;/font&gt; MyLibrary.&lt;font color="#2b91af"&gt;EmployeeManager&lt;/font&gt;();
employeeManager.AddNewEmployee(employee);&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;8. When you try to compile your application and you’ll receive following error: 
  &lt;br /&gt;&lt;em&gt;&lt;img title="VS compile error" border="0" alt="VS compile error" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/VS%20compile%20error_3.png" width="703" height="62" /&gt;&amp;#160; Error&amp;#160;&amp;#160;&amp;#160; 11&amp;#160;&amp;#160;&amp;#160; The best overloaded method match for 'MyLibrary.EmployeeManager.AddNewEmployee(MyLibrary.Employee)' has some invalid arguments&amp;#160;&amp;#160;&amp;#160; C:\&amp;lt;path&amp;gt;\MyWinApp\MainForm.cs&amp;#160;&amp;#160;&amp;#160; 41&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160; MyWinApp 

    &lt;br /&gt;Error&amp;#160;&amp;#160;&amp;#160; 12&amp;#160;&amp;#160;&amp;#160; Argument '1': cannot convert from 'MyWinApp.WebServices.Employee' to 'MyLibrary.Employee'&amp;#160;&amp;#160;&amp;#160; C:\&amp;lt;path&amp;gt;\MyWinApp\MainForm.cs&amp;#160;&amp;#160;&amp;#160; 41&amp;#160;&amp;#160;&amp;#160; 32&amp;#160;&amp;#160;&amp;#160; MyWinApp 

    &lt;br /&gt;&lt;/em&gt;

  &lt;br /&gt;9. You open up the generated proxy code: 

  &lt;br /&gt;&lt;img title="Reference" border="0" alt="Reference" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/Reference_3.png" width="172" height="97" /&gt; 

  &lt;br /&gt;

  &lt;br /&gt;10. You locate the code where &lt;em&gt;Employee&lt;/em&gt; is defined and comment that part. And then you compile again with following results: 

  &lt;br /&gt;&lt;em&gt;Error&amp;#160;&amp;#160;&amp;#160; 11&amp;#160;&amp;#160;&amp;#160; The type or namespace name 'Employee' could not be found (are you missing a using directive or an assembly reference?)&amp;#160;&amp;#160;&amp;#160; C:\&amp;lt;path&amp;gt;\MyWinApp\Web References\WebServices\Reference.cs&amp;#160;&amp;#160;&amp;#160; 82&amp;#160;&amp;#160;&amp;#160; 36&amp;#160;&amp;#160;&amp;#160; MyWinApp 
    &lt;br /&gt;&lt;/em&gt;

  &lt;br /&gt;11. You fix that by resolving the missing type: 

  &lt;br /&gt;&lt;img title="VS Resolve" border="0" alt="VS Resolve" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/VS%20Resolve_3.png" width="518" height="66" /&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;12. You compile and you’re happy right (obviously you need to modify also all &lt;em&gt;WebServices.Employee&lt;/em&gt; types to &lt;em&gt;MyLibrary.Employee&lt;/em&gt; types)?&lt;/p&gt;

&lt;p&gt;Well you might be happy since now your code works... &lt;strong&gt;BUT&lt;/strong&gt; you have manually edited generated file which will be re-generated every time you do “Update Web Reference” from Visual Studio and you’ll lose you modifications. And that’s not nice.&lt;/p&gt;

&lt;p&gt;If this would be question for me I would give you following answer (you might find different opinions on this one): Go to the WCF route instead &lt;strong&gt;:-) &lt;/strong&gt;If Windows Communication Foundation (WCF) is something new to you I think you should check these out and find more information on the web: &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Windows_Communication_Foundation"&gt;Overview of WCF from Wikipedia&lt;/a&gt; and &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/netframework/aa663324.aspx"&gt;Windows Communication Foundation home on MSDN&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I’m going to run through this same example with WCF way and then we can (hopefully) see why it fits like good glove.&lt;/p&gt;

&lt;p&gt;1. Create new WCF project called “MyWcf” using following VS template: 
  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/WCF%201_2.png"&gt;&lt;img title="WCF 1" border="0" alt="WCF 1" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/WCF%201_thumb.png" width="138" height="18" /&gt;&lt;/a&gt; 

  &lt;br /&gt;2. Add reference to “MyLibrary” 

  &lt;br /&gt;3. Delete &lt;em&gt;IService1.cs&lt;/em&gt; and &lt;em&gt;Service1.svc &lt;/em&gt;from your newly created project. 

  &lt;br /&gt;4. Add new item “MyWcfService.svc” (using &lt;em&gt;WCF Service &lt;/em&gt;template) 

  &lt;br /&gt;5. Modify “IMyWcfService.cs” file: 

  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.ServiceModel;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; MyLibrary;

&lt;font color="#0000ff"&gt;namespace&lt;/font&gt; MyWcf
{
  [&lt;font color="#2b91af"&gt;ServiceContract&lt;/font&gt;]
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;interface&lt;/font&gt; &lt;font color="#2b91af"&gt;IMyWcfService
&lt;/font&gt;  {
    [&lt;font color="#2b91af"&gt;OperationContract&lt;/font&gt;]
    &lt;font color="#0000ff"&gt;void&lt;/font&gt; AddNewEmployee(&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee);
  }
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
6. Modify “MyWcfService.cs” file: 

  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; System;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; MyLibrary;

&lt;font color="#0000ff"&gt;namespace&lt;/font&gt; MyWcf
{
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;class&lt;/font&gt; &lt;font color="#2b91af"&gt;MyWcfService&lt;/font&gt; : &lt;font color="#2b91af"&gt;IMyWcfService
&lt;/font&gt;  {
    &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; AddNewEmployee(&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee)
    {
      &lt;font color="#008000"&gt;// TODO: implement
&lt;/font&gt;    }
  }
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
7. Open up “web.config” and modify the &lt;em&gt;system.serviceModel&lt;/em&gt; section as follows: 

  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;system.serviceModel&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
 &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;services&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
  &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;service&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;behaviorConfiguration&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;MyWcf.MyWcfServiceBehavior&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;MyWcf.MyWcfService&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;&amp;gt;
   &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;endpoint&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;address&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&amp;quot;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;binding&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;basicHttpBinding&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;contract&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;MyWcf.IMyWcfService&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;&amp;gt;
    &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;identity&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
     &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;dns&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;value&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;localhost&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; /&amp;gt;
    &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;identity&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
   &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;endpoint&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
   &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;endpoint&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;address&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;mex&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;binding&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;mexHttpBinding&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;contract&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;IMetadataExchange&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; /&amp;gt;
  &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;service&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
 &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;services&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
 &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;behaviors&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
  &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;serviceBehaviors&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
   &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;behavior&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;MyWcf.MyWcfServiceBehavior&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;&amp;gt;
    &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;serviceMetadata&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;httpGetEnabled&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; /&amp;gt;
    &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;serviceDebug&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;includeExceptionDetailInFaults&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;false&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; /&amp;gt;
   &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;behavior&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
  &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;serviceBehaviors&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
 &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;behaviors&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;system.serviceModel&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
(you might notice that most important part is the &lt;strong&gt;binding&lt;em&gt; &lt;/em&gt;&lt;/strong&gt;what I have changed to be &lt;strong&gt;basicHttpBinding&lt;/strong&gt;) 

  &lt;br /&gt;

  &lt;br /&gt;8. Go back to your “My Win App” project. 

  &lt;br /&gt;&amp;#160; - Remove “WebServices” web references 

  &lt;br /&gt;&amp;#160; - Add new “Service Reference” to your newly created WCF Service and name it “WcfServices” 

  &lt;br /&gt;&amp;#160; - Verify that you have “Reuse types in referenced assemblies” checked in settings (you can see them if you click &lt;em&gt;Advanced...&lt;/em&gt; button from the “&lt;em&gt;Add Service Reference”&lt;/em&gt; dialog): 

  &lt;br /&gt;&lt;img title="WCF 2" border="0" alt="WCF 2" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/WCF%202_3.png" width="287" height="256" /&gt;&amp;#160; &lt;br /&gt;

  &lt;br /&gt;9. Modify your code to use this new service: 

  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;MyLibrary.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee = &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;Employee&lt;/font&gt;();
employee.FirstName = &lt;font color="#a31515"&gt;&amp;quot;John&amp;quot;&lt;/font&gt;;
employee.LastName = &lt;font color="#a31515"&gt;&amp;quot;Doe&amp;quot;&lt;/font&gt;;
WcfServices.&lt;font color="#2b91af"&gt;MyWcfServiceClient&lt;/font&gt; myWcf = &lt;font color="#0000ff"&gt;new&lt;/font&gt; MyWinApp.WcfServices.&lt;font color="#2b91af"&gt;MyWcfServiceClient&lt;/font&gt;();
myWcf.AddNewEmployee(employee);&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
10. Enjoy one of the benefits of WCF &lt;strong&gt;:-)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is just one of the many benefits that WCF over the “good old ASP.NET Web Services”. So if you’re interested then you should start looking more information on the web. 
  &lt;br /&gt;

  &lt;br /&gt;Anyways... Happy hacking! 

  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9000423" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/.NET+General/default.aspx">.NET General</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/WCF/default.aspx">WCF</category></item><item><title>Maximize the use of CPU with parallel extensions (+ some WPF stuff)</title><link>http://blogs.msdn.com/jannemattila/archive/2008/08/26/maximize-the-use-of-cpu-with-parallel-extensions-some-wpf-stuff.aspx</link><pubDate>Tue, 26 Aug 2008 22:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8898129</guid><dc:creator>jannemattila</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/8898129.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=8898129</wfw:commentRss><description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Since this is my &lt;u&gt;40th post to this blog&lt;/u&gt; I decided to go back to square one… or &lt;a href="http://blogs.msdn.com/jannemattila/archive/2007/01/07/solving-small-puzzles-with-just-a-few-lines-of-code.aspx" target="_blank"&gt;post one&lt;/a&gt; actually &lt;strong&gt;:-)&lt;/strong&gt; I’m going to create Windows Presentation Foundation (WPF) application that solves the &lt;a href="http://en.wikipedia.org/wiki/Knight%27s_tour" target="_blank"&gt;Knight’s Tour&lt;/a&gt; puzzle. I actually didn’t know about this puzzle before I bought book called &lt;a href="http://www.wrox.com/WileyCDA/WroxTitle/Puzzles-for-Programmers-and-Pros.productCd-0470121688.html" target="_blank"&gt;Puzzles for Programmers and Pros&lt;/a&gt;. That book had interesting puzzle that lead in to this post. So here we go!&lt;/p&gt;  &lt;p&gt;I said that I’m going to create WPF application for my UI. You might ask why not the “good old” Windows Forms application...? Well for these simple reasons:    &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;1.&lt;/strong&gt;&amp;#160; I don’t like to write code to &lt;em&gt;OnPaint&lt;/em&gt; / &lt;em&gt;MainForm_Paint&lt;/em&gt; methods.     &lt;br /&gt;&lt;strong&gt;2. &lt;/strong&gt;I wanted to define my user interface and then just say in code “hey knight go there” and it should just draw the UI with the knight in the correct position. But the defined UI must be also scalable.     &lt;br /&gt;&lt;strong&gt;3. &lt;/strong&gt;WPF doesn’t have same barriers than Windows Forms does =&amp;gt; It’s the face of future applications!     &lt;br /&gt;    &lt;br /&gt;I don’t probably have to explain my reason #1 for you if you have experienced the same that I have :-) You’ll end up writing the UI code a lot and that’s not what you’re trying to do. You’re trying to solve puzzle and you are suddenly focusing for the UI code. That’s wrong approach. Therefore reason #2 goes hand-in-hand with #1. &lt;/p&gt;  &lt;p&gt;So let’s look the the UI of the running application:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/WPF%20UI_2.png"&gt;&lt;img title="WPF UI" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="306" alt="WPF UI" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/WPF%20UI_thumb.png" width="306" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;It’s the view of the classic chess board and I decided to take shortcut when creating the knight. I decided to use gray circle instead (or ellipse actually) &lt;strong&gt;:-)&lt;/strong&gt; And for the layout management I just took the easy approach by using &lt;em&gt;Grid&lt;/em&gt; and defining &lt;em&gt;Columns&lt;/em&gt; and &lt;em&gt;Rows&lt;/em&gt;. Here is the XAML for the UI:&lt;/p&gt;  &lt;table cellspacing="10"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" align="right"&gt;         &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;pre&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Window&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Class&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Window1&amp;quot;
&lt;/font&gt;  &lt;font color="#ff0000"&gt; xmlns&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;
&lt;/font&gt;  &lt;font color="#ff0000"&gt; xmlns&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;x&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;
&lt;/font&gt;  &lt;font color="#ff0000"&gt; Title&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Knight's Tour&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; MinHeight&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;100&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; MinWidth&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;100&amp;quot;&lt;/font&gt; 
    &lt;font color="#ff0000"&gt; Width&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;300&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Height&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;300&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Loaded&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Window_Loaded&amp;quot;&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;  &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Grid&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Board&amp;quot;&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    
    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Grid.ColumnDefinitions&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;Grid.ColumnDefinitions&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Grid.RowDefinitions&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;Grid.RowDefinitions&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;

&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Ellipse&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Knight&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Fill&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Gray&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Panel.ZIndex&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;1&amp;quot;
&lt;/font&gt;        &lt;font color="#ff0000"&gt; Grid.Column&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;{&lt;/font&gt;&lt;font color="#a31515"&gt;Binding&lt;/font&gt;&lt;font color="#ff0000"&gt; Path&lt;/font&gt;&lt;font color="#0000ff"&gt;=KnightX}&amp;quot;&lt;/font&gt; 
        &lt;font color="#ff0000"&gt; Grid.Row&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;{&lt;/font&gt;&lt;font color="#a31515"&gt;Binding&lt;/font&gt;&lt;font color="#ff0000"&gt; Path&lt;/font&gt;&lt;font color="#0000ff"&gt;=KnightY}&amp;quot; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    
    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Rectangle&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;A8&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Fill&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;White&amp;quot; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Rectangle&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;B8&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Grid.Column&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;1&amp;quot; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#008000"&gt;&amp;lt;!-- Etc... --&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;  &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;Grid&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;Window&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;You probably noticed the interesting part of the XAML... and that’s the lines 30 and 31 where &lt;em&gt;Binding &lt;/em&gt;is defined. It means that these values coming from the public properties of the &lt;em&gt;DataContext&lt;/em&gt;. So let’s look at the code behind that XAML and let’s discuss the binding little bit more: 

  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; System;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.ComponentModel;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Diagnostics;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Windows;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Windows.Media;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Windows.Shapes;

&lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;partial&lt;/font&gt; &lt;font color="#0000ff"&gt;class&lt;/font&gt; &lt;font color="#2b91af"&gt;Window1&lt;/font&gt; : &lt;font color="#2b91af"&gt;Window&lt;/font&gt;, &lt;font color="#2b91af"&gt;INotifyPropertyChanged
&lt;/font&gt;{
  &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;int&lt;/font&gt; knightX = 0;
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;int&lt;/font&gt; KnightX
  {
    &lt;font color="#0000ff"&gt;get&lt;/font&gt; { &lt;font color="#0000ff"&gt;return&lt;/font&gt; knightX; }
    &lt;font color="#0000ff"&gt;set
&lt;/font&gt;    {
      knightX = &lt;font color="#0000ff"&gt;value&lt;/font&gt;;
      NotifyPropertyChanged(&lt;font color="#a31515"&gt;&amp;quot;KnightX&amp;quot;&lt;/font&gt;);
    }
  }

  &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;int&lt;/font&gt; knightY = 0;
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;int&lt;/font&gt; KnightY
  {
    &lt;font color="#0000ff"&gt;get&lt;/font&gt; { &lt;font color="#0000ff"&gt;return&lt;/font&gt; knightY; }
    &lt;font color="#0000ff"&gt;set
&lt;/font&gt;    {
      knightY = &lt;font color="#0000ff"&gt;value&lt;/font&gt;;
      NotifyPropertyChanged(&lt;font color="#a31515"&gt;&amp;quot;KnightY&amp;quot;&lt;/font&gt;);
    }
  }

  &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; Window_Loaded(&lt;font color="#0000ff"&gt;object&lt;/font&gt; sender, &lt;font color="#2b91af"&gt;RoutedEventArgs&lt;/font&gt; e)
  {
    Knight.DataContext = &lt;font color="#0000ff"&gt;this&lt;/font&gt;;
    &lt;font color="#008000"&gt;// Now moving the Knight is easy!
&lt;/font&gt;    KnightX = 3;
    KnightY = 3;
  }

  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;event&lt;/font&gt; &lt;font color="#2b91af"&gt;PropertyChangedEventHandler&lt;/font&gt; PropertyChanged;
  &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; NotifyPropertyChanged(&lt;font color="#2b91af"&gt;String&lt;/font&gt; info)
  {
    &lt;font color="#0000ff"&gt;if&lt;/font&gt; (PropertyChanged != &lt;font color="#0000ff"&gt;null&lt;/font&gt;)
    {
      PropertyChanged(&lt;font color="#0000ff"&gt;this&lt;/font&gt;, &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;PropertyChangedEventArgs&lt;/font&gt;(info));
    }
  }
  &lt;font color="#008000"&gt;//...&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
You might already noticed that my window also implements &lt;em&gt;INotyfyPropertyChanged&lt;/em&gt; interface. And my two properties actually call &lt;em&gt;NotifyPropertyChanged&lt;/em&gt; method when they are changed. So what’s this all about? Well if you don’t do this your values will be updated for the first time and after that they don’t actually get “bubbled” up to the ellipse anymore... unless you implement the notify mechanisms yourself. This is quite important and you should probably read more information about it on MSDN. &lt;/p&gt;

&lt;p&gt;For the actual solving part I just used classic “old recursion” to solve the puzzle. And this is the part where we finally are going to the &lt;em&gt;title&lt;/em&gt; of my post... &lt;/p&gt;

&lt;p&gt;Classic one worker thread approach gives “fairly easy to implement but sub-optimal” solution. And you might ask why? And to answer this question I’m going so show you picture of task manager: 
  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/CPU1_2.png"&gt;&lt;img title="CPU1" height="116" alt="CPU1" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/CPU1_thumb.png" width="654" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;This picture was taken when my solver was running in “full speed ahead” –mode. And guess what... &lt;u&gt;I’m not impressed&lt;/u&gt;! I’m actually just using single CPU (see the third box where green line has reached the roof)!!! So my worker thread approach is far from optimal resource usage.&lt;/p&gt;

&lt;p&gt;Okay... What can I do then? I could do multiple threads and handle them manually but that’s again writing a lot of code that doesn’t have anything to do with the actual solving!? So if I would chosen Windows Forms + manual handling of multiple threads I would have a lot of code and just small fraction of that would actually do work that I was originally planning to do. &lt;/p&gt;

&lt;p&gt;This is where &lt;a href="http://blogs.msdn.com/pfxteam/" target="_blank"&gt;&lt;em&gt;Parallel Extensions&lt;/em&gt;&lt;/a&gt;&lt;em&gt; &lt;/em&gt;(&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=348F73FD-593D-4B3C-B055-694C50D2B0F3&amp;amp;displaylang=en" target="_blank"&gt;download&lt;/a&gt;)&lt;em&gt; &lt;/em&gt;comes into the game! It’s additional library (&lt;em&gt;System.Threading.dll&lt;/em&gt;) sitting on top of .NET Framework 3.5 and it’s currently in CTP phase. But I still highly recommend you to check it out if you want easily get more horse power to your algorithms. &lt;/p&gt;

&lt;p&gt;I analyzed my code and noticed part where I could do things differently: 
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#008000"&gt;// My code was this:
&lt;/font&gt;&lt;font color="#0000ff"&gt;foreach&lt;/font&gt; (&lt;font color="#0000ff"&gt;int&lt;/font&gt; location &lt;font color="#0000ff"&gt;in&lt;/font&gt; startLocations)
{
  &lt;font color="#008000"&gt;// Calculations here!
&lt;/font&gt;}

&lt;font color="#008000"&gt;// And I changed it to this:
&lt;/font&gt;&lt;font color="#2b91af"&gt;Parallel&lt;/font&gt;.ForEach&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt;(startLocations, (location) =&amp;gt;
{
  &lt;font color="#008000"&gt;// Calculations here!
&lt;/font&gt;});&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;

  &lt;br /&gt;So I changed code in line 2 to be the code at line 8. What was the result at the task manager then: 

  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/CPU2_2.png"&gt;&lt;img title="CPU2" height="111" alt="CPU2" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/CPU2_thumb.png" width="650" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Well I believe that I managed to get better use of the available horse power &lt;strong&gt;:-)&lt;/strong&gt; It shows of course in the results: 

  &lt;br /&gt;“foreach”: ~50 solved solutions in ~5 minutes 

  &lt;br /&gt;”Parallel.ForEach”: ~450 solved solutions in &amp;lt; 5 minutes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To summarize... &lt;/strong&gt;&lt;u&gt;I just changed 1 line of code and I was able to get unbelievable results from it!&lt;/u&gt; So if you’re doing something similar then I recommend checking out the parallel extensions first before doing “own custom solution” for that. &lt;/p&gt;

&lt;p&gt;I originally thought that I would go little bit deeper into the details of my solver but this post ended up too long even without it so maybe I’ll pass this time... But I’ll include &lt;a href="http://blogs.msdn.com/jannemattila/attachment/8898129.ashx" target="_blank"&gt;video clip&lt;/a&gt; that shows the UI of the application when it’s solving.

  &lt;br /&gt;

  &lt;br /&gt;Anyways... Happy hacking! 

  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;

&lt;p&gt;P.S. I’m also interested in F# and I’m probably going to do something fun with that too.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8898129" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/jannemattila/attachment/8898129.ashx" length="18623" type="video/x-ms-wmv" /><category domain="http://blogs.msdn.com/jannemattila/archive/tags/.NET+General/default.aspx">.NET General</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Application+Development/default.aspx">Application Development</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Transferring attachment file from Web Service to InfoPath</title><link>http://blogs.msdn.com/jannemattila/archive/2007/02/08/transferring-attachment-file-from-web-service-to-infopath.aspx</link><pubDate>Thu, 08 Feb 2007 09:21:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1624447</guid><dc:creator>jannemattila</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/1624447.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=1624447</wfw:commentRss><description>&lt;p&gt;I &lt;a title="InfoPath and Web Service data connection" href="http://blogs.msdn.com/jannemattila/archive/2007/01/21/infopath-and-web-service-data-connection.aspx" target="_blank"&gt;previously posted&lt;/a&gt; about InfoPath and Web Service data connections. Back then I didn't mention that you could use the same method to transfer InfoPath attachments as well. It's actually pretty straightforward thing. All you need&amp;nbsp;to do at the web service is wrap up you existing file data to InfoPath file attachment format and return it as BASE64 encoded. It sounds easy and it really is. Well enough talking... let's checkout the code!&lt;/p&gt; &lt;p&gt;First we need method that creates InfoPath attachment from byte array. It could be something like this:&lt;/p&gt;&lt;pre&gt; 1  &lt;font color="#0000a0"&gt;static public &lt;/font&gt;&lt;font color="#008080"&gt;String&lt;/font&gt; CreateInfoPathAttachment(&lt;font color="#008080"&gt;String &lt;/font&gt;fileName, &lt;font color="#0000a0"&gt;byte&lt;/font&gt;[] fileData)
 2  {
 3      &lt;font color="#008000"&gt;// This memory stream for InfoPath attachment buffer before Base64 encoding.
&lt;/font&gt; 4      using (MemoryStream ms = new MemoryStream())
 5      {
 6          &lt;font color="#0000a0"&gt;uint&lt;/font&gt; fileNameLength = (&lt;font color="#0000a0"&gt;uint&lt;/font&gt;)fileName.Length + 1;
 7          &lt;font color="#0000a0"&gt;byte&lt;/font&gt;[] fileNameBytes = &lt;font color="#008080"&gt;Encoding&lt;/font&gt;.Unicode.GetBytes(fileName);
 8
 9          &lt;font color="#0000a0"&gt;using&lt;/font&gt; (BinaryWriter bw = &lt;font color="#0000a0"&gt;new&lt;/font&gt; BinaryWriter(ms))
10          {
11              &lt;font color="#008000"&gt;// Write the InfoPath attachment signature. 
&lt;/font&gt;12              bw.Write(&lt;font color="#0000a0"&gt;new byte&lt;/font&gt;[] { 0xC7, 0x49, 0x46, 0x41 });
13
14              &lt;font color="#008000"&gt;// Write the default header information.
&lt;/font&gt;15              bw.Write((&lt;font color="#0000a0"&gt;uint&lt;/font&gt;)0x14); &lt;font color="#008000"&gt;// size
&lt;/font&gt;16              bw.Write((&lt;font color="#0000a0"&gt;uint&lt;/font&gt;)0x01); &lt;font color="#008000"&gt;// version
&lt;/font&gt;17              bw.Write((&lt;font color="#0000a0"&gt;uint&lt;/font&gt;)0x00); &lt;font color="#008000"&gt;// reserved&lt;/font&gt;
18
19              &lt;font color="#008000"&gt;// Write the file size.
&lt;/font&gt;20              bw.Write((&lt;font color="#0000a0"&gt;uint&lt;/font&gt;)fileData.Length);
21  
22              &lt;font color="#008000"&gt;// Write the size of the file name.
&lt;/font&gt;23              bw.Write((&lt;font color="#0000a0"&gt;uint&lt;/font&gt;)fileNameLength);
24  
25              &lt;font color="#008000"&gt;// Write the file name (Unicode encoded).
&lt;/font&gt;26              bw.Write(fileNameBytes);
27  
28              &lt;font color="#008000"&gt;// Write the file name terminator. This is two nulls in Unicode.
&lt;/font&gt;29              bw.Write(&lt;font color="#0000a0"&gt;new byte&lt;/font&gt;[] { 0, 0 });
30              bw.Write(fileData);
31          }
32          &lt;font color="#0000a0"&gt;return&lt;/font&gt; &lt;font color="#008080"&gt;Convert&lt;/font&gt;.ToBase64String(ms.ToArray());
33      }
34  }
&lt;/pre&gt;
&lt;p&gt;This code has been&amp;nbsp;"copy-pasted" mainly&amp;nbsp;from &lt;a title="How to encode and decode a file attachment programmatically by using Visual C# in InfoPath" href="http://support.microsoft.com/kb/892730" target="_blank"&gt;this source&lt;/a&gt;. However there is unnecessary code (=my opinion)&amp;nbsp;so I have changed it a little bit. I prefer using the existing &lt;em&gt;ToBase64String&lt;/em&gt; method (unlike in original code sample).&lt;/p&gt;
&lt;p&gt;Now all you need to do at you web service is to pass filename and data to the method and it creates BASE64 encoded string that can be returned to the InfoPath.&lt;/p&gt;&lt;pre&gt;1  ...
2  &lt;font color="#008080"&gt;String&lt;/font&gt; fileName;&lt;br&gt;3  DocumentContainer myDocument;
4  ...
5  &lt;font color="#0000a0"&gt;byte&lt;/font&gt;[] fileData = &lt;font color="#008080"&gt;File&lt;/font&gt;.ReadAllBytes(fileName);
6  myDocument.Size = fileData.Length;
7  myDocument.Document = CreateInfoPathAttachment(fileName, fileData);
8  &lt;font color="#0000a0"&gt;return&lt;/font&gt; myDocument;
&lt;/pre&gt;
&lt;p&gt;In my example I use similar approach as in my previous example. I have &lt;em&gt;Document&lt;/em&gt; struct that has (in this case) at least to fields: size and document. And in InfoPath I could map those fields exactly as I explained in my earlier&amp;nbsp;post. Now you just map &lt;em&gt;Document&lt;/em&gt; field to your InfoPath attachment and that's it:&lt;br&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/InfoPathWebserviceTransferringattachment_150C9/InfoPath1%5B1%5D.png" atomicselection="true"&gt;&lt;img style="border-right: 0px; border-top: 0px; margin: 5px 0px; border-left: 0px; border-bottom: 0px" height="73" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/InfoPathWebserviceTransferringattachment_150C9/InfoPath1_thumb%5B1%5D.png" width="163" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;br&gt;And what makes this really nice... it works also with Forms Services as well! Of course this idea could be also otherway around... so you could sent file from InfoPath to web service using the same underlying idea. In that case you should only parse the InfoPath attachment so that you get the original filename and filedata. Read more about this case in &lt;a title="Programmatically reading a file attachment from an Infopath form" href="http://www.cubido.at/Blog/tabid/176/EntryID/40/Default.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;br&gt;&lt;br&gt;Anyways... Happy hacking!&lt;br&gt;&lt;br&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1624447" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/.NET+General/default.aspx">.NET General</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/2007+Office+system/default.aspx">2007 Office system</category></item></channel></rss>