时间:2021-07-01 10:21:17 帮助过:12人阅读
接口实现部分:
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.Data; 6 7 using System.Linq; 8 9 using System.Runtime.Serialization; 10 11 using System.ServiceModel; 12 13 using System.ServiceModel.Web; 14 15 using System.Text; 16 17 18 19 namespace SelfWcfService 20 21 { 22 23 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。 24 25 // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。 26 27 public class SelfSQLData : ISelfSQLData 28 29 { 30 31 private static SQLData _sqlData; 32 33 public static SQLData SqlData 34 35 { 36 37 get 38 39 { 40 41 if (_sqlData == null) 42 43 { 44 45 _sqlData = new SQLData(); 46 47 } 48 49 if (_sqlData.DataTables == null) 50 51 { 52 53 _sqlData.DataTables = new Dictionary<string, System.Data.DataTable>(); 54 55 } 56 57 return _sqlData; 58 59 } 60 61 set 62 63 { 64 65 _sqlData = value; 66 67 } 68 69 } 70 71 72 73 private void InitalSqlData() 74 75 { 76 77 if (_sqlData == null) 78 79 { 80 81 _sqlData = new SQLData(); 82 83 } 84 85 if (_sqlData.DataTables == null) 86 87 { 88 89 _sqlData.DataTables = new Dictionary<string, System.Data.DataTable>(); 90 91 } 92 93 } 94 95 96 97 public ExcuteResult CreateTable(string tableName, DataTable colNames) 98 99 { 100 101 if (string.IsNullOrWhiteSpace(tableName)) 102 103 { 104 105 return new ExcuteResult(1, "表名不能为空!"); 106 107 } 108 109 if (colNames == null || colNames.Columns.Count < 1) 110 111 { 112 113 return new ExcuteResult(1, "表字段不能为空!"); 114 115 } 116 117 InitalSqlData(); 118 119 if (!_sqlData.DataTables.ContainsKey(tableName)) 120 121 { 122 123 try 124 125 { 126 127 _sqlData.DataTables.Add(tableName, colNames); 128 129 } 130 131 catch (Exception ex) 132 133 { 134 135 return new ExcuteResult(1, ex.Message); 136 137 } 138 139 return new ExcuteResult(0, "表" + tableName + "创建成功!"); 140 141 } 142 143 else 144 145 { 146 147 return new ExcuteResult(1, "已存在名为‘" + tableName + "‘的表"); 148 149 } 150 151 } 152 153 154 155 public ExcuteResult Insert(string tableName, SelfDataRow dr) 156 157 { 158 159 if (string.IsNullOrWhiteSpace(tableName)) 160 161 { 162 163 return new ExcuteResult(1, "表名不能为空!"); 164 165 } 166 167 if (dr == null) 168 169 { 170 171 return new ExcuteResult(1, "不能插入空的数据行!"); 172 173 } 174 175 InitalSqlData(); 176 177 if (!_sqlData.DataTables.ContainsKey(tableName)) 178 179 { 180 181 try 182 183 { 184 185 _sqlData.DataTables[tableName].Rows.Add(dr.DataTable.Rows[0]); 186 187 } 188 189 catch (Exception ex) 190 191 { 192 193 return new ExcuteResult(1, ex.Message); 194 195 } 196 197 return new ExcuteResult(0, "数据新增成功!"); 198 199 } 200 201 else 202 203 { 204 205 return new ExcuteResult(1, "表‘" + tableName + "‘不存在!"); 206 207 } 208 209 } 210 211 212 213 public ExcuteResult Delete(string tableName, SelfDataRow dr) 214 215 { 216 217 if (string.IsNullOrWhiteSpace(tableName)) 218 219 { 220 221 return new ExcuteResult(1, "表名不能为空!"); 222 223 } 224 225 if (dr == null) 226 227 { 228 229 return new ExcuteResult(1, "请指定删除对象!"); 230 231 } 232 233 InitalSqlData(); 234 235 if (!_sqlData.DataTables.ContainsKey(tableName)) 236 237 { 238 239 try 240 241 { 242 243 _sqlData.DataTables[tableName].Rows.Remove(dr.DataTable.Rows[0]); 244 245 } 246 247 catch (Exception ex) 248 249 { 250 251 return new ExcuteResult(1, ex.Message); 252 253 } 254 255 return new ExcuteResult(0, "数据删除成功!"); 256 257 } 258 259 else 260 261 { 262 263 return new ExcuteResult(1, "表‘" + tableName + "‘不存在!"); 264 265 } 266 267 } 268 269 270 271 public ExcuteResult DropTable(string tableName) 272 273 { 274 275 if (string.IsNullOrWhiteSpace(tableName)) 276 277 { 278 279 return new ExcuteResult(1, "表名不能为空!"); 280 281 } 282 283 InitalSqlData(); 284 285 if (!_sqlData.DataTables.ContainsKey(tableName)) 286 287 { 288 289 try 290 291 { 292 293 _sqlData.DataTables.Remove(tableName); 294 295 } 296 297 catch (Exception ex) 298 299 { 300 301 return new ExcuteResult(1, ex.Message); 302 303 } 304 305 return new ExcuteResult(0, "表" + tableName + "删除成功!"); 306 307 } 308 309 else 310 311 { 312 313 return new ExcuteResult(1, "表‘" + tableName + "‘不存在!"); 314 315 } 316 317 } 318 319 320 321 public Dictionary<string, DataTable> GetData() 322 323 { 324 325 return SqlData.DataTables; 326 327 } 328 329 } 330 331 }
整个WCF实现的逻辑都在上面了,web配置文件不需要改,因为它不以web的形式发布寄宿在IIS中,而是windows服务中。
第2步:将WCF寄宿在Windows服务中
Windows服务的创建、安装、启动在这里也不作累述,跟WCF一样,网上资料也很多。这里重点介绍一下将WCF寄宿在Windows服务中。
(1) 将wcf项目的dll引用到windows服务项目中
(2) 在app.config文件中配置WCF终结点
1 <system.serviceModel> 2 3 <services> 4 5 <service behaviorConfiguration="BasicServiceBehavior" 6 7 name="SelfWcfService.SelfSQLData"> 8 9 <endpoint address="" binding="netTcpBinding" bindingConfiguration="" 10 11 contract="SelfWcfService.ISelfSQLData"> 12 13 <!--<identity> 14 15 <dns value="192.168.1.4" /> 16 17 </identity>--> 18 19 </endpoint> 20 21 <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" 22 23 contract="IMetadataExchange" /> 24 25 <host> 26 27 <baseAddresses> 28 29 <add baseAddress="net.tcp://localhost:9000/SelfSQLData.svc"/> 30 31 <add baseAddress="http://localhost:9001/SelfSQLData.svc"/> 32 33 </baseAddresses> 34 35 </host> 36 37 </service> 38 39 </services> 40 41 <behaviors> 42 43 <serviceBehaviors> 44 45 <behavior name="BasicServiceBehavior"> 46 47 <serviceMetadata httpGetEnabled="true" /> 48 49 <serviceDebug includeExceptionDetailInFaults="true" /> 50 51 </behavior> 52 53 </serviceBehaviors> 54 55 </behaviors> 56 57 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" /> 58 59 <bindings> 60 61 <netTcpBinding> 62 63 <binding name="defaultBinding" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"> 64 65 <security mode="None"> 66 67 <message clientCredentialType="None"/> 68 69 <transport clientCredentialType="None"></transport> 70 71 </security> 72 73 <readerQuotas /> 74 75 </binding> 76 77 </netTcpBinding> 78 79 </bindings> 80 81 </system.serviceModel>
(3) 在windows服务启动的时候启动WCF服务
1 using SelfHelper; 2 3 using System; 4 5 using System.Collections.Generic; 6 7 using System.ComponentModel; 8 9 using System.Data; 10 11 using System.Diagnostics; 12 13 using System.Linq; 14 15 using System.ServiceProcess; 16 17 using System.Text; 18 19 using System.ServiceModel; 20 21 22 23 namespace SelfSQL 24 25 { 26 27 public partial class SelfSQLServer : ServiceBase 28 29 { 30 31 public SelfSQLServer() 32 33 { 34 35 InitializeComponent(); 36 37 } 38 39 40 41 ServiceHost host = new ServiceHost(typeof(SelfWcfService.SelfSQLData)); 42 43 protected override void OnStart(string[] args) 44 45 { 46 47 LogWriter.WriteLog("服务已启动!"); 48 49 try 50 51 { 52 53 host.Open(); 54 55 LogWriter.WriteLog("WCF启动成功"); 56 57 } 58 59 catch (Exception ex) 60 61 { 62 63 LogWriter.WriteLog("WCF启动异常:" + ex.ToString()); 64 65 } 66 67 68 69 } 70 71 72 73 protected override void OnStop() 74 75 { 76 77 LogWriter.WriteLog("服务已停止!"); 78 79 } 80 81 } 82 83 }
(4) 启动windows服务进行测试
第3步:创建客户端进行测试
首先在控制台程序中添加服务引用
然后就可以调用客户端对象进行测试了。
测试代码:
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.Data; 6 7 using System.Linq; 8 9 using System.Text; 10 11 12 13 namespace SelfSQLClient 14 15 { 16 17 class Program 18 19 { 20 21 static void Main(string[] args) 22 23 { 24 25 SelfSQLServiceCilent.SelfSQLDataClient client = new SelfSQLServiceCilent.SelfSQLDataClient(); 26 27 DataTable dt1 = new DataTable(); 28 29 30 31 DataColumn idColumn = new DataColumn(); 32 33 idColumn.DataType = System.Type.GetType("System.Int32"); 34 35 idColumn.ColumnName = "t1"; 36 37 idColumn.AutoIncrement = true; 38 39 dt1.Columns.Add(idColumn); 40 41 42 43 DataColumn aColumn = new DataColumn(); 44 45 aColumn.DataType = System.Type.GetType("System.Int32"); 46 47 aColumn.ColumnName = "t2"; 48 49 aColumn.AutoIncrement = false; 50 51 dt1.Columns.Add(aColumn); 52 53 54 55 DataColumn bColumn = new DataColumn(); 56 57 bColumn.DataType = System.Type.GetType("System.DateTime"); 58 59 bColumn.ColumnName = "t3"; 60 61 bColumn.AutoIncrement = false; 62 63 dt1.Columns.Add(bColumn); 64 65 66 67 for (int i = 0; i < 3; i++) 68 69 { 70 71 DataRow dr = dt1.NewRow(); 72 73 dr[0] = i; 74 75 dr[1] = i + 1; 76 77 dr[2] = DateTime.Now; 78 79 dt1.Rows.Add(dr); 80 81 } 82 83 dt1.TableName = "FirstTable"; 84 85 client.CreateTable("FirstTable", dt1); 86 87 88 89 Dictionary<string, DataTable> dts = client.GetData(); 90 91 foreach (var dt in dts) 92 93 { 94 95 Console.WriteLine("表名:" + dt.Key); 96 97 string colNames = string.Empty; 98 99 foreach (var col in dt.Value.Columns) 100 101 { 102 103 colNames += col.ToString() + "--"; 104 105 } 106 107 Console.WriteLine("字段:" + colNames); 108 109 foreach (DataRow val in dt.Value.Rows) 110 111 { 112 113 string vals = string.Empty;