【问题标题】:Calling WCF Service Methods from test Website throws error从测试网站调用 WCF 服务方法会引发错误
【发布时间】:2010-01-06 10:52:21
【问题描述】:

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

现在,当我从 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> 

【问题讨论】:

    标签: wcf service


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的回复,我只是缺少添加数据库连接字符串。但是现在当我在 Windows 服务配置文件中添加数据库连接字符串时,我仍然遇到同样的错误 :-( 我已经编辑了我的帖子并在上面添加了我的 WCF 库和 Windows 服务的配置文件。任何 cmets?
    • 我们需要查看配置 NHibernate 会话工厂的代码。
    • 编辑了我的帖子以包括会话工厂配置和 hibernate.cfg.xml
    • 是的,我在 Windows 服务安装文件夹中手动复制了 hibernate.cfg.xml。另外,因为我的机器上有所有这些应用程序,所以它应该可以在运行时访问 xml 文件。
    • 不确定,但我检查了 Windows 服务登录属性,它设置为:本地系统帐户。我尝试将其更改为 ASPNET 用户。但是当我尝试为 ASPNET 用户启动服务时,它说.. 由于登录失败,服务无法启动...不知道我错过了什么:(
    【解决方案2】:

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

    以下链接可能会有所帮助:

    会话状态模式

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 2011-09-12
      • 2011-03-02
      • 1970-01-01
      相关资源
      最近更新 更多