Saturday, October 31, 2015

WCF Service as DataSource in SSRS





In this article, I will show you how to use WCF Service as Data Source in SSRS.
1. First of all create one WCF Service Application in visual studio 2010. In that service create one class named as Item. Let’s give DataContract attribute to class. Also add some properties with DataMember attribute in Class.
?
1
2
3
4
5
6
7
8
9
10
11
12
[DataContract]
public class Item
{
    [DataMember]
    public int ItemID { get; set; }
    [DataMember]
    public string ItemName { get; set; }
    [DataMember]
    public double ItemSales { get; set; }
}
1-WCF Service as DataSource in SSRS
2. Then create one OperationContract named as GetAllItems in IService1Interface.
?
1
2
3
4
5
6
[ServiceContract]
public interface IService1
{
    [OperationContract]
    List GetAllItems();
}
2-WCF Service as DataSource in SSRS
3. Now in your Service1.svc class add two methods named as GetAllItems()and ItemData().
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public List GetAllItems()
{
    return ItemData();
}
private List ItemData()
{
    List itemList = new List();
    itemList.Add(new Item { ItemID = 1, ItemName = "Bikes", ItemSales = 125000.00 });
    itemList.Add(new Item { ItemID = 2, ItemName = "Clothes", ItemSales = 100000.00 });
    itemList.Add(new Item { ItemID = 3, ItemName = "Cars", ItemSales = 75000.00 });
    return itemList;
}
3-WCF Service as DataSource in SSRS
4. To check WCF service, press F5 button. A WCF Test Client window opens. Invoke GetAllItems() method and see results like below screen shot.
4-WCF Service as DataSource in SSRS
So we have done with WCF Service. Now we use above WCF Service as DataSource in SSRS Report.
5. Now open Visual Studio 2010 and open your existing ssrs project or create a new project.
In that project crate new report named as DemoWCFServiceDataSource. Then create a new Data Source. To create a new data source, right click on Data Source in Report Data window and select Add Data Source.
Data Source Properties window opens. In that select Embedded Connection and select XML from list of data source. In connection string text box enter following URL :
http://localhost:12140/Service1.svc 
In your case 12140 may be changed. so modify it as par your requirement.
5-WCF Service as DataSource in SSRS
6. Then right click on Datasets in Report Data window and select Add Dataset.
Dataset Properties window opens. In that select DataSource and then click on Query Desinger. In Query Designer window enter following XML query into text box.
?
1
2
3
4
<Query>
<Method Namespace="http://tempuri.org/" Name="GetAllItems" />
</Query>
6-WCF Service as DataSource in SSRS
You can also check whether WCF Service returns data or not by clicking on Run button as shown in above screen shot. Then click on OK button.
7. Now right click on Report Area and go to Insert–>Table. Now select ItemIDas Data field in 1st Column, ItemName in 2nd Column and ItemSales in Last Column.
so your report design looks like below :
7-WCF Service as DataSource in SSRS
8. To see the results click on preview tab. Your report should look like below screen shot :
8-WCF Service as DataSource in SSRS
Congratulations! We successfully completed use of  WCF Service as DataSource in SSRS