Hosting WCF Service 4.0 in IIS 7.5

Hi Everyone,

Today I want to show you what I did to get my WCF Service hosted on my development machine IIS (this could be your web server too). I am using VS 2010, .NET 4.0 framework, and IIS 7.5 in Windows 7 environment.

In this demonstration I am going to use my AlkitabWebService.svc found in this post: Silverlight 4 Simple Project Part 2 (WCF Service).

Step 1 - Enabling IIS + ASP.NET Registration
  • Click Start -> Control Panel -> Programs -> Programs and Features.
  • On the left tab, click Turn Windows Features on or off. Turn on the features below and click OK.


  • When you go to http://localhost , you will now see the IIS7 homepage.
  • Now go to a command prompt and run aspnet_regiis:


Step 2 - Publish the WCF service from VS 2010
  • Right-click the project and click Publish.
  • Choose 'File System' as Publish method, and type C:\inetpub\wwwroot\AlkitabWebService\


This will create a folder for your WCFService that you can refer to from IIS in the next step.

Step 3 - Create New Application in IIS
  • Go to your IIS Manager (can be accessed from Administrative Tools).
  • Right-click Default Web Site -> Add Application...
  • Enter in the Application Name and point the physical path to the folder that contains our published WCF service.


Step 4 - CrossDomain.xml and ClientAccessPolicy.xml
  • In order to be able to access your localhost in IIS, you need to implement a cross domain policy. I'm just going to make this simple. In your C:\inetpub\wwwroot folder, create a crossdomain.xml and clientaccesspolicy.xml files:

  • Crossdomain.xml



  • Clientaccesspolicy.xml


Step 5 - Test your WCF Service
  • In IIS Manager, click the AlkitabWebService (alias).
  • On the right tab, click Browse*:80 to go to your http://localhost/AlkitabWebService/
  • You should get the view like this:


  • Clicking on AlkitabWebService.svc is our Service page. Yay!

Silverlight 4 Simple Project Part 4 (Binding & Command)

Silverlight 4 Simple Project Part 3 (MVVM Pattern)

This is the last part of the blog (finally :P).

In the previous post we already created our MVVM. How do we connect our View to the ViewModel? Easy! There are couple of ways to do this. We can do it declaratively in XAML or we can do it in the code-behind.


View - View Model & Binding

I will choose the first option.



Looking at the XAML snippet above, we declare our ViewModel as a User Control Resource and set the DataContext of our main grid to that resource.

Having done that, now we can easily do our data binding for our user elements.



How cool is that? We don't need any code-behind at all for our XAML! This is the beauty of MVVM Pattern.

Now when we select our book title, chapter, or verse, we will get our results back in the grid without any postback since all calls are made asynchronously.



Command

Another new feature in Silverlight 4 is the built-in Commanding option. In this project, if you look at the image above, there is an 'Enable Range' button. I attached a command to this check box so that when IsChecked is True, it will enable the SelectBookVerseEnd selection. If IsChecked is False, it will disable the combobox.

The SelectBookVerseEnd property will in turn decide whether we call the GetAlkitabSnippet function (single AlkitabSnippet object) or the GetAlkitabSnippets function (multiple AlkitabSnippet objects).



Notice that I use UI Element to Element binding between the IsEnabled property of the combobox and the IsChecked property of the checkbox.

To make the commanding easier, I am using the DelegateCommand class (put this inside your ViewModel) that implements the ICommand interface. This class will have the Action and CanExecute parameters that are required by the interface.



If you look at the XAML, the command of the checkbox is bound to DisableRangeCommand.
Lets look at how the commanding is implemented in our view model




and that's it! Simple is that. Commanding enables MVVM pattern since we don't require to handle the UI event in the XAML code behind.

I will post the solution later on. Thanks for reading this and I hope you will find it useful for your own purposes.



Regards,

Andreas

Silverlight 4 Simple Project Part 3 (MVVM Pattern)

Silverlight 4 Simple Project Part 2 (WCF Service)

In Part 2 we already created our WCF Service that can return some results to us when functions are called.

In this post I will start building my Silverlight Application :)

Silverlight Project

