RabbitMq.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. using MassTransit;
  2. using MassTransit.Util;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. namespace Message.MessageTypes
  12. {
  13. public class RabbitMq
  14. {
  15. private readonly IRequestClient<ISimpleRequest, ISimpleResponse> _requestClient;
  16. public RabbitMq(IRequestClient<ISimpleRequest, ISimpleResponse> requestClient)
  17. {
  18. _requestClient = requestClient;
  19. }
  20. //CommonRepository oCommonRepository = new CommonRepository();
  21. //public bool SendMq(string Tranid, string UserId, string AppType, string StageType, string Enviroment)
  22. //{
  23. // //oCommonRepository.WriteLog("SendMq:", "Main App");
  24. // string ServiceAddress = "";
  25. // string RabbitMQHost = "";
  26. // string userName = "";
  27. // string password = "";
  28. // using (IDbConnection dbConnection = ConnectionBase.Connection)
  29. // {
  30. // dbConnection.Open();
  31. // DynamicParameters param = new DynamicParameters();
  32. // param.Add("p_tranid", dbType: DbType.Int32, value: Tranid, direction: ParameterDirection.Input);
  33. // param.Add("p_userid", dbType: DbType.Int32, value: UserId, direction: ParameterDirection.Input);
  34. // param.Add("p_status", dbType: DbType.String, value: "P", direction: ParameterDirection.Input);
  35. // param.Add("p_option", dbType: DbType.String, value: AppType, direction: ParameterDirection.Input);
  36. // var Checkinapp = dbConnection.Query<string>("public.spusrlottranupdatev1", param, commandType: CommandType.StoredProcedure).ToList();
  37. // if (Checkinapp != null)
  38. // {
  39. // // var i_Checkinapp = JsonConvert.DeserializeObject<object>(Checkinapp[0].ToString());
  40. // // checkinprocess obj = JsonConvert.DeserializeObject<checkinprocess>(Checkinapp[0].ToString());
  41. // var next_ordernodata10 = JsonConvert.DeserializeObject<object>(Checkinapp[0].ToString());
  42. // JArray dtparallelnextstage10 = JArray.Parse(next_ordernodata10.ToString());
  43. // checkinprocess obj = JsonConvert.DeserializeObject<checkinprocess>(dtparallelnextstage10[0].ToString());
  44. // if (obj.ipaddress != null)
  45. // {
  46. // ServiceAddress = obj.ipaddress + "" + "/" + obj.root_path;
  47. // RabbitMQHost = obj.ipaddress;
  48. // userName = obj.username;
  49. // password = obj.password;
  50. // StageType = obj.stagetype;
  51. // }
  52. // if (StageType != "server")
  53. // return false;
  54. // }
  55. // }
  56. // var datetime = DateTime.Now.ToString();
  57. // var message = new PublishMessage(datetime, Tranid, UserId, AppType, StageType, Startup.Configuration.GetValue<string>("AppSettings:envirnmentname"), null, "");
  58. // var task = Publish(message);
  59. // if (task.Result != true)
  60. // {
  61. // return false;
  62. // }
  63. // return true;
  64. //}
  65. //public bool SendMq_WorkArea(string Tranid, string UserId, string AppType, string StageType, string Enviroment, string cflag, string extravalues, string Batch="", string PublicationType="", string comments="", string PublicationID = "" )
  66. //{
  67. // string RabbitMQHost = "";
  68. // oCommonRepository.WriteLog("RabbitMq", "SendMq_WorkArea");
  69. // using (IDbConnection dbConnection = ConnectionBase.Connection)
  70. // {
  71. // dbConnection.Open();
  72. // DynamicParameters param = new DynamicParameters();
  73. // param.Add("p_tranid", dbType: DbType.Int32, value: Convert.ToInt32(Tranid), direction: ParameterDirection.Input);
  74. // param.Add("p_userid", dbType: DbType.Int32, value: Convert.ToInt32(UserId), direction: ParameterDirection.Input);
  75. // param.Add("p_status", dbType: DbType.String, value: "P", direction: ParameterDirection.Input);
  76. // param.Add("p_option", dbType: DbType.String, value: AppType, direction: ParameterDirection.Input);
  77. // var Checkinapp = dbConnection.Query<string>("public.spusrlottranupdatev1", param, commandType: CommandType.StoredProcedure).ToList();
  78. // if (Checkinapp != null)
  79. // {
  80. // oCommonRepository.WriteLog("SendMq_WorkArea", Checkinapp[0].ToString());
  81. // var next_ordernodata10 = JsonConvert.DeserializeObject<object>(Checkinapp[0].ToString());
  82. // JArray dtparallelnextstage10 = JArray.Parse(next_ordernodata10.ToString());
  83. // checkinprocess obj = JsonConvert.DeserializeObject<checkinprocess>(dtparallelnextstage10[0]["json_build_object"].ToString());
  84. // RabbitMQHost = Startup.Configuration.GetValue<string>("AppSettings:chechoutqueue_path");
  85. // oCommonRepository.WriteLog("SendMq_WorkArea:RabbitMQHost", RabbitMQHost);
  86. // if (obj.stagetype != null)
  87. // {
  88. // StageType = obj.stagetype;
  89. // }
  90. // }
  91. // }
  92. // oCommonRepository.WriteLog("SendMq_WorkArea:Tranid" + Tranid, AppType);
  93. // var datetime = DateTime.Now.ToString();
  94. // var message = new PublishMessage(datetime, Tranid, UserId, AppType, StageType, Startup.Configuration.GetValue<string>("AppSettings:envirnmentname"), extravalues, cflag, Batch, PublicationType, comments, PublicationID);
  95. // var task = Publish(message);
  96. // if (task.Result != true)
  97. // {
  98. // return false;
  99. // }
  100. // return true;
  101. //}
  102. public bool SendMq_StageComplete(string Tranid, string UserId, string AppType, string StageType, string Enviroment, string cflag, string extravalues)
  103. {
  104. //string exchangeName = Startup.Configuration.GetValue<string>("AppSettings:chechoutqueue_exchange");
  105. Enviroment = ConfigurationManager.AppSettings["Environment"];
  106. var datetime = DateTime.Now.ToString();
  107. var message = new PublishMessage("", Tranid, UserId, AppType, StageType, Enviroment,null, cflag);
  108. var task = Publish(message);
  109. if (task.Result != true)
  110. {
  111. return false;
  112. }
  113. return true;
  114. }
  115. public async Task<bool> Publish(PublishMessage message)
  116. {
  117. //string ServiceAddress = Startup.Configuration.GetValue<string>("AppSettings:chechoutqueue_path") + "" + "/" + Startup.Configuration.GetValue<string>("AppSettings:chechoutqueue");
  118. //string QueueName = Startup.Configuration.GetValue<string>("AppSettings:chechoutqueue");
  119. //string RabbitMQHost = Startup.Configuration.GetValue<string>("AppSettings:chechoutqueue_path");
  120. string ServiceAddress = ConfigurationManager.AppSettings["chechoutqueue_path"] + "" + "/" + ConfigurationManager.AppSettings["chechoutqueue"];
  121. string QueueName = ConfigurationManager.AppSettings["chechoutqueue"];
  122. string RabbitMQHost = ConfigurationManager.AppSettings["chechoutqueue_path"];
  123. string userName = "admin"; //obj.username;
  124. string password = "admin"; //obj.password;
  125. var _busControl = Bus.Factory.CreateUsingRabbitMq(x =>
  126. {
  127. var host = x.Host(new Uri(RabbitMQHost), h =>
  128. {
  129. h.Username(userName);
  130. h.Password(password);
  131. });
  132. });
  133. try
  134. {
  135. //TaskUtil.Await(() => _busControl.StartAsync());
  136. Uri ServiceAddress_uri = new Uri(ServiceAddress);
  137. var endPoint = await _busControl.GetSendEndpoint(ServiceAddress_uri);
  138. await endPoint.Send(message);
  139. //await _busControl.Publish(message);
  140. }
  141. catch (Exception ex)
  142. {
  143. //oCommonRepository.ErrorLog(ex);
  144. // Console.WriteLine($"Error publishing message: {ex.Message}");
  145. return false;
  146. }
  147. finally
  148. {
  149. _busControl.Stop();
  150. }
  151. return true;
  152. }
  153. //public async Task<bool> Publish_MailQueue(PublishMessage message)
  154. //{
  155. // string ServiceAddress = Startup.Configuration.GetValue<string>("AppSettings:mailqueue_path") + "" + "/" + Startup.Configuration.GetValue<string>("AppSettings:mailqueue");
  156. // string RabbitMQHost = Startup.Configuration.GetValue<string>("AppSettings:mailqueue_path");
  157. // string userName = "admin"; //obj.username;
  158. // string password = "admin"; //obj.password;
  159. // var _busControl = Bus.Factory.CreateUsingRabbitMq(x =>
  160. // {
  161. // var host = x.Host(new Uri(RabbitMQHost), h =>
  162. // {
  163. // h.Username(userName);
  164. // h.Password(password);
  165. // });
  166. // });
  167. // try
  168. // {
  169. // TaskUtil.Await(() => _busControl.StartAsync());
  170. // Uri ServiceAddress_uri = new Uri(ServiceAddress);
  171. // var endPoint = await _busControl.GetSendEndpoint(ServiceAddress_uri);
  172. // await endPoint.Send(message);
  173. // // _busControl.Publish(message);
  174. // }
  175. // catch (Exception ex)
  176. // {
  177. // oCommonRepository.ErrorLog(ex);
  178. // return false;
  179. // }
  180. // finally
  181. // {
  182. // _busControl.Stop();
  183. // }
  184. // return true;
  185. //}
  186. //public bool SendMq_Stage_Call_Web(string Tranid, string UserId, string AppType, string StageType, string Enviroment, string cflag, object extravalues = null)
  187. //{
  188. // var datetime = DateTime.Now.ToString();
  189. // var message = new PublishMessage(datetime, Tranid, UserId, AppType, StageType, Startup.Configuration.GetValue<string>("AppSettings:envirnmentname"), extravalues, cflag);
  190. // var task = Publish(message);
  191. // if (task.Result != true)
  192. // {
  193. // return false;
  194. // }
  195. // return true;
  196. //}
  197. //public bool SendMqMail(string Tranid, string JobWorkflowid, string flag, string curtranid, string userid)
  198. //{
  199. // oCommonRepository.WriteLog("SendMqMail started:", "RabbitMq");
  200. // DynamicParams dynamicparams = new DynamicParams()
  201. // {
  202. // jobworkflowid = JobWorkflowid,
  203. // nexttranid = curtranid
  204. // };
  205. // List<DynamicParams> dynamicparams_ = new List<DynamicParams>();
  206. // dynamicparams_.Add(dynamicparams);
  207. // var datetime = DateTime.Now.ToString();
  208. // var message = new PublishMessage(DateTime.Now.ToString(), Tranid, userid, "", "", Startup.Configuration.GetValue<string>("AppSettings:envirnmentname"), dynamicparams, flag);
  209. // oCommonRepository.WriteLog("JobWorkflowid:" + JobWorkflowid, "RabbitMq-SendMqMail");
  210. // oCommonRepository.WriteLog("curtranid:" + curtranid, "RabbitMq-SendMqMail");
  211. // var task = Publish_MailQueue(message);
  212. // if (task.Result != true)
  213. // {
  214. // return false;
  215. // }
  216. // return true;
  217. //}
  218. //public bool SendMqTest(string Tranid, string JobWorkflowid, string flag, string curtranid, string userid)
  219. //{
  220. // DynamicParams dynamicparams = new DynamicParams()
  221. // {
  222. // jobworkflowid = JobWorkflowid,
  223. // nexttranid = curtranid
  224. // };
  225. // List<DynamicParams> dynamicparams_ = new List<DynamicParams>();
  226. // dynamicparams_.Add(dynamicparams);
  227. // var message = new PublishMessage(DateTime.Now.ToString(), "123", userid, "", "", Startup.Configuration.GetValue<string>("AppSettings:envirnmentname"), dynamicparams, flag);
  228. // oCommonRepository.WriteLog("JobWorkflowid:" + JobWorkflowid, "RabbitMq-SendMqMail");
  229. // oCommonRepository.WriteLog("curtranid:" + curtranid, "RabbitMq-SendMqMail");
  230. // var task = Publish_MailQueue(message);
  231. // if (task.Result != true)
  232. // {
  233. // return false;
  234. // }
  235. // return true;
  236. //}
  237. public IRequestClient<ISimpleRequest, ISimpleResponse> CreateRequestClient(IBusControl busControl, string ServiceAddress)
  238. {
  239. var serviceAddress = new Uri(ServiceAddress);
  240. IRequestClient<ISimpleRequest, ISimpleResponse> client =
  241. busControl.CreateRequestClient<ISimpleRequest, ISimpleResponse>(serviceAddress, TimeSpan.FromSeconds(30));
  242. return client;
  243. }
  244. public IBusControl CreateBus(string RabbitMQHost, string userName, string password)
  245. {
  246. var currentBus = Bus.Factory.CreateUsingRabbitMq(x =>
  247. {
  248. var Host = x.Host(new Uri(RabbitMQHost), h =>
  249. {
  250. h.Username(userName);
  251. h.Password(password);
  252. });
  253. });
  254. return currentBus;
  255. }
  256. static void ConfigureLogger()
  257. {
  258. // const string logConfig = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
  259. //<log4net>
  260. // <root>
  261. // <level value=""INFO"" />
  262. // <appender-ref ref=""console"" />
  263. // </root>
  264. // <appender name=""console"" type=""log4net.Appender.ColoredConsoleAppender"">
  265. // <layout type=""log4net.Layout.PatternLayout"">
  266. // <conversionPattern value=""%m%n"" />
  267. // </layout>
  268. // </appender>
  269. //</log4net>";
  270. // using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(logConfig)))
  271. // {
  272. // XmlConfigurator.Configure(stream);
  273. // }
  274. }
  275. }
  276. //public class PublishMessage
  277. //{
  278. // public PublishMessage(string customerId, string Tranid, string UserId, string AppType, string StageType, string Enviroment, object DynamicList, string Flag)
  279. // {
  280. // _customerId = customerId;
  281. // _TransId = Tranid;
  282. // _UserId = UserId;
  283. // _AppType = AppType;
  284. // _StageType = StageType;
  285. // _Enviroment = Enviroment;
  286. // _timestamp = DateTime.UtcNow;
  287. // _DynamicList = DynamicList;
  288. // _Flag = Flag;
  289. // }
  290. // public string _customerId { get; set; }
  291. // public string _TransId { get; set; }
  292. // public string _UserId { get; set; }
  293. // public string _AppType { get; set; }
  294. // public string _StageType { get; set; }
  295. // public string _Enviroment { get; set; }
  296. // public DateTime _timestamp { get; set; }
  297. // public object _DynamicList { get; set; }
  298. // public string _Flag { get; set; }
  299. //}
  300. class SimpleRequest : ISimpleRequest
  301. {
  302. readonly string _customerId;
  303. readonly string _TransId;
  304. readonly string _UserId;
  305. readonly string _AppType;
  306. readonly string _StageType;
  307. readonly string _Enviroment;
  308. readonly DateTime _timestamp;
  309. readonly object _DynamicList;
  310. readonly string _Flag;
  311. public SimpleRequest(string customerId, string Tranid, string UserId, string AppType, string StageType, string Enviroment, object DynamicList, string Flag)
  312. {
  313. _customerId = customerId;
  314. _TransId = Tranid;
  315. _UserId = UserId;
  316. _AppType = AppType;
  317. _StageType = StageType;
  318. _Enviroment = Enviroment;
  319. _timestamp = DateTime.UtcNow;
  320. _DynamicList = DynamicList;
  321. _Flag = Flag;
  322. }
  323. public DateTime Timestamp
  324. {
  325. get { return _timestamp; }
  326. }
  327. public string TransId
  328. {
  329. get { return _TransId; }
  330. }
  331. public string CustomerId
  332. {
  333. get { return _customerId; }
  334. }
  335. public string UserId
  336. {
  337. get { return _UserId; }
  338. }
  339. public string AppType
  340. {
  341. get { return _AppType; }
  342. }
  343. public string StageType
  344. {
  345. get { return _StageType; }
  346. }
  347. public string Enviroment
  348. {
  349. get { return _Enviroment; }
  350. }
  351. public object DynamicList
  352. {
  353. get { return _DynamicList; }
  354. }
  355. public string Flag
  356. {
  357. get { return _Flag; }
  358. }
  359. string ISimpleRequest.TransId
  360. {
  361. get
  362. {
  363. throw new NotImplementedException();
  364. }
  365. set
  366. {
  367. throw new NotImplementedException();
  368. }
  369. }
  370. string ISimpleRequest.UserId
  371. {
  372. get
  373. {
  374. throw new NotImplementedException();
  375. }
  376. set
  377. {
  378. throw new NotImplementedException();
  379. }
  380. }
  381. string ISimpleRequest.AppType
  382. {
  383. get
  384. {
  385. throw new NotImplementedException();
  386. }
  387. set
  388. {
  389. throw new NotImplementedException();
  390. }
  391. }
  392. string ISimpleRequest.StageType
  393. {
  394. get
  395. {
  396. throw new NotImplementedException();
  397. }
  398. set
  399. {
  400. throw new NotImplementedException();
  401. }
  402. }
  403. string ISimpleRequest.Enviroment
  404. {
  405. get
  406. {
  407. throw new NotImplementedException();
  408. }
  409. set
  410. {
  411. throw new NotImplementedException();
  412. }
  413. }
  414. object ISimpleRequest.DynamicList
  415. {
  416. get
  417. {
  418. throw new NotImplementedException();
  419. }
  420. set
  421. {
  422. throw new NotImplementedException();
  423. }
  424. }
  425. string ISimpleRequest.Flag
  426. {
  427. get
  428. {
  429. throw new NotImplementedException();
  430. }
  431. set
  432. {
  433. throw new NotImplementedException();
  434. }
  435. }
  436. }
  437. public class checkinprocess
  438. {
  439. public Int32? jobworkflowid { get; set; }
  440. public Int32? isexe { get; set; }
  441. public Int32? isservice { get; set; }
  442. public Int32? applicationserverid { get; set; }
  443. public string paramss { get; set; }
  444. public string ipaddress { get; set; }
  445. public string appname { get; set; }
  446. public string stagetype { get; set; }
  447. public string file_movement { get; set; }
  448. public string root_path { get; set; }
  449. public Int32? stageorder { get; set; }
  450. public string checkout_app { get; set; }
  451. public Int32? appid { get; set; }
  452. public string username { get; set; }
  453. public string password { get; set; }
  454. }
  455. public interface ISimpleRequest
  456. {
  457. DateTime Timestamp { get; }
  458. string CustomerId { get; }
  459. string TransId { get; set; }
  460. string UserId { get; set; }
  461. string AppType { get; set; }
  462. string StageType { get; set; }
  463. string Enviroment { get; set; }
  464. object DynamicList { get; set; }
  465. string Flag { get; set; }
  466. }
  467. public interface ISimpleResponse
  468. {
  469. string CusomerName { get; }
  470. }
  471. public class DynamicParams
  472. {
  473. public string jobworkflowid { get; set; }
  474. public string nexttranid { get; set; }
  475. public string extravalues { get; set; }
  476. }
  477. }