异常自主机WCF服务 - “System.ServiceModel.Diagnostics.Trac

2019-10-20 05:22发布

我有以下情况例外,当我试图端口现有的基于IIS的REST服务自托管之一。

对于“System.ServiceModel.Diagnostics.TraceUtility”的类型初始值引发异常。

我在下面的自承载WCF REST服务端口我现有的代码,前成功测试在此给出的示例代码。

我的内部异常如下。

现在注释掉后整<configSections>标签,它抛出另一个异常:

该合同名称“SelfHostedRESTService.IService”无法通过该服务“服务”实现合同的名单上找到。

这里是我的配置文件:

<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>

  <!--<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
  </configSections>-->

  <!--<log4net>
    <appender type="log4net.Appender.RollingFileAppender" name="DebugLog">
      <file value="C:\\TestProj\\Info.txt" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
    <appender type="log4net.Appender.RollingFileAppender" name="RestServiceLog">
      <file value="C:\\TestProj\\Error.txt" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
    <appender type="log4net.Appender.RollingFileAppender" name="bmitWarningLog">
      <file value="C:\\TestProj\\PingResult.txt" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
    <logger name="DebugLoggerClass">
      <level value="DEBUG" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1024KB" />
      <staticLogFileName value="true" />
      <param value="DEBUG" name="Threshold" />
      <appender-ref ref="DebugLog" />
    </logger>
    <logger name="SelfHostedRESTService">
      <level value="ERROR" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1024KB" />
      <staticLogFileName value="true" />
      <param value="ERROR" name="Threshold" />
      <appender-ref ref="RestServiceLog" />
    </logger>
    <logger name="WarningLoggerClass">
      <level value="WARN" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1024KB" />
      <staticLogFileName value="true" />
      <param value="WARN" name="Threshold" />
      <appender-ref ref="bmitWarningLog" />
    </logger>
  </log4net>-->

  <appSettings/>
  <connectionStrings/>
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpSoap" />
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>

    <compilation debug="true">
      <assemblies>
        <add assembly="System.Core, Version=4.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>

    <authentication mode="Windows"/>

    <pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </controls>
    </pages>
    <httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
    </httpHandlers>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>
    <identity impersonate="false" />
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v4.0"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
    </compilers>
  </system.codedom>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </modules>
    <handlers>
      <remove name="WebServiceHandlerFactory-Integrated"/>
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </handlers>
  </system.webServer>

  <system.serviceModel>

    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IService" closeTimeout="10:01:00" openTimeout="10:01:00" receiveTimeout="10:10:00" sendTimeout="10:01:00"
                 bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <reliableSession ordered="true" inactivityTimeout="10:10:00" enabled="false" />
        </binding>
      </wsHttpBinding>
    </bindings>

    <services>
      <service name="SelfHostedRESTService.Service" behaviorConfiguration="SelfHostedRESTService.RestServiceBehavior">
        <endpoint address="SelfHostedRESTService.Service" binding="webHttpBinding" contract="SelfHostedRESTService.IService" behaviorConfiguration="web">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="SelfHostedRESTService.RestServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

  </system.serviceModel>

</configuration>

下面是我的界面:

namespace Contracts
{
    [ServiceContract]
    public interface IService
    {
        [XmlSerializerFormat]
        [OperationContract]
        [WebGet]
        XmlDocument postGeneralXMLDocument(Stream strInput);

        [OperationContract]
        [WebInvoke]
        string PostMessage(string inputMessage);
    }
}

为什么抛出此异常?

Answer 1:

根据您的实际内部异常,

Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element.

的问题的原因是<startup>..</startup>部分中,如果<configSections>存在于web.config中它必须是根元素的第一个子节点。 移动<startup>..</startup>后段<configSections>将解决您的问题。

  1. 取消注释<configSections>元素。

  2. 移动<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>下面部<configSections>...</configSections>部分。


更新

对于例外"The contract name 'SelfHostedRESTService.IService' could not be found in the list of contracts implemented by the service 'Service'."

请更正使用的名称空间,它在界面和使用的web.config命名空间不匹配。

在合同: namespace Contracts

在web.config中: SelfHostedRESTService.IService

要么,

  • 更改名称空间SelfHostedRESTService在接口

或可替代

  • 使用Contracts.IService在web.config中。

更新2异常"Operation 'postGeneralXMLDocument' in contract 'IService' has a query variable named 'strInput' of type 'System.IO.Stream', but type 'System.IO.Stream' is not convertible by 'QueryStringConverter'. Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'." 看来你使用的是一些变量类型的名为“strInput“System.IO.Stream”,这直接不能被序列化。 尝试序列化到串,然后在另一端反序列化。

一个有用的链接是: http://www.techques.com/question/1-5241661/WCF-Complex-JSON-INPUT-Error-(not-convertible-by-QueryStringConverter)

希望能帮助到你。



文章来源: Exception in self host wcf service - The type initializer for 'System.ServiceModel.Diagnostics.TraceUtility' threw an exception
标签: c# wcf rest