Sunday, March 28, 2010

Calling WCF Service (Hosted in SharePoint 2010) From Silverlight client .

In My previous blog post  I showed how to host custom WCF service inside SharePoint 2010. In this blog post we are going to see how to consume the same service from SilverLight client. here is the link which talks about how to host custom WCF Service in SharePoint 2010 http://mstecharchitect.blogspot.com/2010/03/hosting-wcf-service-inside-sharepoint.html.

Here are steps we are going to follow.

  1. First Create a new SilverLight Project.
  2. Add WCF Service Reference.
  3. Call the WCF Service.

Step 1:

First Fire up VS.NET 2010 and Create SilverLight Application project.

image

Step 2:

Now add WCF Service Reference to newly created SilverLight project.

image

image

Step 3:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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;

namespace SilverlightApplicationCallingWCFsvc
{
    public partial class MainPage :
UserControl
   
{
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SharePointWCFServiceReference.CustomServiceClient svc = new SharePointWCFServiceReference.CustomServiceClient();
            svc.GetTimeCompleted += new EventHandler<SharePointWCFServiceReference.GetTimeCompletedEventArgs>(svc_GetTimeCompleted); svc.GetTimeAsync(); } void svc_GetTimeCompleted(object sender, SharePointWCFServiceReference.GetTimeCompletedEventArgs e)
        {
            txtfrommoss.Text = e.Result.ToString ();
        }
    }
}

In the above code snippet we are creating  the instance of WCF Serviceclient class and Invoking  the WCF Method. In Silverlight Application the service calls are made as Asynchronous calls. To call GetTime Method  in asynchronous we need to add event Handler for the GetTime Mehtod.

Now Hit F5 and test the app.

image 


Nandri(Thanks)



Sreenivasaragavan

5 comments:

Ronak said...

Hi there,
Thanks for Sharing Knowledge now i have deployed WCF Service in ISAPI folder and consuming this service in silverlight.
Now i have WebApplication with extension to Internet zone with Anonymous access.
It works fine on Webapplication on INtranet zone when user is logged in but it ask for username and password on Extended webapplication.
Please advise me if you have any idea.

Thanks
Ronak

Jennifer said...

I am new one for this technology. I get it some new information through your blog. It's really helpful to improve my knowledge.

web hosting india

steve said...

I have never heard about this service because i am new to this, I got great knowledge from this post and this type of information is always good for our knowledge.
Register Domain

Unknown said...

I found great points in your post. Its hard to find a post on this topic!!
Web hosting

best digital watch said...

This post really uncovered a lot more data than I was wanting to discover. I'm truly happy I found this site thus.
analog sports watches
men analog digital watch