Monday 10 August 2009

Publish Silverlight 3 on IIS 7.0 / 7.5

Please note: Windows Vista/Server 2008 comes with IIS 7.0 and Windows 7 comes with IIS 7.5, however there are no major changes in publishing your web application and web service. The following steps guide you via process of publishing.

Building/Publishing Web Application in Visual Studio

In the Visual Studio, on your website project, or from the Build menu, select Publish Web Site. In the opened window setup "Target Location" where you'd like to publish the application. Set appropriate check boxes and click OK.

Example:
In the Visual Studio, you have the Silverlight.Web web application and the Silverlight project. Right click on "Silverlight.Web" and select "Publish Web Site" from the content menu. In the opened window, browse for "Target Location" such as "C:\Inetpub\wwwroot\TestApp" and click OK.

Building/Publishing WCF Service in Visual Studio

First of all, please make sure that you've edited the ServiceReferences.ClientConfig file located in your Silverlight project. You need to change client's end point address to the real one. It's essential because your WCF Service won't work.

Example:
Suppose, you're about to publish your WCF Service named TestService.svc to the IIS root directory to the WebService folder, so after successful publishing it can be accessible as http://localhost/WebService/TestService.svc
Your web service worked fine in the development environment because the service references' config file (ServiceReferences.ClientConfig) looked like this:

<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_Service">
<binaryMessageEncoding />
<httpTransport maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647" />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://localhost:49741/SilverlightApplication.Web/TestService.svc"
binding="customBinding" bindingConfiguration="CustomBinding_Service"
contract="wcfService.Service" name="CustomBinding_Service" />
</client>
</system.serviceModel>
</configuration>

So, you need to change <endpoint address from one provided by Visual Studio to the address that represents the web service's web location. In this case, just make this change:

<endpoint address="http://localhost/WebService/TestService.svc"

rest of the line leave as it. By changing this address you setup your web service.

After that, you can continue to publish your web service. In Visual Studio, in Solution Explorer, right click on project's name and select "Publish". In the opened pop-up window browse for target directory. I prefer setup the publish process as following, check these options:
  1. "Delete all existing files prior to publish"
  2. "Copy only files needed to run this application"
  3. "Include files from the App_Data folder" - if you use it

Preparing IIS Environment


After you built your Silverlight application you're probably about to publish it on IIS.

First of all, check the IIS in the Windows Features (e.g. on Windows Vista/7, navigate to Control Panel - Programs and open Turn Windows Features On Or Off). Make sure you have checked the following options:
  • Internet Information Services (recommend all features except FTP if you don't use it)
  • Microsoft .NET Framework 3.5.1 (recommend all features)
If you already have installed the IIS, you can start it (IIS Manager), it can be found in Control Panel - Administrative Tools in Vista or Control Panel - System And Security - Administrative Tools in Windows 7 as Internet Information Services (IIS) Manager.

Adding your Silverlight application

In IIS Manager, right click on your Default Web Page, located under Sites, and Add New Virtual Directory.
In the opened window (Add Virtual Directory) input Alias name and browse for Physical path. You can also specify a user whos credentials will be used to open a web site by clicking on Connect as.... If you're happy with your settings click OK. You have successfully created the virtual directory where your applications resists.

Now, you have to convert this virtual directory to application. To do this, just right click on it and select "Convert To Application" option from the content menu. The application pool should be "Classic .NET AppPool". To set it up click the "Select" button and choose "Classic .NET AppPool" from the drop-down. After then, click "OK" and your website is published on the IIS.

Please follow this simplified example to setup web application in IIS for a project that's been build in C:\Inetpub\wwwroot\TestApp
1. Open IIS
2. Expand Sites
3. Right click on Default Web Page and select "Add New Virtual Directory" option
4. In the opened pop-up window input "Alias" as Test and populate "Physical path" with C:\Inetpub\wwwroot\TestApp and click "OK"
5. Right click on the created virtual directory (Test) and select "Convert To Application" option from the content menu.
6. In the opened pop-up window make sure you have selected by clicking the "Select..." button Classic .NET AppPool in "Application pool". Click "OK".

That's all. Right now, you can check your website. Right click on the created application (Test) select "Manage Application" - "Browse" from the content menu. This will open your website on your default browser.
Right now, your website is also accessible by typing the http://localhost/Test on the browser's URL.

Adding your WCF Service

To add your WCF Service please follow steps described above (Adding your Silverlight application).
Suppose, you've added Service1.svc from C:\Inetpub\wwwroot\Service folder. You can test the accessibility of your service by browsing this URL: http://localhost/webService/Service1.svc
If you successfully installed the service you'll see the service test page, follow the very first link at this page (something like: http://localhost/webService/Service1.svc?wsdl) to test your service - you'll get an XML output.

If everything went smoothly, you've got WSDL output, you'll need to accomplish one simple step to make your service accessible from your web application. Because it's a separate service (built outside the Silverlight application solution), you have to add clientAccessPolicy.xml and crossDomain.xml files to your web root directory. In these files you can specify, who and from which domain can access (consume) your web service. If you avoid these files or wrongly setup them your service can no longer be accessible.

Example: Suppose, your web root directory is C:\Inetpub\wwwroot and you'd like that your web service to be accessible by everyone from any domain. All you need is copy the clientAccessPolicy.xml and crossDomain.xml to your C:\Inetpub\wwwroot folder.

Example of clientAccessPolicy.xml that allow accessing the service for everyone:

<?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 include-subpaths="true" path="/"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>

Example of crossDomain.xml to allow access web service from any domain:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>

If you've created your WCF Service within your Silverlight application by adding new item (Silverlight-enabled WCF Service) you don't need to create and copy clientAccessPolicy.xml and crossDomain.xml files to your web root directory. Visual Studio automatically creates security and accessible roles for your Silverlight application. In this case this WCF Service is accessible only from your Silverlight application.

2 comments:

Dhananjay said...

Nicely explained post. It has saved many hours.

Thanks!
Dhananjay

Unknown said...

Thanks :)

Vahag