Intermittent NotFound error with Prism Modules +MEF when running Silverlight out of browser

Topics: Prism v4 - Silverlight 4
Jan 19, 2011 at 9:05 PM

I get an intermittent error (happens over 70% of the time) module load error (NotFound) when running Silverlight out of browser while launching the application; then rerunning (sometimes it takes 4-5 attempts) the application launches without an error.  I had experienced this on occasion when running in browser, but it was far more rare. 

Best analogy I can come up with the symptoms is that it is similar to executing JavaScript on a web page before the page (DOM) and/or JavaScript has fully been loaded in the browser.  The problem does not seem to occur when running without debugging (hard to say that is the case definitively since the problem is intermittent).

I have been through the copy local references of all my modules many times (copy local = true in main xap, copy local = false in modules) ... so please don't reply asking if I have checked that.  Second, I have seen seemingly related posts on MEF for "offline" out of browser, but in my case I am running online only.

Here is corresponding error detail / call stack:

 

Microsoft.Practices.Prism.Modularity.ModuleTypeLoadingException was unhandled by user code

Message=Failed to load type for module NurseModule.

 

If this error occurred when using MEF in a Silverlight application, please ensure that the CopyLocal property of the reference to the MefExtensions assembly is set to true in the main application/shell and false in all other assemblies.

 

Error was: The remote server returned an error: NotFound..

ModuleName=NurseModule

StackTrace:

       at

Microsoft.Practices.Prism.Modularity.ModuleManager.HandleModuleTypeLoadingEr

ror(ModuleInfo moduleInfo, Exception exception)

       at

Microsoft.Practices.Prism.Modularity.ModuleManager.IModuleTypeLoader_LoadMod

uleCompleted(Object sender, LoadModuleCompletedEventArgs e)

       at

Microsoft.Practices.Prism.MefExtensions.Modularity.MefXapModuleTypeLoader.Ra

iseLoadModuleCompleted(LoadModuleCompletedEventArgs e)

       at

Microsoft.Practices.Prism.MefExtensions.Modularity.MefXapModuleTypeLoader.Ra

iseLoadModuleCompleted(ModuleInfo moduleInfo, Exception error)

       at

Microsoft.Practices.Prism.MefExtensions.Modularity.MefXapModuleTypeLoader.Ha

ndleDownloadCompleted(DeploymentCatalog deploymentCatalog, AsyncCompletedEventArgs e)

       at

Microsoft.Practices.Prism.MefExtensions.Modularity.MefXapModuleTypeLoader.De

ploymentCatalog_DownloadCompleted(Object sender, AsyncCompletedEventArgs e)

       at

System.ComponentModel.Composition.Hosting.DeploymentCatalog.OnDownloadComple

ted(AsyncCompletedEventArgs e)

       at

System.ComponentModel.Composition.Hosting.DeploymentCatalog.HandleOpenReadCo

mpleted(Object sender, OpenReadCompletedEventArgs e)

       at

System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)

       at System.Net.WebClient.OpenReadOperationCompleted(Object arg)

InnerException: System.Net.WebException

       Message=The remote server returned an error: NotFound.

       StackTrace:

           at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback

beginMethod, Object state)

           at

System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult

asyncResult)

           at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)

           at System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult

result)

      InnerException: System.Net.WebException

           Message=The remote server returned an error: NotFound.

           StackTrace:

                 at

System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult

asyncResult)

                at

System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>

b__4(Object sendState)

                 at

System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object

sendState)

           InnerException:

 

 

Developer
Jan 20, 2011 at 2:32 PM
Edited Apr 19, 2011 at 4:06 PM

Hi,

The exception you're experiencing could be because of a Cross-Domain issue. From the information you have provided, the ModuleTypeLoadingException is most likely being thrown because a call to a web service is failing (returning a 404-not found error). As that exception is being thrown, MEF is unable to compose the module, thus making the ModuleManager throw a module type loading exception.

You might find the following resources useful to deal with Cross-Domain issues in Silverlight, as well as other errors in a WCF or web service:

Also, you should take into account that it's possible that Silverlight displays a not found error when there is an exception in a web service or WCF service (regardless of whether that exception is or not a not found exception), so one possibility would be to debug your service to see if there is anything that could cause intermitent exceptions.

After further research, we’ve found that the cause of the intermittent issue you’ve mentioned is not related to Prism, as we’ve been able to reproduce it using two simultaneous web client requests for a local file in a non-Prism Silverlight OOB application. Additionally, we’ve only been able to reproduce it using Cassini. When we deployed the solution to IIS, we didn’t face the issue anymore. You can find the sample we’ve created here.

We found out that the necessary elements to trigger the intermittent error in Prism are having two modules load simultaneously via XapModuleTypeLoader (used when you use Unity or other DI container) or MefXapModuleTypeLoader (used when you use MEF), as both of them internally use a WebClient to download the modules. Therefore, the issue is caused by neither Unity nor MEF.

One possible workaround for you to avoid experiencing this issue would be to host your application in a different place, such as IIS Express. Another possibility would be to alter the timing of your module loading, so that you don’t make two simultaneous requests. For example, if you set your second module to be loaded after the initialization of the first one, the intermittent issue shouldn’t appear. To illustrate this:

    public class Module1 : IModule

    {

       private IModuleManager moduleManager;

 

        [ImportingConstructor]

        public Module1(IModuleManager manager)

        {

            this.moduleManager = manager;

        }

 

         public void Initialize()

        {

            moduleManager.LoadModule("Module2");

        }

    }

 

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jan 21, 2011 at 11:17 PM

Thanks for the suggestions; however I get the error at a point when no WCF service is being called by the modules.

In fact, given the two different modules I am currently working with I can load either one individually without any error, but when I load them both, I get the error.

This seems to point to a duplicate export or assembly reference across the two modules; however as I mentioned earlier the problem is intermittent and I have been through the assembly list multiple times.

Aug 18, 2011 at 3:47 PM
Edited Aug 18, 2011 at 3:47 PM

[oops, wrong forum]