05.10.08

Migrating a Silverlight v2 Beta 1 application that uses SQL Server 2005, WCF, LINQ, and ASP.NET v3.5, on the cheap, to GoDaddy.com

Posted in LINQ, SQL Server 2005, Silverlight, Visual Studio, WCF, XAML, XBAP at 8:36 pm by TalynOne



Recently I was involved in a project that used Silverlight v2 beta 1, SQL Server 2005, and a WCF service using LINQ in an ASP.NET v3.5 project. A link to an article describing this project will follow soon.

When looking for the most affordable web host that would support this application, fellow blogger & Silverlight aficionado Alan Cobb suggested GoDaddy.

GoDaddy's Windows Economy Plan seemed to fit the bill, it gave me access to 1 SQL Server 2005 database, ASP.NET v3.5 support, running under IIS v6 or v7 for around $9 for two months. That's a price that's hard to beat, but would it work? The short answer is yes, the long answer is that it requires some teeth pulling. I'll explain the gotchas, tips, and modifications necessary to successfully deploy such an application under GoDaddy's Windows Economy web hosting plan.



Silverlight MIME Types

When setting up a new web hosting account with GoDaddy, choose IIS 7, not IIS 6. With a new GoDaddy IIS 7 plan all the necessary MIME types should already be registered for you.

You may be asking, wait a minute, Silverlight is a client side technology, it should be supported on any web server, right? Well that's mostly correct. It is supported on any web server, as long as that server has the Silverlight required MIME types registered. When the Silverlight plug-in requests certain assets (such as .xaml or .xap files) it requires the web server hosting those files to have the correct MIME types registered for those file extensions. Every web server worth a grain of salt lets you configure MIME types. Though every web host, may not give you direct access or the ability to do so.

The Silverlight related MIME types include:

Extension MIME Type
.xap application/x-silverlight-app
.xaml application/xaml+xml
.xbap application/x-ms-xbap


The .xaml MIME type is mostly used by Silverlight v1.0 applications.

The .xap file is where all the Silverlight v2 assets are stored. It's stored in the "ClientBin" folder in a Silverlight v2 application. It's actually just a ZIP file, you can open it in any .ZIP capable application to see what's inside. The files inside typically include a .xaml file (the UI), Silverlight assemblies (.dll files), and embedded resources (.jpg,.wmv, etc..).

The .xbap MIME type is not necessary for Silverlight applications, but could be useful if you wish to deploy WPF XBAP applications.

When I initially created my GoDaddy account, I set it up as an IIS 6 account. I'm much more familiar with IIS v6 than v7, so I chose IIS v6 with the thinking that it's better to go with the devil you know. The IIS v6 account did not have the .xap MIME type registered. The symptom was that the Silverlight plug-in loads fine, but the application shows up as a blank white canvas, as the plug-in can't/didn't download the .xap asset properly. Tip to Microsoft: If the .xap asset can't be downloaded/processed properly in a timely manner, please show an error message, a blank white canvas is a bit disconcerting.

If you already have or must have IIS 6 under GoDaddy (if you need FrontPage Extensions, ColdFusion support, or have other reasons such as this or this), the following are your choices:
  1. Contact GoDaddy support and have them add the necessary MIME types for you. Some have had good luck with this, I did not.

    <Bad Customer Experience Rant Start>

    First, you should know, any support ticket under the Economy plan takes up to 24 hours to process (usually fewer than 12 hours). I guess you get what you pay for. Second, when I put in a ticket for them to add the MIME types they responded by e-mailing me information on how to do so myself using a web.config under IIS 7. That's all fine and dandy, but I was on a IIS 6 account, and the ability to configure MIME types using web.config files is only supported under IIS 7. So I got my support ticket re-opened and escalated. They responded that the MIME type would be added within 12 hours. After 12 hours, when my Silverlight application still did not work, I called in again and they informed me that the techs said the MIME type has been configured. When I explained that it had not been done properly, the rep, in not so many words, said, it is configured properly, I can't talk to the IIS admin directly, and that I don't know how MIME types work under GoDaddy. At this point I said screw it, I'm not going to get anywhere with this doofus, I told him to migrate me to IIS 7. Migrating from IIS 6 to IIS 7 under GoDaddy means canceling out my current IIS 6 web hosting account, getting a refund, and signing up for a new hosting account, this time choosing IIS 7 when setting up the account. After this was done, I was able to run simple Silverlight applications just fine without any additional configuration.

    </ Bad Customer Experience Rant End>

  2. Brad Abrams suggests a workaround where you rename the .xap file to .zip, and change the reference to the new extension in the application's .xaml file, and in the .aspx/.html file that hosts the Silverlight control.
If you're on another web host that's running IIS 7, but doesn't give you direct access to IIS MMC, you can configure mime types using a web.config file using this guide. Don't do this on a GoDaddy IIS7 account! Placing these directives in a Godaddy IIS 7 account, which already has these MIME type configured, will cause the server to spit out server configuration errors. Under IIS 6, these directives are ignored.

If you're hosting on your own IIS server, or on a different web server that gives you full access to the IIS 6/7 MMC, read this link for information on how to configure the MIME types.



WCF Services on web hosts with Multiple IIS Identities (GoDaddy)

WCF services hosted on IIS can only have one base address. Under some hosting configurations, such as GoDaddy's shared Windows Economy Plan, multiple base addresses exist. When attempting to access WCF services on an IIS with multiple base addresses, an exception similar to the following occurs:

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.
Parameter name: item
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.
Parameter name: item

