This is the second article of 3 parts(probably more) series regarding PRISM and MVVM. The first post can be found at this link.
Silverlight PRISM & MVVM – PART I
Hi again,
In the second post of my 3 part series of my Silverlight MVVM PRISM application we will go through creating a normal WCF service and updating it to work with Silverlight rather than creating Silverlight enabled WCF services. Even before doing that we will add two other projects named which kind of infrastructure classes containing our business entity class.
So let’s get started.
Step 1:-
Let’s create a simple regular class library and add one class named RssItem show as below. Here we created a class library project called RSSReaderModel and added a class called RssItem whose content are shown as below.

public string Title
{ get; set; }
public string Link
{ get; set; }
public string Description
{ get; set; }
public string PublishedDate
{ get; set; }
Step 2:-
Now we will crate a silverlight class library project and add the RssItem class file as a link into the silverlight porject. The idea is to resue the same class between your server and client rather then creating two different copies and maintining them separately. How to add a file as a reference into your silverlight class library project is shown as below. Rigth click on your project as click “Add Existing Item”. Navigate to that item and do as show in the second image.



In your solution explorere your silverlight class library porject should look like as above.
Step 3:-
Now next thing to do is add a WCF service and it’s host project into our application. There are couple of approaches two do this. First is to crate silverlight enabled WCF service and other is creating a simple WCF service and make it silverlight enabled. Sometimes I have seen people creating two projects for this, one as a class library and other as a asp.net application to host the WCF service class library. For now we will keep this simple and just add one WCF service projects.


Next thing we did is we changed the defautl Service1 and IService1 to some meanigful names as shown above.Next we will enable this WCF service to be silverlight enabled. For this reasong we need to do two major changes.
1) Add following section in the web.config file of your newly added WCF servic project.

2) In your RssService.cs file add following line.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
3) The last thing to do is modify the binding into your web.config from wsHttpBinding to basicHttpBinding as shown below.
<service behaviorConfiguration="MVVM.RSSReader.Service.Service1Behavior" name="MVVM.RSSReader.Service.RssService">
<endpoint address="" binding="basicHttpBinding" contract="MVVM.RSSReader.Service.IRssService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
With this thigs done our wcf servie is almost ready to be consumed by the silverlight client. One thing we want to do now is to add a method which will return us a list of RssItem to the silverlight client. In your IRssService inrerface mehtion following OperationContract.
Add reference to your Normal Class Library project’s reference to this application which will allow us to define and use instances of RssItems.
[OperationContract]
List<RssItem> GetBlogItems(string blogUrl);
And ofcourse implement this interface into your RssService.cs as shown below.
public List<RssItem> GetBlogItems(string blogUrl)
{
List<RssItem> items = new List<RssItem>();
var uri = new Uri(blogUrl);
WebRequest request = WebRequest.Create(uri);
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
XElement sml = XElement.Parse(responseFromServer);
List<XElement> elements = (from c in sml.Descendants("item") select c).ToList();
foreach (XElement elem in elements)
{
RssItem item = new RssItem();
item.Title = elem.Element("title").Value.ToString();
item.Description = elem.Element("description").Value.ToString();
item.Link = elem.Element("link").Value.ToString();
items.Add(item);
}
return items;
}
Okey we are almost there. The next step is to add view and call this method to display RssItems into our silverlight client. Before we close this post let’s do one more thing and that we will be left with very few things to be done in the last post.
Step 4:-
Let’s add a new module to our project called RssModule to respect the PRISM strucutre. For this let’s add a new silverlight library project and name it as RSSModule. In this project add new two folders called View and ViewModel.
In the View folder add one file named ReadRss.xaml. This view will actually display the rss items but for now let’s leave ut aside and do following.
Add reference to folowing DLLs.
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
Add one class file named ViewRSSModule.cs and add implement IModule interface as shown below. What this does is basically registers our ReadRss vuew in the MainRegion defined in the Shell.
public class ViewRSSModule : IModule
{
private readonly IRegionManager regionManager;
public ViewRSSModule(IRegionManager regionManager)
{
this.regionManager = regionManager;
}
public void Initialize()
{
regionManager.RegisterViewWithRegion("MainRegion", typeof(RSSModule.View.ReadRss));
}
}
Once this is done add reference of your Silverlight Class Library project’s reference to this project. For now we are not using it anywhere but we will use that in the last post.
If you remember, in our bootstrapper class we created a dummy module catalog now is the time to change it and register the actual module we careted above.
protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog()
.AddModule(typeof(ViewRSSModule));
return catalog;
}
Okey. So far so good. Now in the next and final post we will make our client and service talking to each other and modify our view to display content of blog url.
Thanks