ConfigurationManager.ConnectionStrings [0] .Name返回LocalSqlServer

Jay*_*yce 5 c# asp.net wcf jquery web-services

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

我测试了我的Web服务WcfTestClient.exe并且运行良好.

但是,当我调用Web服务方法时Jquery,我有一个错误(对象引用未设置为对象的实例).

我调试,我有我的**ConfigurationManager.ConnectionStrings[0].Name** : *LocalSqlServer*.

我的dataBase密钥是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>
Run Code Online (Sandbox Code Playgroud)

我创建了一个.asmxWeb服务,然后我成功了JQuery.我认为我的解决方案没有使用正确的web.config.如何解决这个问题?

小智 12

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

<clear />
Run Code Online (Sandbox Code Playgroud)

在以下条目之前:

 <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" />
Run Code Online (Sandbox Code Playgroud)

描述:没有clear条目,将读取默认的machine.config连接字符串.此条目称为LocalSqlServer.清除避免这种情况.


Jay*_*yce 1

我解决了这个问题。

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

这是正确的方法吗?