WcfSVCHost encountered a critical error and must exit. This may be caused by invalid configuration file

7.3k views Asked by At
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.Assembly.GetTypes()
   at Microsoft.Tools.SvcHost.ServiceHostHelper.LoadServiceAssembly(String svcAssemblyPath)

How can I loop through LoaderExceptions property to see what errors have occured as it is not hitting any service and give me this error before it runs any code?

Thanks

2

There are 2 answers

0
Leo On

I managed to fix the problem. One of my project was set to x86 and others were set to Any CPU. Changing all to Any CPU fixed the problem for me.

Thanks

1
Katie Kilian On

The solution was different for me. This may be obvious information to people who are even passingly familiar with WCF development. But in the hopes that it will help other novices struggling with the same problem, here is what I figured out.

It turned out the app I was debugging didn't need WcfSvcHost.exe at all. WcfSvcHost.exe is for self-hosted WCF applications. This app used services exposed as an endpoint on a web application.

What finally clued me in to this was this: First, this screen would show up:

Screenshot of error message "WcfSvcHost encountered a critical error and must exit."

I noticed this shows up as a separate process for WcfSvcHost.exe in the Windows Task Manager. This explained why I couldn't get the debugger to break even when I adjusted Exception Settings to break when System.Reflection.ReflectionTypeLoadException was thrown. Visual Studio wasn't breaking because it was not attached to the WcfSvcHost.exe process. It was attached to the IISExpress.exe process that was running my app. But the WcfSvcHost.exe process was throwing the exception.

When I clicked the OK button on the error message above, the WcfSvcHost.exe process would exit and no longer appear in Task Manager. But the app was still running fine. So clearly whatever is happening isn't needed. A quick check with another developer confirmed that the app didn't require self-hosted WCF services.

For some reason, Visual Studio was starting WcfSvcHost.exe anyway. And that finally led me to this answer. WCF class library projects can be configured to launch WcfSvcHost.exe whenever debugging starts.

The answer is to right-click on each WCF class library project, choose Properties, and click on the "WCF Options" tab. Then uncheck Start WCF Service Host when debugging another project in the same solution.

Screenshot of WCF class library Properties page, WCF Options tab

You must do this for all WCF class libraries in your solution. I wasn't sure which ones were which, so I just looked at the Properties for each class library and fixed the ones that had the WCF Options tab.