首页 文章

在调试`WCF服务库'时,我在哪里可以设置try-catch块(即哪里是入口点/ Main() - 方法)

提问于
浏览
1

This is my situation: 我在Win 10 64位计算机上使用Visual Studio 2015和.NET Framework 4.6.1来构建WCF服务 .

在我的解决方案中,我有几种不同的项目类型,主要是普通的C# Class Library (类库),一些 C++ -dll引用,当然还有 WCF Service Library 本身 .

在我继续之前,我想说这是我与WCF的第一次约会......

这太问题是解决了类似的问题,Where is the startup method of a WCF Service?,但因为我负责的一个 WCF Service Library 几年后,当原来的问题是问,原来的问题是没有使用相同的项目类型的事实,我认为, (?)答案不适应我的问题 . 不是只在调试时至少?

在我的解决方案中,我将 WCF Service Library -project设置为StartUp项目 . 当按下F5键(开始)时,这就是我得到的:

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)

好吧,通常,在一个普通的C# Console Application 中,我会在 Main() -method中设置一个try-catch-block来检查我的问题 . 但我的 WCF Service Library 项目似乎没有这样的 .

My question: 如何找出 LoaderException 属性是什么?

编辑1:我尝试了@ user497745的答案(两个提案),第一个帮助了我:

  • 我启动了Visual Studio>调试> Windows>异常设置

  • 并提供了以下设置:

  • 当我启动容易出错的WCF项目时,我仍然收到与之前相同的消息

  • 当尝试@ user497745建议的另一个选项,禁用Visual Studio>调试>选项>调试>常规> Just My Code时,我更接近我的异常:

  • 我通过在我的App.config文件中为 WCF Class Library 添加了部分,尝试了第二个带有诊断跟踪的提议 .

  • 这是我的 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>
  • 我启动了以管理员身份运行Visual Studio 2015的应用程序

  • 不幸的是我的输出目录C:\ Users \ haunsTM \ Desktop \ WinService \ debuglog.svclog在运行后为空 . 不幸的是,不知何故,Configuration Manager中的Build复选框已取消选中我的可执行文件 . 检查时,我得到一条长日志消息!

1 回答

  • 1

    所以,如果你试图打破调试器来捕捉错误,我会推荐一种不同的方法 .

    或者:

    • 转到DEBUG> Exceptions并选择Common Language Runtime Exceptions> System.Reflection并检查您获得的确切异常类型 . 我不确定你是否还需要在DEBUG>选项中取消选中“启用我的代码” . 也许请检查“启用.NET Framework源代码” . 然后,当异常发生时,Visual Studio应该中断并允许您像任何异常一样查看异常详细信息 .

    • 通过将以下内容添加到WCF类库的App.config文件中来添加诊断跟踪 . 然后尝试启动库,在收到错误后,停止调试器并在SvcTraceViewer中打开生成的跟踪文件(位于下面选择的路径和文件名中) . 您可以打开Visual Studio Developer命令提示符并从那里启动它 .

    <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>
    

相关问题