简体   繁体   English

Webservice + consumer c#error服务器无法处理请求。 --->连接已经打开

[英]Webservice+consumer c# error Server was unable to process request. ---> The connection is already open

i have a webservice and a comsumer, the webservice has its methode where it returns data from a mysql database. 我有一个web服务和一个comsumer,webservice有它的方法,它从mysql数据库返回数据。 in the comsumer i called 在我打电话的消息中

WebService.Service1 Service = new WebService.Service1();        

in the beginning (not within a methode) when the consumer starts asking for data it will be 20 requests within 10 minutes first 15-18 requests worked perfectly but the last few times it returns the error 在开始时(不在一个方法中)当消费者开始询问数据时,它将在10分钟内提出20个请求,首先15-18个请求完美地工作但最后几次它返回错误

Server was unable to process request. 服务器无法处理请求。 ---> The connection is already open. --->连接已经打开。

I hope i provided enough information like this, i rather not post the code. 我希望我提供了这样的足够信息,我宁愿不发布代码。

This is the methode of the webservice: 这是webservice的方法:

        public string GetAnswer(string Question, string Option1, string Option2, string Option3, string Option4)
    {
        string connstring = "Server=Server;Port=3306;Database=DB;UID=User;password=pw;";
        MySqlConnection conn = new MySqlConnection(connstring);
        MySqlCommand command = conn.CreateCommand();

        command.CommandText = "SELECT * FROM `tbl` where `Question` = '" + Question + "' LIMIT 1";
        conn.Open();

        MySqlDataReader reader = command.ExecuteReader();
        if (reader.HasRows)
        {
            string TheAnswer = "";
            while (reader.Read())
            {
                string question = reader["Question"].ToString();
                string answer = reader["Answer"].ToString();
                if (Option1.Equals(answer))
                    TheAnswer = Option1;
                if (Option2.Equals(answer))
                    TheAnswer = Option2;
                if (Option3.Equals(answer))
                    TheAnswer = Option3;
                if (Option4.Equals(answer))
                    TheAnswer = Option4;
            }
            conn.Close();
            conn.Dispose();
            return TheAnswer;
        }
        else
        {
            MySqlCommand command2 = conn.CreateCommand();
            command.CommandText = "Insert Into `new` (`Question`, `Answer1`,`Answer2`,`Answer3`,`Answer4`) VALUES ('" + Question + "','" + Option1 + "','" + Option2 + "','" + Option3 + "', '" + Option4.Replace("~", " ") + "')";
            conn.Open();
            command.ExecuteNonQuery();

            conn.Close();
            conn.Dispose();
            return "Error: Question is unknown, saving the question to get it answered.";
        }
    }

You have conn.open() in your else statement, and the connection was already opened before that. 你的else语句中有conn.open() ,之前已经打开了连接。 You should probably consider using the using statement: 您应该考虑使用using语句:

using (MySqlConnection conn = new MySqlConnection(connstring))
{
  using (MySqlCommand command2 = conn.CreateCommand())
  {
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从C#连接到mysql,连接已打开错误 - Unable to connect to mysql from c#, connection already open error 连接已打开错误C# - Connection already open error c# C# WebService:服务器无法处理请求 - C# WebService: server can't process the request 例外:服务器无法处理请求。 --->数据为空 - Exception: Server was unable to process request. ---> Data is Null 如何打开与C#中已在使用的SQL Server数据库的连接? - How to open a connection to a SQL Server database that is already in use in c#? C#WCF“ System.Net.WebException:远程服务器返回错误:(400)错误的请求。” - C# WCF “System.Net.WebException: The remote server returned an error: (400) Bad Request.” 服务器无法处理请求。 --->请求失败,HTTP状态为401:未经授权 - Server was unable to process request. ---> The request failed with HTTP status 401: Unauthorized SoapException Server无法在MVC站点中处理ASMX Web服务上的请求 - SoapException Server was unable to process request on ASMX webservice in MVC site 打开与 COM1 端口的连接,即使它已被 C# 中的另一个进程打开 - Open a connection with COM1 port even if its already opened by another process in C# System.Web.Services.Protocols.SoapException:服务器无法处理请求。 ---> System.ArgumentNullException - System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ArgumentNullException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM