c# - ConfigurationManager.ConnectionStrings[0].Name 返回 LocalSqlServer

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

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

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

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

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

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

我的解决方案中有 2 个项目,WCF 和一个应用程序控制台,它们将返回 Web 服务。

这是我的解决方案

enter image description here

这是我的 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。 如何解决这个问题?

最佳答案

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

<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 避免了这种情况。

关于c# - ConfigurationManager.ConnectionStrings[0].Name 返回 LocalSqlServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22298929/

相关文章:

javascript - 使用 javascript 更改 Bootstrap 3 导航栏的背景不透明度

javascript - jquery获取输入值

asp.net - 发布后 asp.net 中的母版页问题

asp.net 身份验证 - 使用 web.config 中的凭据 - 问题

c# - TFS 2010 : Getting list of changeset Ids

c# - 带有列表框的 foreach 的奇怪问题

javascript - 如何使用 Javascript 和 Ajax 更改 div 背景颜色?

c# - Asp.net 菜单导航事件类属性

C# HttpClient 添加 "User-Agent" header 显示为几个不同的 header

c# - .NET 4.5 UserPrincipal.FindByIdentity (System.DirectoryServices.AccountManagement) 中的错误