Create new project inside the same solution. Choose Silverlight Application and name it AlkitabApplication. Right-click the project and choose Add Service Reference. click Discover and you can see that our AlkitabWebService.svc is shown. Easy!



In the above picture you can also see that I created three new folders called Model, ViewModel, and Views. Move the MainPage.xaml to the Views folder.

Model View View Model (MVVM) Pattern

For those who are new to this term, you can read this. Otherwise you might want to move on.

MVVM is just a pattern. There is no strict procedure on how to implement it. This pattern is created for developers to separate the logic of their application, providing a nice way to do unit testing and creating blendability between designer <-> developer.

What this means is that designers can focus on playing around with the Views and developers can provide back-end data in the View Model (that perform queries against the Model) independently. The View can then just hook the View Model into it and create the bindings between the UI Elements <-> data. Moreover, developers can provide designers with dummy data to test while designing the Views.

People have been creating tools and frameworks to assist this pattern, such as MVVM Light, MEF, and Prism. However, I won't be using any of those since my application is really simple.

Confused? Let's see in action!

Model

Rather than calling WCF Service straight in our XAML code behind, we will create a class called AlkitabService.cs in our Model folder. It is nice to have a high level layer that provides us with one logical purpose: Our Data Source!

This class will provide us sets of functions that call WCF Service functions asynchronously. Asynchronous call is what Silverlight is all about right? :)



Views

I am not much of a designer, but I like clean design :) Here is what the application looks like:



I won't show you the XAML just yet because I will show it when I'm doing the bindings. In this project I am using Telerik Silverlight controls (RadComboBox and RadGridView). Simple reason, they have nice features such as Auto-Complete, Filtering, etc. However you can use the normal Silverlight controls to do the same thing.

View Model

Here is the main player. This is where all the logic resides. Inside the folder, I created two classes: ViewModelBase.cs and AlkitabViewModel.cs

ViewModelBase is just a base class that implements INotifyPropertyChanged. Remember that all data/properties exposed in the UI need to have this event raised to notify the UI elements.



Inside AlkitabViewModel.cs is where we call our Model and store the data into our class properties that we can then bind to our UI.



Notice that we raise the NotifyPropertyChanged event in the setter of all properties.

I hope you don't get a headache from this. In the next post I will talk about hooking up our ViewModel to our Views and perform the bindings. I will also quickly explain the Commanding in Silverlight 4.

Silverlight 4 Simple Project Part 4 (Binding & Command)

Silverlight 4 Simple Project Part 2 (WCF Service)

Silverlight 4 Simple Project Part 1 (Overview & Database)

Here I will create a WCF Service as a middleman for our Silverlight <-> Alkitab Database that I mentioned in Part 1.

New Project & LINQ to SQL

