In Silverlight, you cannot connect to a database directly. You can create visually appealing interfaces, handle events, make user controls , use all logic and .net framework , but you cannot connect directly to a database.
To access data, the Silverlight application needs to call a Web service, which supplies it with data. The Windows Communication Foundation (WCF) provides you a way to access all your databases by writing all the data access logic within it, and your silverlight application can request data from this WCF service.
Before we create a WCF service, you need to keep in mind that the Web service and the Silverlight application are different applications , that merely exchange data. I am going to show you how to create your first WCF service .
There are only two topics which will be covered in this lesson.
1. Creating the WCF service.
2. Modifying the WCF service.
3. Publishing the WCF service
Step 1: Creating the WCF service
1. Open Visual Studio -> New Project -> WCF -> WCF Service Application.
Name it as MyDataService , and click OK.
2. Open Solution explorer, present to the right of the screen and double click on IService1.cs
You will find an Interface and a class there.
3. The methods in the interface have an attribute [OperationContract] , which is used to specify that this method can be called from the Silverlight application.
4. The class has an attribute [DataContract] , which indicates that this class object can be sent from the WCF service to the Silverlight application and vice versa.
5. Only the members of the class which have [DataMember] attribute will be accessible in the silverlight application.
6. The methods defined in the interface are implemented in the Service1.cs file , which is obtained by expanding the Service1.svc in the Solution Explorer.
7. If you want to create your own class, with its data members, you need to give the [DataContract] attribute to that class also , and its members should be given [DataMember] attribute.
Step 2 : Modifying the WCF service
1. We will create a new method in the interface IService1. Add the following code in it
[OperationContract]
int getStatus(string name);
2. Now save this, and go to Service1.svc.cs . We need to implement the new method defined in the interface. So add the following code there
3. That all :) You have created your web service. You can call this getStatus method, which takes a string as a parameter, and returns you the status as 1 if length is greater than 10 , and 0 if lesser.
How to actually use these WCF services will be covered in the next lesson. For a more detailed explanation and examples, check this video.
To access data, the Silverlight application needs to call a Web service, which supplies it with data. The Windows Communication Foundation (WCF) provides you a way to access all your databases by writing all the data access logic within it, and your silverlight application can request data from this WCF service.
Before we create a WCF service, you need to keep in mind that the Web service and the Silverlight application are different applications , that merely exchange data. I am going to show you how to create your first WCF service .
There are only two topics which will be covered in this lesson.
1. Creating the WCF service.
2. Modifying the WCF service.
3. Publishing the WCF service
Step 1: Creating the WCF service
1. Open Visual Studio -> New Project -> WCF -> WCF Service Application.
Name it as MyDataService , and click OK.
2. Open Solution explorer, present to the right of the screen and double click on IService1.cs
You will find an Interface and a class there.
3. The methods in the interface have an attribute [OperationContract] , which is used to specify that this method can be called from the Silverlight application.
4. The class has an attribute [DataContract] , which indicates that this class object can be sent from the WCF service to the Silverlight application and vice versa.
5. Only the members of the class which have [DataMember] attribute will be accessible in the silverlight application.
6. The methods defined in the interface are implemented in the Service1.cs file , which is obtained by expanding the Service1.svc in the Solution Explorer.
7. If you want to create your own class, with its data members, you need to give the [DataContract] attribute to that class also , and its members should be given [DataMember] attribute.
Step 2 : Modifying the WCF service
1. We will create a new method in the interface IService1. Add the following code in it
[OperationContract]
int getStatus(string name);
2. Now save this, and go to Service1.svc.cs . We need to implement the new method defined in the interface. So add the following code there
3. That all :) You have created your web service. You can call this getStatus method, which takes a string as a parameter, and returns you the status as 1 if length is greater than 10 , and 0 if lesser.
How to actually use these WCF services will be covered in the next lesson. For a more detailed explanation and examples, check this video.
No comments:
Post a Comment