WCF:System.ServiceModel.Diagnostics.TraceUtility的类型初始化程序引发了异常

时间:2017-11-20 10:32:11

标签: c# wcf nhibernate

我想测试我的WCF服务,但是我收到了这个错误:

  

System.TypeInitializationException: 'The type initializer for 'System.ServiceModel.Diagnostics.TraceUtility' threw an exception.'

我将我的WCF服务引用到我的测试类(WSTester)。

这就是内部异常:

**ConfigurationErrorsException: Unrecognized configuration section compile.**

我是WCF的新手。我认为我的App.config文件是错误的,但我不知道如何实现它。我在浏览器中尝试过它。它工作,但不是在控制台应用程序。

这是我的WSTester中的测试程序:

public class Program
{
    static void Main(string[] args)
    {
        Service1Client client = new Service1Client();
        Customer c = new Customer();
        c.Name = "dsfsfsdf";
        c.SurName="asdass";
        c.Age = 12;
        client.CreateCustomer(c);
    }
}

这是我的App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <compile debug="true">
  </compile>
  <system.serviceModel>
    <serviceHostingEnvironment minFreeMemoryPercentageToActivateService="99" />
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" />
      </webHttpBinding>
   </bindings>
    <client>
      <endpoint address="http://192.168.0.xx:7767/Service1.svc"
        binding="webHttpBinding"
        contract="WorkWS.IService1" behaviorConfiguration="webhttp" />
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webhttp">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Web服务下的Web.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>

    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
      <property name="connection.connection_string">Data Source=.\SQLEXPRESS;;Initial Catalog=FirstProject; User Id = aykut ; password= ****</property>
      <property name="show_sql">true</property>
    </session-factory>
  </hibernate-configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <add key="HBM_ASSEMBLY" value="BusinessEntities" />
  </appSettings>
  <system.web>

    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
    <httpRuntime maxRequestLength="524288" />
  </system.web>

  <system.serviceModel>
    <services>
      <service name="WebService.Service1">
        <endpoint address="" behaviorConfiguration="webHttpBehavior" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="WebService.IService1" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
    </bindings>

    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior>
          <!-- Meta veri bilgilerini açığa çıkarmaktan kaçınmak için, dağıtımdan önce aşağıdaki bilgileri false yapın -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- Hatalarda hata ayıklamak amacıyla özel durum ayrıntıları almak için aşağıdaki değeri true yapın.  Özel durum bilgilerini açığa çıkarmaktan kaçınmak için dağıtımdan önce false yapın -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>


    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>

  <runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

      <dependentAssembly>

        <assemblyIdentity name="Iesi.Collections" publicKeyToken="aa95f207798dfdb4" culture="neutral" />

        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />

      </dependentAssembly>

    </assemblyBinding>

  </runtime>
</configuration>

最后这是我的Service1.svc:

    [WebInvoke(Method = "POST",
    UriTemplate = "CreateCustomer",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]
    public Customer CreateCustomer(Customer customer)
    {
        if (customer == null)
        {
            throw new ArgumentNullException("FAIL");
        }
        CustomerManager cm = new CustomerManager();
        cm.CreateCustomer(customer);

        return customer;
    }

2 个答案:

答案 0 :(得分:1)

编译器抱怨

  <compile debug="true">
  </compile>
app.config中的

元素无效。只需删除它。

顺便说一下,它是从哪里来的?

答案 1 :(得分:0)

当Service1.svc?wsdl写入URL

    <wsdl:service name="Service1"/>
    </wsdl:definitions>