Delta Engine Blog

AI, Robotics, multiplatform game development and Strict programming language

Fixing the Sys.Res. namespaceContainsObject error with ASP.NET Ajax+Silverlight

When you try to implement a Silverlight project into an existing ASP.NET Ajax
website you will just click Add Silverlight link to a specific directory in your
website (don't spam everything into your main directory). Then you might want
to use same the html code from your test sites in the ASPX page, like this:

<head runat="server">
  <title>Some title</title>
  <script type="text/javascript" src="Silverlight.js"></script>
  <script type="text/javascript" src="Default.aspx.js"></script>
</head>


And the SilverlightControlHost somewhere in the body (in the form).
You also might want to move the body onload method to a div panel in case you
are using a master page or you want multiple silverlight controls:

        <div id="SilverlightControlHost"
            onload="document.getElementById('SilverlightControl').focus()">
            <script type="text/javascript">
                createSilverlight();
            </script>
        </div>


You might be able to compile and start that without any problems, BUT if you
already have a lot of ASP.NET Ajax code you might end up with this error (and
a bunch more, most of them with Sys.Res in the error description).

Line: 967
Error: Sys.Res.namespaceContainsObject is null or not an object



The problem is that both Silverlight and some generated ASP.NET Ajax javascript
files use the same javascript object (Sys.Res), but overwrite it and disable the
functionality that is needed. You won't find much information on that with
google, but the solution is very simple! Just put the scripts directly into
your ScriptManager of the site like this and remove the scripts from the header:

        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Scripts>
                <asp:ScriptReference Path="Silverlight.js" />
                <asp:ScriptReference Path="Default.aspx.js" />
            </Scripts>
        </asp:ScriptManager>

        
Hope this helps and saves you some time.