C# VS2019 WebService创建与发布,并部署到Windows Server 2012R

2019-12-04 来源: Jeremy.Wu 发布在  https://www.cnblogs.com/jeremywucnblog/p/11982155.html

前言

  上一次数据库灾备和性能优化后,数据库专家建议,在不扩容的情况下,客户端不能再频繁的扫描数据库了!一句惊醒梦中人,因为我也发现数据库越来越卡了,自从上个项目上线后,就出现了这个情况。后来分析其原因,发现客户端每3秒中扫描一次数据库,一共5000+客户端,可想而知,频繁扫描严重影响到数据库性能。所以,我这边准备采用三层架构来解决这个问题,将现有的业务逻辑全部移植到WebService服务器上,客户端通过WebService服务,进而实现对数据库的操作。

  此篇只是记录一下,便于后续的学习,不足之处请指正。

创建WebService服务

  • 新建ASP.NET Web解决方案,命名为WebServiceTest,框架选择.NET Framework 4,如下图;

  • 添加一个Web服务,命名为WebServiceOracleTest,如下图;

  • 开始写一些基础Helper类;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Data;
 using System.Configuration;
 using Oracle.ManagedDataAccess.Client;
 using System.Text;
 using System.IO;

 namespace WebServiceTest
 {
     public class OracleHelper
     {
         //连接字符串
         public static string oraConnStr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=XXX)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XXX)));Persist Security Info=True;User ID=XXX;Password=XXX";

         #region Oracle数据库操作通用方法
         /// <summary>
         /// 测试数据库连接是否正常
         /// </summary>
         /// <param name="strConn"></param>
         /// <returns></returns>
         public static bool CheckOracleConnect()
         {
             try
             {
                 OracleConnection conn = new OracleConnection();
                 conn.ConnectionString = oraConnStr;
                 conn.Open();
                 return true;
             }
             catch
             {
                 return false;
             }
         }

         /// <summary>
         /// 执行数据库非查询操作,返回受影响的行数
         /// </summary>
         /// <param name="connectionString">数据库连接字符串</param>
         /// <param name="cmdType">命令的类型</param>
         /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
         /// <param name="cmdParms">命令参数集合</param>
         /// <returns>当前查询操作影响的数据行数</returns>
         public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
         {
             OracleCommand cmd = new OracleCommand();
             using (OracleConnection conn = new OracleConnection(connectionString))
             {
                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                 int val = cmd.ExecuteNonQuery();
                 cmd.Parameters.Clear();
                 return val;
             }
         }

         /// <summary>
         /// 执行数据库事务非查询操作,返回受影响的行数
         /// </summary>
         /// <param name="transaction">数据库事务对象</param>
         /// <param name="cmdType">Command类型</param>
         /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
         /// <param name="cmdParms">命令参数集合</param>
         /// <returns>当前事务查询操作影响的数据行数</returns>
         public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
         {
             OracleCommand cmd = new OracleCommand();
             PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
             int val = cmd.ExecuteNonQuery();
             cmd.Parameters.Clear();
             return val;
         }

         /// <summary>
         /// 执行数据库非查询操作,返回受影响的行数
         /// </summary>
         /// <param name="connection">Oracle数据库连接对象</param>
         /// <param name="cmdType">Command类型</param>
         /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
         /// <param name="cmdParms">命令参数集合</param>
         /// <returns>当前查询操作影响的数据行数</returns>
         public static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
         {
             if (connection == null)
                 throw new ArgumentNullException("当前数据库连接不存在");
             OracleCommand cmd = new OracleCommand();
             PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
             int val = cmd.ExecuteNonQuery();
             cmd.Parameters.Clear();
             return val;
         }

         /// <summary>
         /// 执行数据库查询操作,返回OracleDataReader类型的内存结果集
         /// </summary>
         /// <param name="connectionString">数据库连接字符串</param>
         /// <param name="cmdType">命令的类型</param>
         /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
         /// <param name="cmdParms">命令参数集合</param>
         /// <returns>当前查询操作返回的OracleDataReader类型的内存结果集</returns>
         public static OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
         {
             OracleCommand cmd = new OracleCommand();
             OracleConnection conn = new OracleConnection(connectionString);
             try
             {
                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                 OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                 cmd.Parameters.Clear();
                 return reader;
             }
             catch
             {
                 cmd.Dispose();
                 conn.Close();
                 throw;
             }
         }

         /// <summary>
         /// 执行数据库查询操作,返回DataSet类型的结果集
         /// </summary>
         /// <param name="connectionString">数据库连接字符串</param>
         /// <param name="cmdType">命令的类型</param>
         /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
         /// <param name="cmdParms">命令参数集合</param>
         /// <returns>当前查询操作返回的DataSet类型的结果集</returns>
         public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
         {
             OracleCommand cmd = new OracleCommand();
             OracleConnection conn = new OracleConnection(connectionString);
             DataSet ds = null;
             try
             {
                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                 OracleDataAdapter adapter = new OracleDataAdapter();
                 adapter.SelectCommand = cmd;
                 ds = new DataSet();
                 adapter.Fill(ds);
                 cmd.Parameters.Clear();
             }
             catch
             {
                 throw;
             }
             finally
             {
                 cmd.Dispose();
                 conn.Close();
                 conn.Dispose();
             }

             return ds;
         }

         /// <summary>
         /// 执行数据库查询操作,返回DataTable类型的结果集
         /// </summary>
         /// <param name="connectionString">数据库连接字符串</param>
         /// <param name="cmdType">命令的类型</param>
         /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
         /// <param name="cmdParms">命令参数集合</param>
         /// <returns>当前查询操作返回的DataTable类型的结果集</returns>
         public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
         {
             OracleCommand cmd = new OracleCommand();
             OracleConnection conn = new OracleConnection(connectionString);
             DataTable dt = null;

             try
             {
                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                 OracleDataAdapter adapter = new OracleDataAdapter();
                 adapter.SelectCommand = cmd;
                 dt = new DataTable();
                 adapter.Fill(dt);
                 cmd.Parameters.Clear();
             }
             catch
             {
                 throw;
             }
             finally
             {
                 cmd.Dispose();
                 conn.Close();
                 conn.Dispose();
             }

             return dt;
         }

         /// <summary>
         /// 执行数据库查询操作,返回结果集中位于第一行第一列的Object类型的值
         /// </summary>
         /// <param name="connectionString">数据库连接字符串</param>
         /// <param name="cmdType">命令的类型</param>
         /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
         /// <param name="cmdParms">命令参数集合</param>
         /// <returns>当前查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>
         public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
         {
             OracleCommand cmd = new OracleCommand();
             OracleConnection conn = new OracleConnection(connectionString);
             object result = null;
             try
             {
                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                 result = cmd.ExecuteScalar();
                 cmd.Parameters.Clear();
             }
             catch
             {
                 throw;
             }
             finally
             {
                 cmd.Dispose();
                 conn.Close();
                 conn.Dispose();
             }

             return result;
         }

         ///    <summary>
         ///    执行数据库事务查询操作,返回结果集中位于第一行第一列的Object类型的值
         ///    </summary>
         ///    <param name="trans">一个已存在的数据库事务对象</param>
         ///    <param name="commandType">命令类型</param>
         ///    <param name="commandText">Oracle存储过程名称或PL/SQL命令</param>
         ///    <param name="cmdParms">命令参数集合</param>
         ///    <returns>当前事务查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>
         public static object ExecuteScalar(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
         {
             if (trans == null)
                 throw new ArgumentNullException("当前数据库事务不存在");
             OracleConnection conn = trans.Connection;
             if (conn == null)
                 throw new ArgumentException("当前事务所在的数据库连接不存在");

             OracleCommand cmd = new OracleCommand();
             object result = null;

             try
             {
                 PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms);
                 result = cmd.ExecuteScalar();
                 cmd.Parameters.Clear();
             }
             catch
             {
                 throw;
             }
             finally
             {
                 trans.Dispose();
                 cmd.Dispose();
                 conn.Close();
                 conn.Dispose();
             }

             return result;
         }

         /// <summary>
         /// 执行数据库查询操作,返回结果集中位于第一行第一列的Object类型的值
         /// </summary>
         /// <param name="conn">数据库连接对象</param>
         /// <param name="cmdType">Command类型</param>
         /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
         /// <param name="cmdParms">命令参数集合</param>
         /// <returns>当前查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>
         public static object ExecuteScalar(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
         {
             if (conn == null) throw new ArgumentException("当前数据库连接不存在");
             OracleCommand cmd = new OracleCommand();
             object result = null;

             try
             {
                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                 result = cmd.ExecuteScalar();
                 cmd.Parameters.Clear();
             }
             catch
             {
                 throw;
             }
             finally
             {
                 cmd.Dispose();
                 conn.Close();
                 conn.Dispose();
             }

             return result;
         }

         /// <summary>
         /// 执行数据库命令前的准备工作
         /// </summary>
         /// <param name="cmd">Command对象</param>
         /// <param name="conn">数据库连接对象</param>
         /// <param name="trans">事务对象</param>
         /// <param name="cmdType">Command类型</param>
         /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
         /// <param name="cmdParms">命令参数集合</param>
         private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] cmdParms)
         {
             if (conn.State != ConnectionState.Open)
                 conn.Open();

             cmd.Connection = conn;
             cmd.CommandText = cmdText;

             if (trans != null)
                 cmd.Transaction = trans;

             cmd.CommandType = cmdType;

             if (cmdParms != null)
             {
                 foreach (OracleParameter parm in cmdParms)
                     cmd.Parameters.Add(parm);
             }
         }

         /// <summary>
         /// 将.NET日期时间类型转化为Oracle兼容的日期时间格式字符串
         /// </summary>
         /// <param name="date">.NET日期时间类型对象</param>
         /// <returns>Oracle兼容的日期时间格式字符串(如该字符串:TO_DATE('2007-12-1','YYYY-MM-DD'))</returns>
         public static string GetOracleDateFormat(DateTime date)
         {
             return "TO_DATE('" + date.ToString("yyyy-M-dd") + "','YYYY-MM-DD')";
         }

         /// <summary>
         /// 将.NET日期时间类型转化为Oracle兼容的日期格式字符串
         /// </summary>
         /// <param name="date">.NET日期时间类型对象</param>
         /// <param name="format">Oracle日期时间类型格式化限定符</param>
         /// <returns>Oracle兼容的日期时间格式字符串(如该字符串:TO_DATE('2007-12-1','YYYY-MM-DD'))</returns>
         public static string GetOracleDateFormat(DateTime date, string format)
         {
             if (format == null || format.Trim() == "") format = "YYYY-MM-DD";
             return "TO_DATE('" + date.ToString("yyyy-M-dd") + "','" + format + "')";
         }

         /// <summary>
         /// 将指定的关键字处理为模糊查询时的合法参数值
         /// </summary>
         /// <param name="source">待处理的查询关键字</param>
         /// <returns>过滤后的查询关键字</returns>
         public static string HandleLikeKey(string source)
         {
             if (source == null || source.Trim() == "") return null;

             source = source.Replace("[", "[]]");
             source = source.Replace("_", "[_]");
             source = source.Replace("%", "[%]");

             return ("%" + source + "%");
         }
         #endregion
     }
 }
  • 写几个和Oracle交互的函数;
 [WebMethod(Description = "测试数据库连接,无输入参数,返回bool类型true或者false")]
         public bool CheckOraConnect()
         {
             return OracleHelper.CheckOracleConnect();
         }

         [WebMethod(Description = "输入日期型参数,返回string类型周别")]
         public string GetWeek(string sDate)
         {
             try
             {
                 // 创建参数对象
                 OracleParameter[] param = new OracleParameter[] { new OracleParameter(":date1", OracleDbType.Varchar2) };
                 param[].Value = sDate;
                 // 返回 datatable转换成string;
                 DataSet ds = new DataSet();
                 ds = OracleHelper.ExecuteDataSet(OracleHelper.oraConnStr, CommandType.Text, CommonSQL.sqlGetWeek, param);
                  && ds.Tables[].Rows.Count > )
                 {
                     ].Rows[][].ToString();
                 }
                 else
                 {
                     return "Not Found";
                 }
             }
             catch (Exception ex)
             {
                 return ex.ToString();
             }

         }
  • 配置Web.config(添加一段,解决“测试窗体只能用于来自本地计算机的请求”的异常)
      <webServices>
         <protocols>
           <add name="HttpSoap"/>
           <add name="HttpPost"/>
           <add name="HttpGet"/>
           <add name="Documentation"/>
         </protocols>
       </webServices>

项目发布

  • 生成-发布;
  • 编辑-文件系统;
  • 选择保存,记住的文件名名称,我这里是Publish;
  • 发布。

项目部署

  • 将Publish文件复制到WindowsServer 2012 R2 上指定的路径下;
  • 打开IIS管理器;
  • 添加应用池,注意这里的.NET CLR版本需要与WebService服务.NET版本一致;
  • 添加网站(修改端口,避免端口冲突);
  • 设定默认文档(添加默认文档、设定文件夹权限),如下图。

网站部署测试

选中网站,右键管理网站,选中浏览,出现如下图。

测试一下

=========================================================================================

作者:Jeremy.Wu 
出处:https://www.cnblogs.com/jeremywucnblog/ 
本文版权归作者和博客园共有,欢迎转载【点赞】,转载请保留此段声明,且在文章页面明显位置给出原文连接,谢谢。

相关文章