从测试网站调用WCF服务方法会引发错误

时间:2010-01-06 10:52:22

标签: wcf service

我在Fluent NHibernate中实现了WCF服务库,并在Windows服务下托管 另外,我有一个添加了服务引用的WebSite。

现在,当我从WebSite调用WCF服务方法时,出现以下错误:

[FaultException`1: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

* Database was not configured through Database method.
]
  System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +7596735
  System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +275
  TeamworksReportService.ITemplateService.ListTemplatesByTemplateType(Int32 userId, TemplateType templateType) +0
  TeamworksReportService.TemplateServiceClient.ListTemplatesByTemplateType(Int32 userId, TemplateType templateType)

有什么想法吗?

WCF服务中的App.Config:

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

  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
        propagateActivity="true">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="C:\WCF Service Logs\app_tracelog.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelTraceListener" traceOutputOptions="DateTime, Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
  </system.diagnostics>

  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTcp" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000">
          <readerQuotas maxDepth="500" maxStringContentLength="50000000" maxArrayLength="50000000"
                        maxBytesPerRead="50000000" maxNameTableCharCount="50000000" />
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>

    <services>
      <service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
        name="ReportingComponentLibrary.TemplateReportService">
        <endpoint address="TemplateService" binding="netTcpBinding" bindingConfiguration="netTcp"
          contract="ReportingComponentLibrary.ITemplateService"></endpoint>
        <endpoint address="ReportService" binding="netTcpBinding" bindingConfiguration="netTcp"
          contract="ReportingComponentLibrary.IReportService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" ></endpoint>

        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8001/TemplateReportService" />
            <add baseAddress="http://localhost:8181/TemplateReportService"  />
          </baseAddresses>
        </host>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ReportingComponentLibrary.TemplateServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

服务配置文件:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="connection.connection_string" connectionString="Server=dev01\sql2005;Initial Catalog=TeamWorksReports;User Id=twr;Password=manager2;" />
  </connectionStrings>

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
        propagateActivity="true">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="C:\WCF Service Logs\app_tracelog.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelTraceListener" traceOutputOptions="DateTime, Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
  </system.diagnostics>

  <system.serviceModel>

    <bindings>
      <netTcpBinding>
        <binding name="netTcp" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000">
          <readerQuotas maxDepth="500" maxStringContentLength="50000000" maxArrayLength="50000000"
                        maxBytesPerRead="50000000" maxNameTableCharCount="50000000" />
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ReportingComponentLibrary.TemplateServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
        name="ReportingComponentLibrary.TemplateReportService">

        <endpoint address="TemplateService" binding="netTcpBinding" bindingConfiguration="netTcp"
          contract="ReportingComponentLibrary.ITemplateService"></endpoint>

        <endpoint address="ReportService" binding="netTcpBinding" bindingConfiguration="netTcp"
          contract="ReportingComponentLibrary.IReportService"/>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" ></endpoint>

        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8001/TemplateReportService" />
            <add baseAddress ="http://localhost:8181/TemplateReportService" />
          </baseAddresses>
        </host>
      </service>
    </services>

  </system.serviceModel>
</configuration>

会议工厂:

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
            {
                var configuration = new Configuration();
                configuration.Configure(@"E:\Source\ResourceTechniques.Applications.TemplateReportingService\ReportingService\bin\Debug\hibernate.cfg.xml");

                _sessionFactory = Fluently.Configure(configuration)
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TemplateMap>())
                    .BuildSessionFactory();
            }

            return _sessionFactory;
        }
    }

hibernate.cfg.xml中:

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Server=dev01\sql2005;Initial Catalog=TeamWorksReports;User Id=twr;Password=manager2;</property>
    <property name="show_sql">true</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
  </session-factory>
</hibernate-configuration> 

2 个答案:

答案 0 :(得分:2)

这与WCF无关 - 这是一个NHibernate问题。看起来您可能试图在Web.config文件中配置数据库连接字符串,而不是Windows服务的App.config,这是它需要的位置?

如果您使用NHibernate XML配置文件进行配置,是使用Windows服务而不是Web应用程序部署的吗?您尝试运行的版本是否可以访问代码中XML文件的(硬编码!)路径?运行服务的帐户是否具有(硬编码!)路径的权限?

最好的办法是确保hibernate.cfg.xml始终位于同一文件夹中的二进制文件旁边,并从调用Configure中删除路径参数。

答案 1 :(得分:0)

好像你使用SQL Server来托管你的会话,你还没有在你的wcf服务中为会话状态配置sql server。

以下链接可能有所帮助:

会话状态模式

http://msdn.microsoft.com/en-us/library/ms178586.aspx

HOW TO:配置SQL Server以存储ASP.NET会话状态 http://support.microsoft.com/kb/317604