2
votes

This is my situation: I am using Visual Studio 2015 with .NET Framework 4.6.1 on a Win 10 64-bit machine for building a WCF Service.

In my solution I have several different project types, mainly plain C# Class Library(class libraries), some C++-dll references, and of course the WCF Service Library itself.

Before I continue I would like to state that this is my first date with WCF ever...

This SO question is addressing a similar problem, Where is the startup method of a WCF Service?, but since I'm dealing with a WCF Service Library a couple of years later when the original question was asked, and the fact that the original question is not using the same project type, I believe that (?) the answer there is not adaptable for my problem. Not when just debugging at least?

In my solution, I have set my WCF Service Library-project as StartUp project. When hitting the F5-key (for Start), this is what I get:

WcfSvcHost encountered a critical error an must exit. This may be caused by invalid configuration file. Please inspect additional information below for detail.

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)

Ok, normally, in a plain C# Console Application, I would have set up a try-catch-block in the Main()-method in order to examine my problem. But my WCF Service Library-project doesn't seem to have such.

My question: How can I find out what the LoaderException property is?

EDIT 1: I tried @user497745's answer (both proposals) and the first one helped me:

    • I fired up Visual Studio > Debug > Windows > Exception Settings
    • and provided the following settings:
    • When starting my error prone WCF project I still got the very same message as before
    • When trying another option that @user497745 suggested, disabling Visual Studio > Debug > Options > Debugging > General > Just My Code, I got closer to my Exception:
    • I tried the second proposal with diagnostics tracing by adding sections in my App.config file for the WCF Class Library.
    • This is part of my App.config:
      <system.serviceModel>
          <diagnostics>
            <messageLogging
               logEntireMessage="true"
               logMalformedMessages="false"
               logMessagesAtServiceLevel="true"
               logMessagesAtTransportLevel="false"
               maxMessagesToLog="3000"
               maxSizeOfMessageToLog="2000"/>
          </diagnostics>
          <behaviors>
            <serviceBehaviors>
              <behavior name="HemsamaritenServiceBehavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
              </behavior>
            </serviceBehaviors>
          </behaviors>
          <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
          <services>
            <!-- This section is optional with the new configuration model
                 introduced in .NET Framework 4. -->
            <service name="WCFServiceLibrary.HemsamaritenDuplexService"
                     behaviorConfiguration="HemsamaritenServiceBehavior">
              <host>
                <baseAddresses>
                  <add baseAddress="http://localhost:8000/Hemsamariten/service"/>
                </baseAddresses>
              </host>
              <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/Hemsamariten/service  -->
              <endpoint address=""
                        binding="wsDualHttpBinding"
                        contract="WCFServiceLibrary.Interfaces.IHemsamaritenDuplexService" />
              <!-- the mex endpoint is exposed at http://localhost:8000/Hemsamariten/service/mex -->
              <endpoint address="mex"
                        binding="mexHttpBinding"
                        contract="IMetadataExchange" />
            </service>
          </services>
        </system.serviceModel>
      
      <system.diagnostics>
          <sources>
            <source name="System.ServiceModel" switchValue="All"
              propagateActivity="true">
              <listeners>
                <add name="xml" />
              </listeners>
            </source>
            <source name="System.ServiceModel.MessageLogging" switchValue="All">
              <listeners>
                <add name="xml" />
              </listeners>
            </source>
          </sources>
          <sharedListeners>
            <add initializeData="C:\Users\haunsTM\Desktop\WinService\debuglog.svclog" type="System.Diagnostics.XmlWriterTraceListener"
              name="xml" />
          </sharedListeners>
          <trace autoflush="true" />
      </system.diagnostics>
    • I started my application running Visual Studio 2015 as Administrator
    • Unfortunately my output directory, C:\Users\haunsTM\Desktop\WinService\debuglog.svclog, was empty after the run. Unfortunately, somehow, the Build checkbox in the Configuration Manager had been unchecked for my executable. When checking it, I got a long log message!
1
I don't think you can really capture the exact moment the exception is thrown directly without using some tricks in Visual Studio or capturing the exception information via diagnostics. I posted an answer below with a couple of options.Ayo I

1 Answers

1
votes

So, if you're trying to break in the debugger to catch the error as it happens, I would recommend a different approach.

Either:

  1. Go to DEBUG > Exceptions and select Common Language Runtime Exceptions > System.Reflection and check the exact exception type you're getting. I'm not sure if you also need to un-check "Enable Just my Code" in DEBUG > Options. And perhaps check "Enable .NET Framework source stepping." Then when the exception occurs, Visual Studio should break and allow you to see the exception details like you would any exception.

  2. Add diagnostics tracing by adding the below to you App.config file for the WCF Class Library. Then try to start the library, and after you get the error, stop the debugger and open up the generated tracefile (which is located in whatever path and filename you chose below) in the SvcTraceViewer. You can open Visual Studio Developer Command Prompt and launch it from there.

    <system.serviceModel>
        <diagnostics>
          <messageLogging
             logEntireMessage="true"
             logMalformedMessages="false"
             logMessagesAtServiceLevel="true"
             logMessagesAtTransportLevel="false"
             maxMessagesToLog="3000"
             maxSizeOfMessageToLog="2000"/>
        </diagnostics>
    </system.serviceModel>
    <system.diagnostics>
        <sources>
          <source name="System.ServiceModel" switchValue="All"
            propagateActivity="true">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
          <source name="System.ServiceModel.MessageLogging" switchValue="All">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
        </sources>
        <sharedListeners>
          <add initializeData="[SOME_PATH]\[SOME_FILENAME].svclog" type="System.Diagnostics.XmlWriterTraceListener"
            name="xml" />
        </sharedListeners>
        <trace autoflush="true" />
    </system.diagnostics>