使用IIS时出现System.Data.SqlClient.SqlCommand.ExecuteReader()错误

问题描述 投票:0回答:1

我正在尝试在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上。

希望有人可以提供帮助!

c# sql-server soap asmx
1个回答
0
投票

之所以如此,是因为您每次都在打开连接但没有关闭它。完成查询后,调用cerrar()方法。我建议您最终还是调用cerrar()方法,因此它将始终被调用。从Visual Studio运行时,它没有任何问题,因为打开的连接很少。

请参阅this答案,将非常有帮助

© www.soinside.com 2019 - 2024. All rights reserved.