Freigeben über


Tutorial: Using Silverlight web service client configuration

Overview

Microsoft Silverlight provides a rich platform to write internet applications. One of its key features is to enable the applications to communicate with the web services. Microsoft Silverlight Beta1 version was equipped with the web service client stack. The web service team has recently added web client configuration support (available in Beta2 and later release). This feature enables deployment time configuration such as changing targeted service endpoints or various behavior settings without having to rebuild and redeploy the binaries. Below illustrates the step-by-step tutorial.

Prerequisites

- Microsoft Silverlight 2.0 Beta 1.

- Microsoft Visual Studio 2008.

- Microsoft Silverlight Tools Beta 1 for Visual Studio 2008.

- Microsoft Silverlight SDK 2.0 Beta 1.

Note: available from the Silverlight download site.

Getting started

1. Before we get started, we have to create a web service. You may follow this instruction on how to create and configure WCF web service to work with Silverlight. Create 2 web services to experience the configuration effects on the Silverlight client and deploy the web services at https://localhost/webservice. Use browser to navigate to https://localhost/webservice/service1.svc and https://localhost/webservice/service2.svc to ensure the services are up and running.

namespace CustomerService

{

    public class Service1 : IService1

    {

        public string GetData(int value)

        {

            return string.Format("Serivce1: You entered {0}", value);

        }

    }

    public class Service2 : IService1

    {

        public string GetData(int value)

        {

            return string.Format("Serivce2: You entered {0}", value);

        }

    }

}

 

2. Use Visual Studio 2008 to create a Silverlight Application. Add a <TextBlock/> in page.xaml. This TexBlock will be used to display the result from web service calls.

Add textblock 

 

3. Generate a proxy to a web service. Select the SilverlightApplication1 on “Solution Explorer”, right click and select “Add Service Reference…” . Enter https://localhost/webservice/service1.svc in address box. Then click Go and OK.

Add Service References... 

 

4. The proxy to the webservice is now created along with the client configuration (ServiceReferences.ClientConfig). The file will be deployed along with other silverlight application components. If you are familiar with .Net 3.0 configuration, this file has similar contents (a subset) and generally used to configure the client proxy.

 

5. Add codes to make a web service call and display the result using the textblock.

namespace SilverlightApplication1

{

    public partial class Page : UserControl

    {

        public Page()

        {

            InitializeComponent();

// hookup page's loaded event

Loaded += new RoutedEventHandler(Page_Loaded);

        }

        void Page_Loaded(object sender, RoutedEventArgs e)

        {

            // call service

            ServiceReference1.Service1Client proxy = new

                ServiceReference1.Service1Client();

            proxy.GetDataCompleted += new

                EventHandler<ServiceReference1.GetDataCompletedEventArgs>

                (GetDataCompleted);

            proxy.GetDataAsync(1);

        }

        void GetDataCompleted(object sender,

            ServiceReference1.GetDataCompletedEventArgs e)

        {

            // display on textblock

            uxText.Text = e.Result;

        }

    }

}

 

6. Compile and run the application. You will see the result returned from Service1 as below.

  

 

7. Let’s assume, at deployment time, you want to direct the client to a different web service (Service2.svc). You could simply edit a ServiceReferences.ClientConfig and modify the endpoint address (see below).

<configuration>

  <system.serviceModel>

    <bindings>

      <basicHttpBinding>

        <binding name="BasicHttpBinding_IService1"

            maxBufferSize="65536"

            maxReceivedMessageSize="65536">

          <security mode="None" />

        </binding>

      </basicHttpBinding>

    </bindings>

    <client>

      <endpoint address="https://localhost/webservice/Service2.svc"

          binding="basicHttpBinding"

          bindingConfiguration="BasicHttpBinding_IService1"

          contract="SilverlightApplication1.ServiceReference1.IService1"

          name="BasicHttpBinding_IService1" />

    </client>

  </system.serviceModel>

</configuration>

 

8. Once you repackage this file into the XAP and run the application (or by simply recompile the application). You will see the result returned from Service2.svc.

 

 

Conclusion

The webservice client configuration is very useful for deployment time configuration. For Silverlight application, it contains a subset of .Net WCF client configuration. Unlike .Net configuration, the configuration file must be named “ServiceReferences.ClientConfig”, packaged and deployed along with the application.

Comments

  • Anonymous
    April 16, 2008
    Silverlight 2 Beta1 makes it easy to use Web Services based on either the WCF technology (Windows Communication

  • Anonymous
    June 12, 2008
    In your description of the .config file, you use language like "repackage into the xap" and "recompile the application". Is this file not able to be altered at an installaton site? What I mean is, if I have a software installer that installs my web applicaton, how can it update the URL in my ServiceReferences.ClientConfig? This appears to be similar to issues faced when trying to use OneClick.