Silverlight and WCF for Beginners

In this article we will learn:

  • How to create Silverlight applications
  • How to create simple WCF services
  • How to deploy solutions on Arvixe

Preparation

To work with Silverlight 4 in Visual Studio 2010, you will need to download and install  Silverlight Tools 4

Create project

Open Visual Studio and create a Silverlight Application

The New Project wizard prompts you to create our site for Silverlight applications

Create Simple WCF Services

Right click on Web project and select the “Add -> New Item…” menu

IDateTimeService.cs

[ServiceContract]
public interface IDateTimeService
{
      [OperationContract]
      DateTime GetDateTime();
}

DateTimeService.cs

public class DateTimeService : IDateTimeService
{
      public DateTime GetDateTime()
      {
            return DateTime.Now;
      }
}

web.config

<?xml version=”1.0“?>

<configuration>

   <system.web>

      <compilation debug=”truetargetFramework=”4.0” />

      <customErrors mode=”Off“/>

   </system.web>

   <system.serviceModel>

      <behaviors>

         <serviceBehaviors>

            <behavior name=””>

               <serviceMetadata httpGetEnabled=”true” />

               <serviceDebug includeExceptionDetailInFaults=”true” />

            </behavior>

         </serviceBehaviors>

      </behaviors>

      <serviceHostingEnvironment multipleSiteBindingsEnabled=”true” />

   </system.serviceModel>

</configuration>

Click Add Service Reference next:

In the “Add Service Reference” dialog window, press “Discover” button and select service.

MainPage.xaml.cs

public partial class MainPage : UserControl
{
      DateTimeServiceClient proxy;

      public MainPage()
      {
            InitializeComponent();

            proxy = new DateTimeServiceClient();
            proxy.GetDateTimeCompleted += (sender, e) =>
            {
                  textBlockDateTime.Text = e.Result.ToString();
            };
      }

      private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
      {
            proxy.GetDateTimeAsync();
      }
}

The coding is now over. Press F5 to run solution.

Deployment

Publish the Web project to file system first:

 

Upload the published site via FTP to Arvixe.com. In Web Site Control Panel, on the “Extensions” page, choose ASP.NET version “4.0”. Also, specify BlogArvixeCom.UserNT.WCFTestPage.aspx as default document on the “Home Folder” page. That seems to be all, but do not be surprised if clicking on the button “Server side time” causes nothing to happen. The file ServiceReferences.ClientConfig file is configured to work on localhost. All you need to do is set the correct endpoint address.

Demo

Source code

Tags: , , , , | Posted under 3rd Party Software, Programming/Coding | RSS 2.0

6 Comments on Silverlight and WCF for Beginners

  1. javea65 says:

    Must be remember in the website, just the WFC endpoint, change to wshttpbinding to basicHttpBinding.

  2. javea65 says:

    I forgotten.. is the web.config where the changes…

  3. Andrew Ivanov says:

    In my approach used basicHttpBinding by default. Of course you can configure bindings in web.config. But this is beyond the scope of this article.

  4. www.aksescepat.com says:

    Setting Proxy

  5. Cool! Thanks for this interesting post!

  6. Penni says:

    Heya i’m for the first time here. I came across this board and I find It really useful & it helped me out a lot. I hope to give something back and help others like you helped me.

Leave a Reply

Your email address will not be published. Required fields are marked *


− 1 = 2

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>