Monday, January 11, 2010

Webservices using Silverlight 2.0

In silverlight can't connect directly to the database. it can only connect through webservices.
Before connecting to webservices. we should aware of security, data integrity, binding, performance.
using silverlight user connect diferent services.the services are webservice, WCF service and REST.
hope know about webservice and wcf, so all are asking about REST. Rest means RePresentation of state transition.
through webclient and httpwebrequest can connect and get the response.
usually webservices can connect through http. while connecting you have to maintain the thread. it's should
run with in thread only. if you not connect through proxy. you should maintain the thread. other wise it would
be problem.
Method 1:
Calling ASMX webservices.
Step 1:
Create asmx webservice and in to silverlight application as a service reference.
Step 2:
ADD namespace in the using section.
Step 3:
Get path of asmx file through this method.
public string GetURLforResource(string resourcePage)
{
string webURL = HtmlPage.Document.DocumentUri.ToString();
string containerPage = webURL.Substring(webURL.LastIndexOf("/") + 1);
return webURL = webURL.Replace(containerPage, resourcePage);
}

Step 4:
Call webservice through this. string webServiceURL = GetURLforResource("WebserviceTest.asmx");
BasicHttpBinding binding = new BasicHttpBinding();
WebserviceTestSoapClient webSVC = new WebserviceTestSoapClient(binding,new EndpointAddress(webServiceURL));
webSVC.HelloWorldCompleted += new EventHandler(webSVC_HelloWorldCompleted);
webSVC.HelloWorldAsync();

also for the method has to create the eventhandler.
Step 5:
This is the event handler to display the result of the webservice method.
void webSVC_HelloWorldCompleted(object sender, HelloWorldCompletedEventArgs e)
{

HtmlPage.Window.Alert(e.Result);
}

Method 2:
Calling WCF service.
Everything same as like the above. one thing must to do in web.config.
while creating wcf it will add automatically binding as wsHttpBinding instead of that
have to modify basicHttpBinding.
Method 3:
Coming soon










-