The solution, using a combination of information found here and here, is to create a custom service factory and override the "CreateServiceHost" method. For Godaddy, I need to have it return the second base address found. The code below only returns the second base address when more than one exists, otherwise I return the first base address. This way I can share the same code base when using my local IIS/ASP.NET server, and GoDaddy IIS servers You may need to modify this code to return a different/custom base address depending on your application's needs.

Simply add the following class, as a sister class, in the service code behind file (yourservice.svc.cs):
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{                       
    // If more than one base address exists then return the second 
    // address, otherwise return the first address
    if (baseAddresses.Length > 1)
    {
        return new ServiceHost(serviceType, baseAddresses[1]);
    }
    else
    {
        return new ServiceHost(serviceType, baseAddresses[0]);
    }            
}


Then edit the markup in the .svc file (yourservice.svc), by adding the following attribute to the "ServiceHost" tag: (You can edit this file in any text editor, or by right-clicking on the file in Visual Studio 2008 solution explorer, and choosing the "View Markup" menu item):
Factory="YourWebProject_Web.CustomHostFactory"


WCF .svc files and GoDaddy

For some reason GoDaddy's IIS 7 server configuration doesn't have the buildprovider extension configured for the .svc file extension. This is easy enough to fix. Simply go to the ASP.NET project application folder and open up the web.config file, under the "<compilation>" tag, which is under the "<system.web>" tag, add the following directive:

<buildProviders>
    <add extension=".svc" type="System.ServiceModel.Activation.ServiceBuildProvider, 
         System.ServiceModel, Version=3.0.0.0, Culture=neutral,
         PublicKeyToken=b77a5c561934e089"/>
</buildProviders>




Setting up SQL Server on GoDaddy

This page has information how to create a SQL server database on the GoDaddy account. When you set up the database, you don't need the "DSN" option, as that's for older ODBC connections, which we won't be using. You should check/include the "ASP Schema" option. Don't worry, if you missed these options when you set up the database, you can go edit the database properties through GoDaddy's interface to add/remove these options as needed.

Exporting local SQL Server database to GoDaddy

If you wish to export a local SQL Server 2005 database schema/data to GoDaddy follow the infromation provided here & here.

Configuring LINQ/ASP.NET/WCF to connect to the GoDaddy SQL Server

  1. Find the database host address, database name, user id, and password for the SQL Server you wish to connect to. Information on how to do that is here
  2. Edit the web.config and find the "<connectionStrings>" section, change the "connectionString" attribute to:
    connectionString="Server=SQL_HOSTADDRESS; Database=SQL_DBNAME; User ID=SQL_USER_NAME; Password=SQL_DB_PASSWORD; Trusted_Connection=False"




Setting up IIS Application folder on GoDaddy

For ASP.NET application to work correctly, the folder that it's contained in must be set as an application folder. To do this on GoDaddy:
  1. Logon to the web hosting account ("Hosting & Servers" menu, "My Hosting Account" sub menu item).
  2. Click on the "Manage Account" link next to the domain you wish to configure.á
  3. Under the "Content" menu item, choose the IIS Settings sub menu item.
  4. Click on the "Create" button and set the "Directory Name:" textbox value to the same name as folder you wish to place the ASP.NET application in.
  5. Make sure to select the "Set Application Root" option.
  6. Press the "OK" button.
Configuring a Cross-Domain Policy File (clientaccesspolicy.xml)

If you wish to debug a Silverlight application, which accesses an on-line server's WCF service, through a locally running instance of Visual Studio, you must configure a cross-domain policy file. You may also need a cross-domain policy file configured if the base address returned by "CreateServiceHost" is not interpreted as a local address by the WCF service (covered in the "WCF Services on web hosts with Multiple IIS Identities (GoDaddy)" section above).

For a full explaination of how to create cross-domain policy files, this article is a good read.

Karen Corby's three part article on site of origin, cross domain communication, and configuring a cross domain policy file is also a good read on the details of how Silverlight handles communication.

Uploading the project

I always found it surprising that there's very little documentation detailing which files are actually need for a deployed application to run. When you look at the solution file you will see many files (.sln, .csproj, .aspx, .dll, .dbml, .cs, .svc, folders, etc...). But to deploy an application only a few of these files and folders are needed. The folders/files that actually needed for an application to run include:
  1. Any .aspx and/or .html files that the application uses. A Silverlight web application project usually creates a default.aspx and default.html, which aren't actually used/needed for anything. You probably will want to rename the startup .aspx or .html file to default.aspx or default.html. When the startup file is named "default.aspx/default.html" then users can access the application without specifying the application file name (http://www.yoursite.com/yourappfolder/ instead of http://www.yoursite.com/yourappfolder/yourapp.aspx).
  2. The WCF .svc file, this file associates a service with its implementation and is the means for IIS to create a ServiceHost for you. For further reading, this article details hosting and consuming WCF services.
  3. The "web.config" file in the web project folder. Information on the web.config file can be found in many places, including here, here, and here.
  4. The "bin" folder (ASP.NET assemblies) in the web project folder.
  5. The "Clientbin" folder (Silverlight assemblies/assets) in the web project folder.
  6. Any assets referenced by Silverlight, HTML, and ASP.NET sources, such as images, videos, etc...


Reconfiguring Silverlight WCF Service Reference Location

If the Silverlight application is currently pointed to a locally hosted WCF service location, and you wish to point it to an on-line/different WCF service location, the process is simple.
  1. Go to the Visual Studio Solution Explorer, and right-click on the server reference (under the "Server References" folder)
  2. Choose the "Configure Service Reference" menu item.
  3. Set the "Address:" text box value to the URL of the WCF .svc file (http://www.yourserver.com/yourappfolder/yourservice.svc).
Debugging Silverlight/WCF