Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Wednesday, May 14, 2008

Silverlight RSS Feed Reader Using WCF

We can access RSS Feed from a Silverlight client, but it do have some limitations. ‘Cross-Domain’ Issues. The control and the feed are not hosted on the same domain, we need to add a clientaccesspolicy.xml or crossdomain.xml file to domain where the feed is hosted to enable cross-domain access, this is not a practical solution.

So here is the alternate solution, Access RSS Feed using WCF Service from a Silverlight application. For that we need have a WCF Service and a Silverlight Client.

WCF service will fetch the data from the feed URL and passed to the client.

Steps:
Create a new Silverlight Application using VS2008, Select “Add a new web to the solution for hosting the control’ Option.

Then Right click the Web project, select Add New Item and add WCF service.

Now WCF Service will be added to the Web.

Add a reference System.ServiceModel.Syndication to the service

Create a custom class for Feed data in to the service. Something like below :
//
/// Feeed Data List
///

[DataContract]
public class FeedData
{
public FeedData()
{

}
private string _LastUpdatedTime;
private string _PublishDate;
private string _Title;
private string _Summary;

[DataMember]
public string PublishDate
{
get { return _PublishDate; }
set { _PublishDate = value; }
}
[DataMember]
public string Summary
{
get { return _Summary; }
set { _Summary = value; }
}
[DataMember]
public string Title
{
get { return _Title; }
set { _Title = value; }
}
[DataMember]
public string LastUpdatedTime
{
get { return _LastUpdatedTime; }
set { _LastUpdatedTime = value; }
}


Add Service contact :

///
/// Summary description for IFeedAPI
///

[ServiceContract]
public interface IFeedAPI
{
[OperationContract]
List GetRSSData(string FeedURL);
}


Implement the interface

///
/// Fetched the RSS data from the current RSS Feed
///

/// A List of FeedData
public List GetRSSData(string FeedURL)
{
List feedData = new List();
SyndicationFeed feed = null;

// Get the current feed and load into an XMLReader
using (XmlReader reader = XmlReader.Create(FeedURL))
{
Rss20FeedFormatter feedFormatter = new Rss20FeedFormatter();
// Read the contents of the XMLReader into the FeedFormatter
feedFormatter.ReadFrom(reader);
// Get the current feed.
feed = feedFormatter.Feed;
}
foreach (SyndicationItem dataItem in feed.Items)
{
//Adding Feed Items to FeedData List
feedData.Add(new FeedData { LastUpdatedTime = dataItem.LastUpdatedTime.ToString(), PublishDate = dataItem.PublishDate.ToString(), Summary = dataItem.Summary.Text, Title = dataItem.Title.Text });
}
return feedData;
}


Then in the silverlight application, add the service reference, create the proxy instance of the service, call the GetRSSDataAsync method by passing the Feed URL, in the completed of GetRSSDataCompleted, we will get a data list type FeedData.

Thursday, March 27, 2008

WCF Client Proxy - Default Constructor & Parameterized Constructors

When service reference is added in the client application, many files are added to client as part of service reference like Reference.cs, ServiceReferences.ClientConfig, .svcinfo files, .datasource files, .disco files, .wsdl files, .xsd files etc.

Default Constructor & Parameterized Constructors are created within the Reference.cs


public ServiceAPIClient() :
this(defaultBinding, defaultAddress) {
}

public ServiceAPIClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}


Default Constructor ‘ServiceAPIClient()’ can be used for creating proxy for normal cases. It will use the default EndpointAddress and Binding from the Reference.cs file.

private static System.ServiceModel.Channels.Binding defaultBinding = new System.ServiceModel.BasicHttpBinding();

private static System.ServiceModel.EndpointAddress defaultAddress = new System.ServiceModel.EndpointAddress("http://localhost:56789/Service/ServiceAPI.svc");



In case if the service EndpointAddress to be changed, Usually Service reference is updated and used.

Constructor with Parameters can be used in this case, by defining the EndpointAddress and Binding Type.

public ServiceAPIClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}



Code:

Binding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:56789/Service/ServiceAPI.svc");

ServiceAPI.ServiceAPIClient(binding, address);


Here there is no need to Configure the service again if there is no change in the service part. Only the service host address is changed.