【问题标题】:System.Data.SqlClient.SqlCommand.ExecuteReader() error when using IIS使用 IIS 时出现 System.Data.SqlClient.SqlCommand.ExecuteReader() 错误
【发布时间】:2020-09-22 05:45:21
【问题描述】:

我正在尝试在 IIS 上使用我的 asmx soap 服务。当我将它与 Visual Studio 一起使用时,它工作正常,但是当我在 IIS 上使用它发帖时,我得到了这个答案:

<faultcode>soap:Server</faultcode>
    <faultstring>System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed.
       at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task&amp; task, Boolean&amp; usedCache, Boolean asyncWrite, Boolean inRetry)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
       at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
       at System.Data.SqlClient.SqlCommand.ExecuteReader()
       at PLATAFORMAS.Servicio1.Login(Autenticacion value, String ServiceName) in C:\Users\jofre\Downloads\ServicioWeb - Roldan - Cajias\PLATAFORMAS\Servicio1.asmx.cs:line 67
       at PLATAFORMAS.Servicio1.CiudadBuscar(String ciudad) in C:\Users\jofre\Downloads\ServicioWeb - Roldan - Cajias\PLATAFORMAS\Servicio1.asmx.cs:line 260
       --- End of inner exception stack trace ---</faultstring>

要使用它,我必须发送带有 UserName 的标头和 Pass 以及服务查询。在 Visual Studio 中工作正常,这在 IIS 上失败。我在网上查了很多解决方案,但没有一个有效。

这是登录方式:

public static Boolean Login(Autenticacion value,string ServiceName)
{
    if (value == null)
    {
        return false;
    }

    ConexionBD conn = new ConexionBD();
    conn.abrir();

    string verificacion = string.Format("Select ID from Usuario where Usuario ='{0}' and Password = '{1}'", value.UserName, value.Password);
    SqlCommand commando = new SqlCommand(verificacion, conn.conectarbd);
    string cadena = "";
    SqlDataReader read = commando.ExecuteReader();
    Auditoria(ServiceName, value.UserName, GetLocalIPAddress());

    while (read.Read())
    {
        cadena = read["ID"].ToString();
    }

    if (cadena != "")
    {
        return true;
    }
    else
    {
    return false;
    }
}

这是我的 CONEXIONBD 课程:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace PLATAFORMAS
{
    public class ConexionBD
    {
        string cadena = "Data Source =JOFRERLAP\\SQLEXPRESS;Database=Api;Trusted_Connection=True;MultipleActiveResultSets=True";
        public SqlConnection conectarbd = new SqlConnection();

        public ConexionBD()
        {
            conectarbd.ConnectionString = cadena;
        }

        public void abrir()
        {
            try
            {
                conectarbd.Open();
                Console.WriteLine("CONEXION CORRECTA");
            }
            catch (Exception ex)
            {
                Console.WriteLine("SE MURIO :( " + ex.Message);

            }
        }
        public void cerrar()
        {
            conectarbd.Close();
        }
    }

}

我的数据库在 SQL SERVER 上。

希望有人能帮忙!

【问题讨论】:

  • 你在哪里打开连接?
  • 我有一个名为 ConexionBD 的附加类,有我的连接字符串和 SQL 连接。我只是分开做,只是有更多关于使用它的信息。但我认为这很好,因为如果我使用 IIS Express(Visual Studio 2019 中包含的那个)启动它,它工作正常

标签: c# sql-server soap asmx


【解决方案1】:

问题是因为您打开连接但不是每次都关闭它。完成查询后调用cerrar() 方法。我建议你在 finally 中调用cerrar() 方法,所以它会一直被调用。从 Visual Studio 运行时不会出现任何问题,因为您打开的连接非常少。

参考this的回答,真的很有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 2020-01-28
    相关资源
    最近更新 更多