WEB SERVICE - THE REMOTE ASSEMBLY

Are you trying to create a multiplayer game? You can do this using WPF, the "ClickOnce" architecture and a web service.

By default "ClickOnce" deployed applications run in a security sandbox. An application has the ability to do safe operations, but can not do unsafe things such as directly accessing the file system. This is very analogous to what JavasSript in an HTML page can do.

So, if you need to have access to a database or a printer your "ClickOnce" application needs to contact a server. For a multiplayer game to work or to create a ranking of a game, you need to access a server. Web Service is the way to go.

Let's go to do a very simple (and useless) application to teach the basic comcepts.

We can think about Web Services like we are doing "remote" assemblies. At our exercice we will have a method that, having by parameters two integers, will return the sum of them.

OK. Create a new folder: Baws at your machine and another at your webhoster server that needs to be an IIS having support for .Net. Like: NTT/Verio - US $20/mo - or the free Brinkster have.

Upload to this server the file SumServer.asmx:

<%@ WebService Language="C#" class="SumServer"%>

using System;
using System.Text;
using System.Web.Services;
using System.Data;
using System.Data.OleDb; 

 public class SumServer:WebService {
  
  public SumServer(){
  //Nothing here
  }

  [WebMethod]
   public int SumInt (int a, int b) {
    return a+b;
   }
  }

TIP: Pay attention that we don't have a namespace here.

TIP: You can verify if all is OK at the server openning your IE Browser and going to this file (like a page): http://www.yoursite.com/Baws/SumServer.asmx

Open the CMD Shell included in the Windows SDK and, going to the Baws at YOUR machine, run:

WSDL http://www.yoursite.com/Baws/SumServer.asmx?WSDL

TIP: You can use our version at: www.dmu.com/Baws if you like.

You need to liberate the firewall. In YOUR folder will be created the "proxy": SumServer.cs

Don't close the CMD Shell yet!

At YOUR Baws folder create a Baws.csproj file:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
    <AssemblyName>Baws</AssemblyName>
    <DefaultClrNameSpace>Baws</DefaultClrNameSpace>
    <TargetType>library</TargetType>
    <OutputPath>C:\Baws</OutputPath>
   </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
   
  <ItemGroup>
    <Compile Include="SumServer.cs">
      <SubType>Code</SubType>
    </Compile>
  </ItemGroup>

 <ItemGroup>
   <Reference Include="System"/>
   <Reference Include="System.Xml"/>
   <Reference Include="System.Data"/>
   <Reference Include="WindowsBase"/>
   <Reference Include="PresentationCore"/>
   <Reference Include="PresentationFramework"/>
   <Reference Include="UIAutomationProvider"/>
   <Reference Include="UIAutomationTypes"/>
  
    <Reference Include="System.Web" />
    <Reference Include="System.Web.Services" />
  </ItemGroup>

</Project>

Don't use the Visual C# Express to compile this project. At the CMD Shell type: MSBUILD. Will be created the file: Baws.dll

Now, let's go to create an application that will use this remote assembly.

Create a new folder: Ba3. Copy inside it the Baws.dll file. . Create a new file: Window1.xaml

<Window  
    xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
    x:Class="DMU.Window1"
    Title="DMU Tutorials Serie"
    Top="100"
    Left= "100"
    Width="400"
    Height="400"
>

<Canvas  Width="400"  Height="400" Background="DeepSkyBlue"  >
<TextBlock    Canvas.Top="50" Canvas.Left="100"   FontSize="14">Type two integers   and press 'Sum'  </TextBlock>
<TextBlock    Canvas.Top="70" Canvas.Left="100"   FontSize="14">Wait contact with the server. Liberate firewall.</TextBlock>
<TextBox Name = "Int1" Canvas.Top="100" Canvas.Left="100" FontSize="14"/>
<TextBox Name = "Int2" Canvas.Top="150" Canvas.Left="100" FontSize="14"/>
<Button   Canvas.Top="200" Canvas.Left="100" Click = "Summing">Sum</Button>
<TextBlock Name="Text1"  Canvas.Top="300" Canvas.Left="100"   FontSize="14"  ></TextBlock>
</Canvas>
</Window>

And the Window1.xaml.cs file will be:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
namespace DMU{
	 
public partial class Window1 : Window{
  SumServer sS = new SumServer();
    
  public Window1(){
   InitializeComponent();
  }

  private void Summing(object sender, RoutedEventArgs e){
   int theInt1 = Convert.ToInt32(Int1.Text);
   int theInt2 = Convert.ToInt32(Int2.Text);
   Text1.Text =  sS.SumInt(theInt1, theInt2).ToString();
  }
 } 
} 

TIP: Pay attention that we aren't "using" a namespace for the remote assembly here.

We are creating a new object of the "remote" Class: SumServer.

Like what is inside a TextBox is always an string we need to convert it to be an integer. Like what we have at a TextBlock is an string we need to reconvert the sum result that is calculated using the method SumInt of the remote assembly.

The Ba3.csproj will be:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <PropertyGroup>
  <AssemblyName>Ba3</AssemblyName>
  <DefaultClrNameSpace>DMU</DefaultClrNameSpace>
  winexe
  <OutputPath>C:\Ba3</OutputPath>
  </PropertyGroup>
	 
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets"/>
  <Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets"/>

  <ItemGroup>
   <ApplicationDefinition Include="MyApp.xaml"/>
   <Page Include="Window1.xaml"/>

   <Compile Include="Window1.xaml.cs">
    <DependentUpon>Window1.xaml</DependentUpon>
    <SubType>Code</SubType>
   </Compile> 

   <Compile Include="MyApp.xaml.cs">
    <DependentUpon>MyApp.xaml</DependentUpon>
    <SubType>Code</SubType>
   </Compile>
  
  
   <Reference Include="System"/>
   <Reference Include="System.Xml"/>
   <Reference Include="System.Data"/>
   <Reference Include="WindowsBase"/>
   <Reference Include="PresentationCore"/>
   <Reference Include="PresentationFramework"/>
   <Reference Include="UIAutomationProvider"/>
   <Reference Include="UIAutomationTypes"/>
   <Reference Include="System.Web" />
   <Reference Include="System.Web.Services" /> 
 
    <Reference Include="Baws,   Culture=neutral, processorArchitecture=MSIL">
      <HintPath>.\Baws.dll</HintPath>
      <Name>Baws</Name>
      <SpecificVersion>False</SpecificVersion>
      <Aliases>Global</Aliases>
    </Reference>
   

  </ItemGroup>
 
</Project>

Our application needs to have a MyApp.xaml and a MyApp.xaml.cs file that will be the same of that previous "Hello World" application. We will not repeat them here.

Compiling and publishing the application - using the Visual C# Express like we did for the "Hello World" application - we can put a button at a HTML page to run it. Try it clicking here. Remember to liberate the firewall.

ATTENTION: You need to wait the contact with our server not only for the download but also at the moment where the sum will occurs. This time can be around 10 seconds, depending of the Internet trafic and of your communication resources. And you need to liberate the firewall.

Our application doesn't have any consistency for the data entry. So: type integers or you will have an error.




PREVIOUS LESSON NEXT LESSON
T.CONTENTS HOMEPAGE