The most effectual way for a Silverlight attention to tap into server-side code is through web services. The basic thought is simple—you include a web service with your ASP.NET website or add reference to a standalone web service, and your Silverlight attention calls the methods in that web service. Silverlight applications can call traditional ASP.NET web services (.asmx services) as well as the WCF services, which are the newer standard.
Making a Web Service
There are 2 ways how to make a Web Service in Visual Studio:
- Add New WCF Service Attention Machinate (File – New Machinate – WCF Service Attention)
- Add New Item – Silverlight-Enabled WCF Service (In the Solution Explorer on your web attention machinate right click and select from the make pleased menu Add – New Item – Silverlight-Enabled WCF Service
As I mentioned in my previous post, if you’re about to make a standalone WCF Service Attention you’ll also needed to make and copy to your web root directory clientAccessPolicy.xml and crossDomain.xml files. These two files take care about accessibility of the Web Service.
When the machinate “built-in” WCF Service is made, you don’t need to reflect about accessibility files. Visual Studio involuntarily configures the Web Service to be accessible for the Silverlight attention.
When you make a web service, Visual Studio, makes an Interface and Implementation files, explicitly as defaults: IService1.vb and Service1.svc.
All functions/subs definitions you make in the Interface file (IService1.vb) – make sure you Imports System.ServiceModel – have to start with <OperationContract()>
WCF Service doesn’t support all object types to return results from functions, expect of some defaults such as Integer, String. Non supported object have to be implemented via class. Each class definition has to start with <DataContract()> and all property within a class has to start with <DataMember()>
Example of IService1.vb
Imports System.ServiceModel<ServiceContract()> _Public Interface IService1<OperationContract()> _Function GetUsers() As List(Of user)End Interface<DataContract()> _Public Class userPrivate _uId As IntegerPrivate _firstName As StringPrivate _lastName As String<DataMember()> _Public Property uId() As Integer Get Return _uId End Get Set(ByVal value As Integer) _uId = value End Set End Property<DataMember()> _Public Property firstName() As String Get Return _firstName End Get Set(ByVal value As String) _firstName = value End Set End Property<DataMember()> _Public Property lastName() As String Get Return _lastName End Get Set(ByVal value As String) _lastName = value End Set End PropertyPublic Sub New() End SubPublic Sub New(ByVal usrId As Integer, _ ByVal uFirstName As String, ByVal uLastName As String) Me.uId = usrId Me.firstName = uFirstName Me.lastName = uLastNameEnd Sub
Very vital
If you’d like to use the web service with your Silverlight attention, you must modify the web service’s Web.Config file and exchange service endpoint binding to “basicHttpBinding”. That’s because the Silverlight works only with this one.
To edit this value, open the web service’s Web.Config file and scroll nearly at the very end of the file. You have to search for these tags:
<system.serviceModel>
<services>
<service name=…
under the <service> tag you’ll find <endpoint address=… tag. Edit this tag’s binding value to “basicHttpBinding“
Example – before update:
<system.serviceModel><services> <service name="WcfService.Service1" behaviorConfiguration="WcfService.Service1Behavior"> <endpoint address="" binding="wsHttpBinding" contract="WcfService.IService1">
after update:
<system.serviceModel><services> <service name="WcfService.Service1" behaviorConfiguration="WcfService.Service1Behavior"> <endpoint address="" binding="basicHttpBinding" contract="WcfService.IService1">
Note: Before you publish your Silverlight attention to the IIS, make sure you modified the ServiceReferences.ClientConfig file located in your Silverlight attention folder. Only one but most vital exchange is required – modify WCF service’s endpoint address to the one on which the WCF service to be accessible on the Internet. By defaulting, Visual Studio set up this address for running the web attention in development environment, such as <endpoint address=”http://localhost:49249/SilverlightApplication.Web/Service.svc“.
Suppose, your WCF service is published on Defaulting Web Site\WCFService folder, so the “real” service address will be http://localhost/WCFService/Service.svc therefore service’s endpoint address in ServiceReferences.ClientConfig file should looks like:
<endpoint address=”http://localhost/WCFService/Service.svc“
Delight refer to Publish Silverlight 3 on IIS 7.0/7.5 for more information.
And finally
Whatever service creation way you choose you’ll need to add a web service reference to your Silverlight attention.
Adding a web service reference is simple. Just, in the Solution Explorer right click on your Silverlight attention and from the make pleased menu select “Add Service Reference“. In the opened pop-up window (Add Service Reference) fill-up the “Address” text box with the service URL (if you’re about to add reference to a standalone service already published on the Web) or click “Learn” button and Visual Studio involuntarily finds service(s) from current machinate for you. You can exchange the defaulting service namespace in the “Namespace” text box to be more comfortable for you. Click “OK” and it’s done.
Delight note: if exchange and/or rebuilt your web service you have to update the web service reference in your Silverlight attention. To update a web service reference you have to right click on its name in the Solution Explorer under Service References and select “Update Service Reference” from the offered make pleased menu.
When you add or update a web service reference, Visual Studio, makes some files in Service References folder and ServiceReferences.ClientConfig file in the root of your Silverlight attention.
Start consuming the web service
Security
All data sent from web service are sent as clear text. So, if you’re carriage sensitive data consider by appropriate encryption for your data.
Adding Service Reference
If you’d like to use your WCF Service in your Silverlight attention you need to add a Service Reference.
In Visual Studio, in Solution Explorer right click on your Silverlight attention folder and select “Add Service Reference” from the context menu. The pop-up appears.
To the Address field enter the WCF service’s URL address. If you’re adding an “machinate built-in” WCF service (your service is in your current solution) you can click to “Learn” button – it will list all found services in the current solution. Choose one you’d like to add.
Exchange the Namespace field if you’d like to.
Click “OK“. If everything went smoothly your WCF service reference is successfully added and you can find the ServiceReferences.ClientConfig file in the attention root.
Consuming web service
After you’ve made the Service Reference you’re ready to use/consume the web service in your Silverlight attention.
It’s useful to import the Service Namespace to your attention (Performance tip: every “dot” in your command requires a call to the object and it’s dramatically impact on performance.)
And finally you can write “consumer” speech.
Example: Suppose, you’ve made the Web Service. Next step is to add a Service Reference, setup Service Namespace as wcfService. Your Silverlight machinate’s name is SilverlightApplication. First of all, open or make a xaml file which is about to consume your Web Service, import Service Namespace and write “consumer” speech (in this example I’ll call the Service after page is loaded). Because Service is asynchronious you need to add a Handler to sub what will consume the result from the Web Service. The MainForm.xaml contains <TextBlock x:Name=”txtUsers“/>
Imports SilverlightApplication.wcfService
Incomplete Public Class MainPage Inherits UserControl
Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As _ System.Windows.RoutedEventArgs) Handles Me.Loaded Dim client As New ServiceClient With client AddHandler .getUsersCompleted, AddressOf usersComplete .getUsersAsync() .CloseAsync() End With client = NothingEnd Sub
Private Sub usersComplete(ByVal sender As Object, ByVal e As _ getUsersCompletedEventArgs) Dim users As New System.Text.StringBuilder For Each u As usr In e.Result With users .Append("uID = " & u.uId.ToString & vbCrLf) .Append("firstName = " & u.firstName & vbCrLf) .Appned("lastName = " & u.lastName & vbCrLf) End With Next txtUsers.Text = users.ToStringEnd Sub
That’s all the basic information you need to know about Silverlight ∧ Web Service. For more details see next postings.
Pleased coding!
Check it out:.NET Programming
Answers Rating