C# - VS2019 WebService创建与发布,并部署到Windows Server 2012R
前言
上一次数据库灾备和性能优化后,数据库专家建议,在不扩容的情况下,客户端不能再频繁的扫描数据库了!一句惊醒梦中人,因为我也发现数据库越来越卡了,自从上个项目上线后,就出现了这个情况。后来分析其原因,发现客户端每3秒中扫描一次数据库,一共5000+客户端,可想而知,频繁扫描严重影响到数据库性能。所以,我这边准备采用三层架构来解决这个问题,将现有的业务逻辑全部移植到WebService服务器上,客户端通过WebService服务,进而实现对数据库的操作。
此篇只是记录一下,便于后续的学习,不足之处请指正。
创建WebService服务
- 新建ASP.NET Web解决方案,命名为WebServiceTest,框架选择.NET Framework 4,如下图;
- 添加一个Web服务,命名为WebServiceOracleTest,如下图;
- 开始写一些基础Helper类;
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Data; 6 using System.Configuration; 7 using Oracle.ManagedDataAccess.Client; 8 using System.Text; 9 using System.IO; 10 11 namespace WebServiceTest 12 { 13 public class OracleHelper 14 { 15 //连接字符串 16 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"; 17 18 #region Oracle数据库操作通用方法 19 /// <summary> 20 /// 测试数据库连接是否正常 21 /// </summary> 22 /// <param name="strConn"></param> 23 /// <returns></returns> 24 public static bool CheckOracleConnect() 25 { 26 try 27 { 28 OracleConnection conn = new OracleConnection(); 29 conn.ConnectionString = oraConnStr; 30 conn.Open(); 31 return true; 32 } 33 catch 34 { 35 return false; 36 } 37 } 38 39 /// <summary> 40 /// 执行数据库非查询操作,返回受影响的行数 41 /// </summary> 42 /// <param name="connectionString">数据库连接字符串</param> 43 /// <param name="cmdType">命令的类型</param> 44 /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param> 45 /// <param name="cmdParms">命令参数集合</param> 46 /// <returns>当前查询操作影响的数据行数</returns> 47 public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms) 48 { 49 OracleCommand cmd = new OracleCommand(); 50 using (OracleConnection conn = new OracleConnection(connectionString)) 51 { 52 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms); 53 int val = cmd.ExecuteNonQuery(); 54 cmd.Parameters.Clear(); 55 return val; 56 } 57 } 58 59 /// <summary> 60 /// 执行数据库事务非查询操作,返回受影响的行数 61 /// </summary> 62 /// <param name="transaction">数据库事务对象</param> 63 /// <param name="cmdType">Command类型</param> 64 /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param> 65 /// <param name="cmdParms">命令参数集合</param> 66 /// <returns>当前事务查询操作影响的数据行数</returns> 67 public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms) 68 { 69 OracleCommand cmd = new OracleCommand(); 70 PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms); 71 int val = cmd.ExecuteNonQuery(); 72 cmd.Parameters.Clear(); 73 return val; 74 } 75 76 /// <summary> 77 /// 执行数据库非查询操作,返回受影响的行数 78 /// </summary> 79 /// <param name="connection">Oracle数据库连接对象</param> 80 /// <param name="cmdType">Command类型</param> 81 /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param> 82 /// <param name="cmdParms">命令参数集合</param> 83 /// <returns>当前查询操作影响的数据行数</returns> 84 public static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms) 85 { 86 if (connection == null) 87 throw new ArgumentNullException("当前数据库连接不存在"); 88 OracleCommand cmd = new OracleCommand(); 89 PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms); 90 int val = cmd.ExecuteNonQuery(); 91 cmd.Parameters.Clear(); 92 return val; 93 } 94 95 /// <summary> 96 /// 执行数据库查询操作,返回OracleDataReader类型的内存结果集 97 /// </summary> 98 /// <param name="connectionString">数据库连接字符串</param> 99 /// <param name="cmdType">命令的类型</param>100 /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param> 101 /// <param name="cmdParms">命令参数集合</param> 102 /// <returns>当前查询操作返回的OracleDataReader类型的内存结果集</returns> 103 public static OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)104 {105 OracleCommand cmd = new OracleCommand();106 OracleConnection conn = new OracleConnection(connectionString);107 try108 {109 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);110 OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);111 cmd.Parameters.Clear();112 return reader;113 }114 catch115 {116 cmd.Dispose();117 conn.Close();118 throw;119 }120 }121 122 /// <summary> 123 /// 执行数据库查询操作,返回DataSet类型的结果集 124 /// </summary> 125 /// <param name="connectionString">数据库连接字符串</param>126 /// <param name="cmdType">命令的类型</param>127 /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param> 128 /// <param name="cmdParms">命令参数集合</param> 129 /// <returns>当前查询操作返回的DataSet类型的结果集</returns> 130 public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)131 {132 OracleCommand cmd = new OracleCommand();133 OracleConnection conn = new OracleConnection(connectionString);134 DataSet ds = null;135 try136 {137 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);138 OracleDataAdapter adapter = new OracleDataAdapter();139 adapter.SelectCommand = cmd;140 ds = new DataSet();141 adapter.Fill(ds);142 cmd.Parameters.Clear();143 }144 catch145 {146 throw;147 }148 finally149 {150 cmd.Dispose();151 conn.Close();152 conn.Dispose();153 }154 155 return ds;156 }157 158 /// <summary> 159 /// 执行数据库查询操作,返回DataTable类型的结果集 160 /// </summary> 161 /// <param name="connectionString">数据库连接字符串</param>162 /// <param name="cmdType">命令的类型</param>163 /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param> 164 /// <param name="cmdParms">命令参数集合</param> 165 /// <returns>当前查询操作返回的DataTable类型的结果集</returns> 166 public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)167 {168 OracleCommand cmd = new OracleCommand();169 OracleConnection conn = new OracleConnection(connectionString);170 DataTable dt = null;171 172 try173 {174 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);175 OracleDataAdapter adapter = new OracleDataAdapter();176 adapter.SelectCommand = cmd;177 dt = new DataTable();178 adapter.Fill(dt);179 cmd.Parameters.Clear();180 }181 catch182 {183 throw;184 }185 finally186 {187 cmd.Dispose();188 conn.Close();189 conn.Dispose();190 }191 192 return dt;193 }194 195 /// <summary> 196 /// 执行数据库查询操作,返回结果集中位于第一行第一列的Object类型的值 197 /// </summary> 198 /// <param name="connectionString">数据库连接字符串</param>199 /// <param name="cmdType">命令的类型</param>200 /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param> 201 /// <param name="cmdParms">命令参数集合</param> 202 /// <returns>当前查询操作返回的结果集中位于第一行第一列的Object类型的值</returns> 203 public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)204 {205 OracleCommand cmd = new OracleCommand();206 OracleConnection conn = new OracleConnection(connectionString);207 object result = null;208 try209 {210 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);211 result = cmd.ExecuteScalar();212 cmd.Parameters.Clear();213 }214 catch215 {216 throw;217 }218 finally219 {220 cmd.Dispose();221 conn.Close();222 conn.Dispose();223 }224 225 return result;226 }227 228 /// <summary> 229 /// 执行数据库事务查询操作,返回结果集中位于第一行第一列的Object类型的值 230 /// </summary> 231 /// <param name="trans">一个已存在的数据库事务对象</param> 232 /// <param name="commandType">命令类型</param> 233 /// <param name="commandText">Oracle存储过程名称或PL/SQL命令</param> 234 /// <param name="cmdParms">命令参数集合</param> 235 /// <returns>当前事务查询操作返回的结果集中位于第一行第一列的Object类型的值</returns> 236 public static object ExecuteScalar(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)237 {238 if (trans == null)239 throw new ArgumentNullException("当前数据库事务不存在");240 OracleConnection conn = trans.Connection;241 if (conn == null)242 throw new ArgumentException("当前事务所在的数据库连接不存在");243 244 OracleCommand cmd = new OracleCommand();245 object result = null;246 247 try248 {249 PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms);250 result = cmd.ExecuteScalar();251 cmd.Parameters.Clear();252 }253 catch254 {255 throw;256 }257 finally258 {259 trans.Dispose();260 cmd.Dispose();261 conn.Close();262 conn.Dispose();263 }264 265 return result;266 }267 268 /// <summary> 269 /// 执行数据库查询操作,返回结果集中位于第一行第一列的Object类型的值 270 /// </summary> 271 /// <param name="conn">数据库连接对象</param> 272 /// <param name="cmdType">Command类型</param> 273 /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param> 274 /// <param name="cmdParms">命令参数集合</param> 275 /// <returns>当前查询操作返回的结果集中位于第一行第一列的Object类型的值</returns> 276 public static object ExecuteScalar(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)277 {278 if (conn == null) throw new ArgumentException("当前数据库连接不存在");279 OracleCommand cmd = new OracleCommand();280 object result = null;281 282 try283 {284 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);285 result = cmd.ExecuteScalar();286 cmd.Parameters.Clear();287 }288 catch289 {290 throw;291 }292 finally293 {294 cmd.Dispose();295 conn.Close();296 conn.Dispose();297 }298 299 return result;300 }301 302 /// <summary> 303 /// 执行数据库命令前的准备工作 304 /// </summary> 305 /// <param name="cmd">Command对象</param> 306 /// <param name="conn">数据库连接对象</param> 307 /// <param name="trans">事务对象</param> 308 /// <param name="cmdType">Command类型</param> 309 /// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param> 310 /// <param name="cmdParms">命令参数集合</param> 311 private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] cmdParms)312 {313 if (conn.State != ConnectionState.Open)314 conn.Open();315 316 cmd.Connection = conn;317 cmd.CommandText = cmdText;318 319 if (trans != null)320 cmd.Transaction = trans;321 322 cmd.CommandType = cmdType;323 324 if (cmdParms != null)325 {326 foreach (OracleParameter parm in cmdParms)327 cmd.Parameters.Add(parm);328 }329 }330 331 /// <summary> 332 /// 将.NET日期时间类型转化为Oracle兼容的日期时间格式字符串 333 /// </summary> 334 /// <param name="date">.NET日期时间类型对象</param> 335 /// <returns>Oracle兼容的日期时间格式字符串(如该字符串:TO_DATE('2007-12-1','YYYY-MM-DD'))</returns> 336 public static string GetOracleDateFormat(DateTime date)337 {338 return "TO_DATE('" + date.ToString("yyyy-M-dd") + "','YYYY-MM-DD')";339 }340 341 /// <summary> 342 /// 将.NET日期时间类型转化为Oracle兼容的日期格式字符串 343 /// </summary> 344 /// <param name="date">.NET日期时间类型对象</param> 345 /// <param name="format">Oracle日期时间类型格式化限定符</param> 346 /// <returns>Oracle兼容的日期时间格式字符串(如该字符串:TO_DATE('2007-12-1','YYYY-MM-DD'))</returns> 347 public static string GetOracleDateFormat(DateTime date, string format)348 {349 if (format == null || format.Trim() == "") format = "YYYY-MM-DD";350 return "TO_DATE('" + date.ToString("yyyy-M-dd") + "','" + format + "')";351 }352 353 /// <summary> 354 /// 将指定的关键字处理为模糊查询时的合法参数值 355 /// </summary> 356 /// <param name="source">待处理的查询关键字</param> 357 /// <returns>过滤后的查询关键字</returns> 358 public static string HandleLikeKey(string source)359 {360 if (source == null || source.Trim() == "") return null;361 362 source = source.Replace("[", "[]]");363 source = source.Replace("_", "[_]");364 source = source.Replace("%", "[%]");365 366 return ("%" + source + "%");367 }368 #endregion369 }370 }
View Code
- 写几个和Oracle交互的函数;
1 [WebMethod(Description = "测试数据库连接,无输入参数,返回bool类型true或者false")] 2 public bool CheckOraConnect() 3 { 4 return OracleHelper.CheckOracleConnect(); 5 } 6 7 [WebMethod(Description = "输入日期型参数,返回string类型周别")] 8 public string GetWeek(string sDate) 9 {10 try11 {12 // 创建参数对象13 OracleParameter[] param = new OracleParameter[] { new OracleParameter(":date1", OracleDbType.Varchar2) };14 param[0].Value = sDate;15 // 返回 datatable转换成string;16 DataSet ds = new DataSet();17 ds = OracleHelper.ExecuteDataSet(OracleHelper.oraConnStr, CommandType.Text, CommonSQL.sqlGetWeek, param);18 if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)19 {20 return ds.Tables[0].Rows[0][0].ToString();21 }22 else23 {24 return "Not Found";25 }26 }27 catch (Exception ex)28 {29 return ex.ToString();30 }31 32 }
View Code
- 配置Web.config(添加一段,解决“测试窗体只能用于来自本地计算机的请求”的异常)
1 <webServices>2 <protocols>3 <add name="HttpSoap"/>4 <add name="HttpPost"/>5 <add name="HttpGet"/>6 <add name="Documentation"/>7 </protocols>8 </webServices>
View Code
项目发布
- 生成-发布;
- 编辑-文件系统;
- 选择保存,记住的文件名名称,我这里是Publish;
- 发布。
项目部署
- 将Publish文件复制到WindowsServer 2012 R2 上指定的路径下;
- 打开IIS管理器;
- 添加应用池,注意这里的.NET CLR版本需要与WebService服务.NET版本一致;
- 添加网站(修改端口,避免端口冲突);
- 设定默认文档(添加默认文档、设定文件夹权限),如下图。
网站部署测试
选中网站,右键管理网站,选中浏览,出现如下图。
测试一下
作者:Jeremy.Wu
出处:https://www.cnblogs.com/jeremywucnblog/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
赞 (0)