在SqlServer中插入的Http Listener vs Web Api

时间:2018-10-05 05:58:55

标签: c# asp.net-mvc asp.net-web-api httplistener

我正在使用一个非常简单的软件。我唯一需要的是通过手机在本地计算机上(通过wifi)发送查询,然后通过应用程序执行我的查询。

我找到了两种方法,但是我不知道哪种方法更符合我的需求。 我将描述的两种方法都在计算机上本地运行。

方法1 Http侦听器:

HttpListener listener;
Thread t;

private void Form1_Load(object sender, EventArgs e)
{

        t = new Thread(new ThreadStart(MyThreadMethod));
        t.IsBackground = true;
        listener = new HttpListener();
        listener.Prefixes.Add("http://192.168.0.214:8282/");
        listener.Start();
        t.Start();

}
void MyThreadMethod()
{
    while (true)
    {
         IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
            result.AsyncWaitHandle.WaitOne(30000);
    }
}

public void ListenerCallback(IAsyncResult result)
{

        string Response = "";
        try
        {


            HttpListener listener = (HttpListener)result.AsyncState;
            HttpListenerContext context = listener.EndGetContext(result);
            var SqlData = new StreamReader(context.Request.InputStream,context.Request.ContentEncoding).ReadToEnd();

            string connectionString = "Server=(local);Database=xxxx;User id=admin;password=1234;";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (SqlCommand ReceiveData = new SqlCommand(SqlData, connection))
                    ReceiveData.ExecuteNonQuery();
                Response = "OK";

            }
            HttpListenerResponse response = context.Response;
            string responseString = Convert.ToString(Response);
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            response.ContentLength64 = buffer.Length;
            System.IO.Stream output = response.OutputStream;
            output.Write(buffer, 0, buffer.Length);
            output.Close();
  }
  catch (Exception ex)
  {

      using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\LogFile.txt"))
      {
          file.WriteLine(Convert.ToString(ex));
      }

此方法在我的本地计算机上以* .exe或cmd或Windows服务的形式运行。

方法2 Asp Net Web Api :(我正在创建一个名为InsertData的控制器)

  public class InsertDataController : ApiController
  {

    // POST: api/InsertData
    public string Post(HttpContent httpContent)
    {
        var readAsStringAsync = httpContent.ReadAsStringAsync();
        var SqlData = readAsStringAsync.Result;

        string connectionString = "Server=(local);Database=xxxx;User id=admin;password=1234;";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand ReceiveData = new SqlCommand(SqlData, connection))
                ReceiveData.ExecuteNonQuery();

        }
        return "OK";
    }

我注意到asp net比http侦听器慢了几毫秒。但是用较短的代码似乎更稳定。另外,asp net通过IIS7和httplistener作为* .exe或cmd或Windows服务运行。 但是,哪种方法更稳定,并且在我看来更好?我只需要发送简单的字符串就可以了。

0 个答案:

没有答案