Deploying WCF and Silverlight Applications on IIS 5 and 7






4.92/5 (6 votes)
Configuration and deployment of WCF services used by a Silverlight client on IIS 5 and IIS 7.
Contents
- Prerequisites
- Creation of a WCF service
- Configuring a WCF application on IIS 5
- 3.1 Check for IIS in the server machine
- 3.2. Hosting a WCF service on IIS 5
- 3.3. Testing the WCF service
- Configuring a WCF application on IIS 7
- 4.1. Installing IIS 7 in the machine
- 4.2. Hosting the WCF service on IIS 7
- 4.3. Testing the WCF service
- Configure the security parameters for Silverlight
1. Prerequisites
In this article I would like to dive into the configuration and deployment of WCF services used by a Silverlight client on IIS 5 and IIS 7.
The prerequisites for this article are as follows:
1.1 Server side
You should have installed the following:
- Windows XP or Window 7
- IIS installed
- .NET Framework 3.5 or 4.0 (recommended)
1.2 Client side
Silverlight 2, 3, or 4 (preferably the right version of Silverlight; if the right version is not used, the Silverlight application would be non-functional).
1.3. Local Area Network
A Local Area Network is essential to test our application.
2. Creation of a WCF Service
To create a WCF service, you have to define the Service Contract that has to be exposed to the client. An example of a service contract interface for a calculator service is shown below:
Here the interface needs to be marked as ServiceContract
and the methods should be marked with the OperationContract
attribute, respectively.
If the methods within the interface have parameters to be passed, the parameter types should be either serializable or Data Contracts.
Implement the service in Calculator.svc.cs.
Create a web.config containing the address, binding, and endpoint of the service. If the Silverlight client has to be able to access the service, then basicHttpBinding
or pollingDuplexBinding
, or NetTcpBinding
has to be supported.
Note: PollingDuplex
and NetTcp
bindings are not supported in IIS 5.
Create a .svc file to include the implementation of the server.
Now the WCF service is ready to be hosted and used by the client.
3. Configuring a WCF application on IIS 5
3.1 Check for IIS in the server machine
To check the existence of IIS, click on Start -> Run and type inetmgr in the command window. If IIS is installed, the IIS Management Console opens up.
3.2. Hosting a WCF service on IIS 5
Change the ASP.NET configuration to the version of the .NET Framework in which the WCF service is built.
Create a virtual directory with the name CalculatorService.
Create a folder App_code and drop CalculatorService.cs and CalculatorService.svc.cs in it. Drop the .NET assemblies in the bin folder.
Drop the web.config and CalculatorService.svc files in the virtual directory CalculatorService.
3.3. Testing the WCF service
Type the URL of the WCF service with the path of the SVC file in the virtual directory http://localhost:64692/CalculatorService/CalculatorService.svc and the page shown below appears:
4. Configuring a WCF application on IIS 7
4.1. Installing IIS 7 in the machine
To install IIS, click on Start -> Run and type appwiz.cpl in the command window and press Enter. Now click on Turn Windows features on/off.
Enable Internet Information Services and ensure that all the subfolders under IIS are checked and click OK. This will install IIS on your machine.
Also enable Microsoft .NET Framework 3.5.1 and ensure that WCF HTTP Activation and WCF Non-Http Activation are enabled and click OK.
We have to register aspnet_regiis.exe to inform IIS about WCF extensions. On the command prompt, go to the following path: WINDOWS\Microsoft.NET\Framework\v4.0.30319>asp.net_regiis.exe -i –enable and press Enter.
To register the WCF extensions, on the command prompt, go to the following path: Windows Microsoft.NET\Framework\\V3.0\Windows Communication Foundation\ServiceModelReg.exe –i and press Enter.
To check the existence of IIS, click on Start -> Run and type inetmgr in the command window and press Enter. The IIS Management window will open up. Double click on Application Pools. Set the Framework version to 4.0 or 3.5 and set the pipeline either to Integrated or Classic mode and click OK as shown in the figure below.
In IIS, browse to MIME Types and check for verbs and extensions like .xaml, .xbap, .xap as these extensions are used by Silverlight applications.
Note: In IIS 7.0 onwards, these MIME types are already available so we need not have to add them but in case we are using a lower version, then we need to add them separately.
4.2. Hosting the WCF service on IIS 7
Change the ASP.NET configuration to the version of the .NET Framework in which the WCF service is built.
Create a virtual directory with the name CalculatorService.
Create a folder App_code and drop CalculatorService.cs and CalculatorService.svc.cs in it. Drop the .NET assemblies in the bin folder.
Drop the web.config and patientService.svc files in the virtual directory.
4.3. Testing the WCF service
Type the URL of the WCF service with the path of the svc file in the virtual directory http://localhost:64692/CalculatorService.svc, and the page shown below appears.
To view the WSDL, click on the link in Fig. 9.
5. Configure the security parameters for Silverlight
If you need to use the Silverlight client, you need to add two more files to the virtual directory: clientaccesspolicy.xml and crossDomain.xml. These policies will be the same for IIS 5 and IIS 7.
In the allow-from
tag, we can configure http-request-headers such as SOAP, content-type for supporting MIME.
We could also restrict access to a set of trusted sites by including them in the <domain uri>
tag in the allow-from
tag. This will help us overcome Denial of Service attacks from fake clients. If you want to allow access from only one other domain,
such as http://mysite.com, replace the <domain uri="*"/>
line within the <allow-from>
element
of the clientaccesspolicy.xml file above with the line <domain uri="http://mysite.com"/>
.
If a * is used with the <domain-Uri>
tag, then all the sites are allowed to access the service. As mentioned above, you will also require
a ClientAccessPolicy
to access the Silverlight application. Place ClientAccessPolicy
and CrossDomainPolicy
in the Virtual Directory,
and now the application is ready.