WCF Services in Silverlight by using “Add Service Reference”

Some Intro:

Well, in this post I want to share my experiences druing use of WCF Services in Visual Studio 2008 Silverlight projects by using “Add Service Reference” . You can google MSDN and some other experts blogs for more information.

There exists a lot of drawbacks of using “Add Service Reference” and these are well documented by Miguel A. Castro, NetFXHarmonics and also Developer’s Dump. But before using WCF manually, we could use “Add Service Reference” for this post to understand the WCF service concept.

Note: I will post another article about accessing WCF Services manually without using “Add Service Reference”.

Let’s get started:

First, we create a “WCF Service Application” project. So if you have crated one you should see that we have traditionally “Properties” and “References”, but thanks to Microsoft there exists “Service1.svc” and “IService1″ templates for saying “hello world” to the WCF Services. For this post let’s use them without changing their names for the sake of simplicity.

In this WCF Service my aim is just to add 5 and 6!.

IService1.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace WcfService1

{

[ServiceContract]

public interface IService1

{

[OperationContract]

double Toplam(double x, double y);

}

}

In IService1.cs we have declared our ServiceContract and OperationContract.

Now let’s implement them in Service1.svc.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace WcfService1

{

public class Service1 : IService1

{

public double Toplam(double x, double y)

{

return x + y;

}

}

}

We have almost created a WCF Service but at last we have to add an xml file that configures access policies.

clientaccesspolicy.xml:

<?xml version="1.0" encoding="utf-8"?>

<access-policy>

<cross-domain-access>

<policy>

<allow-from http-request-headers="*">

<domain uri="*">

</allow-from>

<grant-to>

<resource path="/" include-subpaths="true"/>

</grant-to>

</policy>

</cross-domain-access>

</access-policy>

For this example the access policy above will be enough but it is an important issue in WCF so we as developers should work on it to use it efficiently.

Then add a Silverlight Project (which will be our client) to this solution and simply use “Add Service Reference” to access WCF Service we have created before. It automaticly generates required components and you simply in Silverlight use your WCF Service.

Page.xaml.cs:

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using SilverlightApplication1.ServiceReference1;

namespace SilverlightApplication1

{

public partial class Page : UserControl

{

public Page()

{

InitializeComponent();

}

private void Button_Click(object sender, RoutedEventArgs e)

{

Service1Client x = new Service1Client();

x.ToplamCompleted += new EventHandler&lt;ToplamCompletedEventArgs&gt;(x_ToplamCompleted);

x.ToplamAsync(5, 6);

}

void x_ToplamCompleted(object sender, ToplamCompletedEventArgs e)

{

Sonuc.Text = Convert.ToString(e.Result);

}

}

}

That’s sit we accessed our WCF with using “Add Service Reference”. Honestly, I could not  explained it in detail but you can understand it from Chack’s Corner with screenshots.

Download Sample Solution

This night I will prepare the post “WCF Services in  .Net without using “Add Service Reference””.

Thanks for your patience!

kick it on DotNetKicks.com

About these ads