【问题标题】:ConfigurationManager.ConnectionStrings[0].Name return LocalSqlServerConfigurationManager.ConnectionStrings[0].Name 返回 LocalSqlServer
【发布时间】:2014-04-13 11:35:46
【问题描述】:

我有一个 WCF Web 服务,我使用 Ajax (JQuery) 调用 WCF 方法。

我用WcfTestClient.exe 测试了我的网络服务,它运行良好。

但是当我使用Jquery 调用 Web 服务方法时,出现错误(对象引用未设置为对象的实例)。

我进行调试,我的 **ConfigurationManager.ConnectionStrings[0].Name** : *LocalSqlServer* 中有这个。

我的数据库键是 RMS 而不是 LocalSqlServer。

我的解决方案中有 2 个项目,即 WCF 和一个应用程序控制台,用于关闭 Web 服务。

这是我的解决方案

这是我的 web.config :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="RMS" connectionString="Data Source=192.168.40.137;Initial Catalog=RMS_Database;Persist Security Info=True;User ID=****;Password=****"
  providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxRequestLength="16384"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />  
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    <bindings>
      <basicHttpBinding>
        <binding name="basicHttp" allowCookies="true" maxReceivedMessageSize="20000000" maxBufferSize="20000000" maxBufferPoolSize="20000000">
          <readerQuotas maxDepth="32" maxArrayLength="200000000" maxStringContentLength="200000000"/>
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

我创建了一个.asmx 网络服务,并成功调用了JQuery。 我认为我的解决方案没有使用正确的web.config. 如何解决这个问题?

【问题讨论】:

    标签: c# jquery asp.net web-services wcf


    【解决方案1】:

    您的第一篇文章可能只是缺少以下条目:

    <clear />
    

    在以下条目之前:

     <add name="RMS" connectionString="Data Source=192.168.40.137;Initial Catalog=RMS_Database;Persist Security Info=True;User ID=****;Password=****"
      providerName="System.Data.SqlClient" />
    

    描述:如果没有清除条目,则读取默认 machine.config 连接字符串。此条目称为 LocalSqlServer。 Clear 避免了这种情况。

    【讨论】:

    • 完美,如果我们在任何情况下都需要LocalSqlServer,请告诉我?
    【解决方案2】:

    我解决了这个问题。

    我还在 GettingStartedHost 项目的 app.config 中添加了连接字符串,现在它运行良好。

    这是正确的方法吗?

    【讨论】:

    • 如果 web 配置仅用于连接字符串,您应该删除它或将其重命名为 App.config。
    【解决方案3】:

    将 Web.config 重命名为 App.config 以使配置管理器正常工作。

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 2011-10-14
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 2014-07-14
      • 1970-01-01
      相关资源
      最近更新 更多