Showing posts with label sharepoint. Show all posts
Showing posts with label sharepoint. Show all posts
Tuesday, November 29, 2011
How to call a WCF service in silverlight
In Silverlight, we often come across a very common requirement to consume Cross Domain Services. In order to support Cross Domain Services in Silverlight, we have to add 'Cross Domain Policy' file at the root of the web server. But if that Service is hosted in a Console Application or a WPF Application or a Windows Service, then how do we go about it? Where do we look for the Policy file?
In this article, we will discuss one approach to do so. We will be discussing the following steps in the order given below:
1) We will create a WCF Service to consume in a Console Project.
2) We will create another Service which will be used to provide the Policy File at that Particular URI.
3) We will edit our App.Config to be able to expose the service and will test run our Console Application.
4) We will then finally create a Silverlight Client Project which will consume this WCF Service.
Note: I will discuss a step by step approach for a Console Application. In a similar manner, you can also expose services from a Windows Service or WPF application.
Create a WCF Service to consume in a Console Project
Step 1: Open Visual Studio 2008 > File > New Project > Select the language (C# or VB.NET) > Select ‘Windows’ in the Project Types > Select ‘Console Application’ from Templates > Target type ‘.NET Framework 3.5’ > Type a name ‘AccessAServiceFromConsoleApplication’ (for C#) or ‘AccessAServiceFromConsoleApplicationVB’ (for VB) and location for the project and click ok. (Check the screenshot below)
Step 2: If you have performed the above step correctly, you will be having only a Program.cs or Module1.vb file in this Project. Now Right Click on the Project > click Add > click New Items. A window will appear displaying different templates. Select ‘WCF Service’ from the templates and name it as ‘MyService.cs’ or ‘MyService.vb’ (See Screenshot below). Now click on Add.
It will add two .cs (named IMyService.cs and MyService.cs) or .vb files (named IMyService.vb and MyService.vb)
Step 3: We will now make some modifications to the [OperationContract] by changing the return type of default method (DoWork which will be added by default) in the IMyService.cs or IMyService.vb interface from void to string. IMyService.cs or IMyService.vb will look like this:
C#
namespace AccessAServiceFromConsoleApplication
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
string DoWork();
}
}
VB.NET
<ServiceContract()> _
Public Interface IMyService
<OperationContract()> _
Function DoWork() As String
End Interface
Step 4: Since the ‘MyService’ class is implementing ‘IMyService’, we will make a small change in it also. In the ‘DoWork()’ method we will return a simple string.
MyService will look like this.
C#
namespace AccessAServiceFromConsoleApplication
{
public class MyService : IMyService
{
public string DoWork()
{
return "This is A Sample From DotNetCurry.com";
}
}
}
VB.NET
Public Class MyService
Implements IMyService
Public Function DoWork() As String Implements IMyService.DoWork
Return "This is A Sample From DotNetCurry.com"
End Function
End Class
Step 5: Now Right Click on the Project > click Add > click New Items. A window will appear displaying different templates. Select “XML File” from the templates and name the xml file as ClientAccessPolicy.xml (See Screenshot below). Click Add.
Add the following contents to the ClientAccessPolicy.xml. You can get the content of this file from Microsoft’s site.
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
Before moving ahead, please build the solution and copy the ClientAccessPolicy.xml to the \bin directory of the Console Application. If you are unable to view the \bin folder, click on ‘Show All Files’ in the Solution Explorer.
Step 6: Now Right Click on Project > click Add > click New Items. A window will appear displaying different templates. Select WCF service from that and name that as CrossDomainService (See screenshot below). Click on Add.
It will add two .cs files (named ICrossDomainService.cs and CrossDomainService.cs) or two .vb files (named ICrossDomainService.vb and CrossDomainService.vb)
Provide the Policy File at that Particular URI
Step 7: We will now change the default implementation of this service. Here we have to serve the cross domain file manually. To do so, we will use REST Service (Representational State Transfer) using WCF to provide the clientaccesspolicy.xml file. Using this approach, we will make our file available at a particular root URI.
Note: WCF 3.5 provides the new Web Programming Model that is made for building REST services. I have observed that doing REST style coding in WCF proves very handy.
Here our return type will be the message. So replace the contents of the ICrossDomainService.cs or .vb with the following. Our ICrossDomainService.cs or ICrossDomainService.vb will look like this.
C#
using System.ServiceModel.Web;
using System.ServiceModel.Channels;
namespace AccessAServiceFromConsoleApplication
{
// NOTE: If you change the interface name "ICrossDomainService" here, you must also update the reference to "ICrossDomainService" in App.config.
[ServiceContract]
public interface ICrossDomainService
{
[OperationContract]
[WebGet(UriTemplate = "ClientAccessPolicy.xml")]
Message ProvidePolicyFile();
}
}
VB.NET
Imports System.ServiceModel.Web
Imports System.ServiceModel.Channels
<ServiceContract()> _
Public Interface ICrossDomainService
<OperationContract(), WebGet(UriTemplate:="ClientAccessPolicy.xml")> _
Function ProvidePolicyFile() As Message
End Interface
WebGet provides a mechanism to map an HTTP GET operation to a specific method. It will look for that method according to the URI specified in UriTemplate. So actually when we will type the <baseaddress> provided in our app.config file, followed by Uri Template string, it will execute this method.
For eg: You can try this here by typing http://localhost:8731/clientaccesspolicy.xml when your application is ready.
Note: For some reason, if you get the "The type or namespace name 'WebGetAttribute' could not be found", make sure you have added a reference to the System.ServiceModel.Web.
Step 8: Our CrossDomainService.cs or .vb will look like this. Here we are reading an Xml file and returning that.
C#
using System.Xml;
using System.ServiceModel.Channels;
namespace AccessAServiceFromConsoleApplication
{
public class CrossDomainService : ICrossDomainService
{
public System.ServiceModel.Channels.Message ProvidePolicyFile()
{
FileStream filestream = File.Open(@"ClientAccessPolicy.xml", FileMode.Open);
// Either specify ClientAccessPolicy.xml file path properly
// or put that in \Bin folder of the console application
XmlReader reader = XmlReader.Create(filestream);
System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader);
return result;
}
}
}
VB.NET
Imports System.IO
Imports System.Xml
Imports System.ServiceModel.Channels
Public Class CrossDomainService
Implements ICrossDomainService
Public Function ProvidePolicyFile() As System.ServiceModel.Channels.Message Implements ICrossDomainService.ProvidePolicyFile
Dim filestream As FileStream = File.Open("ClientAccessPolicy.xml", FileMode.Open)
' Either specify ClientAccessPolicy.xml file path properly
' or put that in \Bin folder of the console application
Dim reader As XmlReader = XmlReader.Create(filestream)
Dim result As System.ServiceModel.Channels.Message = Message.CreateMessage(MessageVersion.None, "", reader)
Return result
End Function
End Class
Note: Don’t forget to paste your ClientAccessPolicy.xml file in the \Bin folder, since in this sample, we have specified to look for the xml file in that location. We can also read the xml file using different methods; like by giving the absolute path, embedding the file etc.
Edit AppConfig to be able to expose the service will test run our Console Application
Step 9: Now we will modify our AppConfig. Do not forget to change the Binding to basicHttpBinding. Here we are specifying the Base address of CrossDomainService as "http://localhost:8731/" and base address of MyService as http://localhost:8731/AccessAServiceFromConsoleApplication/MyService.
If you observe carefully, both the services ‘base address root’ is the same. CrossDomainService will make our Policy file available at http://localhost:8731/clientaccesspolicy.xml". When our Silverlight application will try to access the Service, it will look in that URI as (http://localhost:8731/clientaccesspolicy.xml) and will get an access to it.
Note: We can change the Base Adresses in the app.config file for both the services, but make sure both the base address are pointing to the same root URI, i.e. “http://localhost:portnumber/” is the same.
The app.config will look similar to the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<serviceBehaviors>
<behavior name="AccessAServiceFromConsoleApplication.MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="CrossDomainServiceBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration=
"AccessAServiceFromConsoleApplication.MyServiceBehavior"
name="AccessAServiceFromConsoleApplication.MyService">
<endpoint address="" binding="basicHttpBinding" contract="AccessAServiceFromConsoleApplication.IMyService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress=
/MyService" />
</baseAddresses>
</host>
</service>
<service name="AccessAServiceFromConsoleApplication.CrossDomainService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/" />
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract=
"AccessAServiceFromConsoleApplication.ICrossDomainService" behaviorConfiguration="CrossDomainServiceBehavior"/>
</service>
</services>
</system.serviceModel>
</configuration>
Step 10: The last step is to code the main method and open the service host to test if it is running. Write the following code in Program.cs or Module1.vb
C#
using System.ServiceModel;
namespace AccessAServiceFromConsoleApplication
{
class Program
{
static void Main(string[] args)
{
ServiceHost myserviceHost = new ServiceHost(typeof(MyService));
ServiceHost crossDomainserviceHost = new ServiceHost(typeof(CrossDomainService));
myserviceHost.Open();
crossDomainserviceHost.Open();
Console.WriteLine("Service is Running");
Console.ReadLine();
}
}
}
VB.NET
Imports System.ServiceModel
Module Module1
Sub Main()
Dim myserviceHost As ServiceHost = New ServiceHost(GetType(MyService))
Dim crossDomainserviceHost As ServiceHost = New ServiceHost(GetType(CrossDomainService))
myserviceHost.Open()
crossDomainserviceHost.Open()
Console.WriteLine("Service is Running")
Console.ReadLine()
End Sub
End Module
The WCF Service is now ready. Run your application. If you have followed all the steps correctly up till here, you should see a message ‘Service is Running’ on the console. We will now create a Silverlight Client for accessing this WCF Service.
Create a Silverlight Client Project which will consume this WCF Service
Step 1: Open Visual Studio 2008 > File > New Project > Select the language (C# or VB.NET) > Select ‘Silverlight’ in the Project Types > from the templates, select ‘Silverlight Application’.
Step 2: Type a name (ConsumeWCFFromConsoleApplication) and location for the project and click ok. Choose the first option ‘Add a new Web to the solution for hosting the control’ and the project type as ‘Web Site’ and click OK. You will see that two projects are created: ConsumeWCFFromConsoleApplicationWeb and ConsumeWCFFromConsoleApplication.
Step 3: Now add a Service Reference of your Service .Right Click on Silverlight Project > click Add Service Reference >the ‘Add Service Reference’ dialog will appear. Give the Service URI mentioned in your App.Config of the console application. Paste it in Address Box > click Go.
Add a NameSpace. Here I have given the namespace as ‘ConsoleServiceReference’. Click on Ok (See Image Below)
Note: Make sure that when you are adding a reference to the service or you are running your Silverlight application, your Service is also running i.e. your Console Application should also be running.
Step 4: Now in your Page.Xaml.cs, refer to the Service. To test the data coming from our service, we will add a TextBlock whose ‘Text’ will be populated from our Service.
Page.Xaml will look like this:
<UserControl x:Class="ConsumeWCFFromConsoleApplication.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</UserControl>
The Page.Xaml.cs or Page.Xaml.vb will look like this:
C#
namespace ConsumeWCFFromConsoleApplication
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
ConsoleServiceReference.MyServiceClient consoleClient = new ConsumeWCFFromConsoleApplication.ConsoleServiceReference.MyServiceClient();
consoleClient.DoWorkCompleted += new EventHandler<ConsumeWCFFromConsoleApplication.ConsoleServiceReference
.DoWorkCompletedEventArgs>(consoleClient_DoWorkCompleted);
consoleClient.DoWorkAsync();
}
void consoleClient_DoWorkCompleted(object sender, ConsumeWCFFromConsoleApplication.ConsoleServiceReference
.DoWorkCompletedEventArgs e)
{
if (e.Result != null)
{
TextBlock serviceText = new TextBlock();
serviceText.Text = e.Result;
LayoutRoot.Children.Add(serviceText);
}
}
}
}
VB.NET
Namespace ConsumeWCFFromConsoleApplication
Public Partial Class Page
Inherits UserControl
Public Sub New()
InitializeComponent()
Dim consoleClient As ConsoleServiceReference.MyServiceClient = NewConsumeWCFFromConsoleApplication.ConsoleServiceReference.MyServiceClient()
AddHandler consoleClient.DoWorkCompleted, AddressOf Of ConsumeWCFFromConsoleApplication.ConsoleServiceReference
.DoWorkCompletedEventArgs
consoleClient.DoWorkAsync()
End Sub
Private Sub consoleClient_DoWorkCompleted(ByVal sender As Object, ByVal e As ConsumeWCFFromConsoleApplication.ConsoleServiceReference
.DoWorkCompletedEventArgs)
If Not e.Result Is Nothing Then
Dim serviceText As TextBlock = New TextBlock()
serviceText.Text = e.Result
LayoutRoot.Children.Add(serviceText)
End If
End Sub
End Class
End Namespace
In the code above, when our service call completes, we are creating a TextBlock control and setting the Text property of that TextBlock to the message returned from the service. We finally add the TextBlock to the Children collection of Layout Root.
Run the application and you will be able to access the WCF Service hosted in Console Application using a Silverlight Client. I hope this article was useful and I thank you for viewing it.
3:10 AM by Dilip kakadiya · 0
Friday, October 14, 2011
Create Dashboard with SQL Server Reporting Services (SSRS) 2008
In this article, we will see how to create and deploy a SharePoint Dashboard which contains SQL Server Reporting Services (SSRS) 2008 reports. We will also see some references of how to create a Chart and Tablix report and deploy them to Report Server.
The steps to create a SharePoint Site using a Template Business Intelligence Center remains the same as shown in my previous articleSharePoint Dashboard with Common Filters using PerformancePoint Services 2010. I also assume you have downloaded the Dashboard Designer tool at your end. If you have not yet done so, please check my previous article for the steps.
The dashboard designer tool will help you to design the Data Sources, KPIs, Scorecards, Reports, Filters and Dashboards. The tool is shown below –
On the left hand section, you will see the Workspace Browser. In this window, you will see two settings:
1) Data Connections - It is used for creating a connection with different types of data sources as shown below:
b. For our demonstration, we are going to use Analysis Services.
2) PerformancePoint Content – Using PerformancePoint Content we can design KPIs, Filters, Reports, Dashboards, Indicators and Scorecards as shown below :
Now let’s create two SQL Server reports using SQL Server Business Intelligence Studio as below–
1) Create a Chart report – For creating a chart report please refer to my article titled as SQL Server: Designing Chart Report in SSRS 2008.
2) Creating Tablix Report – For creating a Tablix report please refer to my article titled as SQL Server: Designing Tablix Report in SSRS 2008.
Once you create both the reports, deploy your reports to you report server. To deploy the reports, right click the project in solution explorer and go to properties. In the property dialog box, type the URL of your report server. In my case, the report server is installed on my local instance. So I have mentioned the address as given below –
Now right click again on the project and click on ‘Deploy’ menu. This will deploy the reports to the Report server. To test the deployment, type the following address (without quotes) in your browser. Please note that your address may differ than what I have here.
“http://localhost/Reports”
You will see the deployed reports as shown below –
Now let’s get back to our Dashboard designer tool. Right click ‘PerformancePoint Content’ and then add a new ‘Report’. This will show you a ‘Report Template Dialog box’. Choose ‘Reporting Services’ template as shown below –
Now let’s rename the report with ‘Sales Chart Report’ and configure the report as shown below –
1. Server Mode – you can install reporting services in two different modes –
- Native Mode
- SharePoint Integration Mode
Choose the appropriate mode. In my case, I will be making a choice of ‘Report Center’.
2. Server Name – provide the same path which we provided during the deployment of report to the report server. In my case, it is ‘http://localhost/reportserver’.
3. Report – now let’s browse the report by clicking the ‘Browse’ button. This will show you all the reports which are deployed to report server in a dialog box. Choose the chart report as shown below –
4. Now you can choose the other properties as per your requirements. You can preview the report by clicking a ‘Preview’ button. You preview should look like the one shown below –
Now let’s add one more report which will take the address of our ‘Tablix’ report. Repeat the steps above and choose ‘Tablix’ report and preview the same. It should look similar to the following –
Note: If you have parameters in your report, then you will have to provide the default values to the parameters. If you do not pass the default values, you will not see the preview of a report. It’s a common mistake that devs do.
Rename your report with ‘Sales Tablix Report’. Save both the reports.
Now let’s create a Dashboard. Right click ‘PerformancePoint Content’ and click on New Dashboard. Choose ‘Two Columns’ dashboard template. Add both the SQL Server reports into our dashboard from the ‘Details’ window, as shown below –
Rename the dashboard with ‘SSRS Sales Dashboard’. Now right click ‘SSRS Sales Dashboard’ and click on ‘Deploy to SharePoint’. This will prompt you for the deployment option. Keep the default values.
Once the deployment is successful, you will see the dashboard in our SharePoint Site, as shown below –
Summary – In this article, we have seen how to create and deploy a Dashboard which contains SQL Server Reporting Services 2008 reports. We have also seen how to create Chart and Tablix reports and deploy them to Report Server.
5:13 AM by Dilip kakadiya · 0
Subscribe to:
Posts (Atom)