To start, I will create a WCF Service Application from Visual Studio 2010 (I didn't choose WCF Service Library because we will host it as a 'web' application in the future).



Then right-click the project, Add New Item -> LINQ to SQL Classes. We want to drag our two tables from the Server Explorer into the dbml file. This will provide us the DataContext we require for performing our queries.



WCF Service

Next, rename Service.svc and IService.cs to friendlier names like AlkitabWebService.svc and IAlkitabWebService.cs

IAlkitabWebService.cs is the interface class for our service. This will hold all the OperationContract (functions) declarations and any DataContract (web class) that you want to create along with its DataMember (class property). Here I have sets of operations and AlkitabSnippet class that will hold verse and content:

[ServiceContract]
public interface IAlkitabWebService
{
[OperationContract]
string GetContent(string bookTitle, int bookChapter, int bookVerse);

[OperationContract]
ObservableCollection<AlkitabSnippet> GetAlkitabSnippet(string bookTitle, int bookChapter, int bookVerse);

[OperationContract]
ObservableCollection<AlkitabSnippet> GetAlkitabSnippets(string bookTitle, int bookChapter, int bookVerseStart, int bookVerseEnd);

[OperationContract]
ObservableCollection<string> GetBookTitles();

[OperationContract]
ObservableCollection<int> GetBookChapters(string bookTitle);

[OperationContract]
ObservableCollection<int> GetBookVerses(string bookTitle, int bookChapter);
}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class AlkitabSnippet
{
string verse;
string content;

[DataMember]
public string Verse
{
get { return verse; }
set { verse = value; }
}

[DataMember]
public string Content
{
get { return content; }
set { content = value; }
}
}


In the AlkitabWebService.svc.cs, we implement all the service functions that we need to get data out of the database. Here are two of the functions:

public class AlkitabWebService : IAlkitabWebService
{
public ObservableCollection<AlkitabSnippet> GetAlkitabSnippets(string bookTitle, int bookChapter, int bookVerseStart, int bookVerseEnd)
{
ObservableCollection<AlkitabSnippet> snippets = new ObservableCollection<AlkitabSnippet>();

AlkitabDataContext db = new AlkitabDataContext();

var query = from c in db.Alkitabs
join b in db.AlkitabBooks
on c.book equals b.bookno
where b.booktitle == bookTitle && c.chapter == bookChapter && c.verse >= bookVerseStart && c.verse <= bookVerseEnd
select new { verse = c.verse, content = c.content };

foreach (var e in query)
{
AlkitabSnippet snippet = new AlkitabSnippet();
snippet.Verse = bookTitle + " " + bookChapter + ":" + e.verse;
snippet.Content = e.content;
snippets.Add(snippet);
}

return snippets;
}

public ObservableCollection<string> GetBookTitles()
{
ObservableCollection<string> booktitles = new ObservableCollection<string>();

AlkitabDataContext db = new AlkitabDataContext();

Table<AlkitabBook> AlkitabBook = db.GetTable<AlkitabBook>();

var query = from a in AlkitabBook orderby a.bookno select a.booktitle;

foreach (var e in query)
{
booktitles.Add(e.ToString());
}

return booktitles;
}
}


One question you might ask (I asked this to myself as well when I was a total newbie) is what is an ObservableCollection. WCF Proxy Class use this type of collection by default. However, this collection is different from other type of normal collection, lets say List, in a sense that it provides notification when items get added, removed, or refreshed and when the items property is changed (it implements INotifyCollectionChanged and INotifyPropertyChanged).

This is required for Silverlight to get the notification any change so that it can update all the UI elements associated with the collection.

That's all we need to do for our WCF Service :)

In the next post we will create our Silverlight application that references this service. I will start with the explanation of MVVM model, the popular pattern of building your application.

Silverlight 4 Simple Project Part 3 (MVVM Pattern)

Silverlight 4 Simple Project Part 1 (Overview + Database)

Howdy!

This will be my first technical blog. The motivation behind this is that I love learning Silverlight (and it's version 4 now!). So I decided to create my own simple project as I am thinking of building a website for my church community group in the future.


Overview:


Why not start with a simple module? I want to be able to search for a bible verse in a quick way and no hassle of having to spend much time on it. Therefore I want the users to be able to select the book title, chapter, and verse (or verse range) and have the contents showed in front of them straight away!

Here is a video showing the completed application with some explanation:



Database:

Now.. I found out that there are already some English bible web services out there. However since my community is Indonesian, I want to have an Indonesian bible, and it turns out that there is none! and there is no SQL formatted database dump available either :(

However, I could find the text format of the bible. I looked inside and here is what it looks like:


'This must be my day', I said to myself; This is because I could easily do this:



And just use bulk insert to load the txt file (Alkitab = Bible):
BULK INSERT Alkitab
FROM 'c:\IndonesianBible.txt'
WITH
(
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
)


Nice. Now I have my Indonesian bible database. To improve my database, I am going to create another table that contains my book title and link both tables together:



In the next post I will create a WCF Service with some functions that will query this database and return the contents that will be consumed by our Silverlight application. :)

Silverlight 4 Simple Project Part 2 (WCF Service)

My Piano Songs - Blog Started!

Hello Everyone,

As I am starting my blog, I want to entertain you all with few of my popular songs. Feel free to listen to all of them and give any feedback! ^_^

Here you go:

1. Rising



2. Winter Love



3. The Truth




I will probably post my other songs regularly. I will also post my YouTube videos here as well. Feel free to follow my blog :)

Thanks and Enjoy!

Andreas