using MassTransit; using MassTransit.Util; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Threading.Tasks; namespace Message.MessageTypes { public class RabbitMq { private readonly IRequestClient _requestClient; public RabbitMq(IRequestClient requestClient) { _requestClient = requestClient; } //CommonRepository oCommonRepository = new CommonRepository(); //public bool SendMq(string Tranid, string UserId, string AppType, string StageType, string Enviroment) //{ // //oCommonRepository.WriteLog("SendMq:", "Main App"); // string ServiceAddress = ""; // string RabbitMQHost = ""; // string userName = ""; // string password = ""; // using (IDbConnection dbConnection = ConnectionBase.Connection) // { // dbConnection.Open(); // DynamicParameters param = new DynamicParameters(); // param.Add("p_tranid", dbType: DbType.Int32, value: Tranid, direction: ParameterDirection.Input); // param.Add("p_userid", dbType: DbType.Int32, value: UserId, direction: ParameterDirection.Input); // param.Add("p_status", dbType: DbType.String, value: "P", direction: ParameterDirection.Input); // param.Add("p_option", dbType: DbType.String, value: AppType, direction: ParameterDirection.Input); // var Checkinapp = dbConnection.Query("public.spusrlottranupdatev1", param, commandType: CommandType.StoredProcedure).ToList(); // if (Checkinapp != null) // { // // var i_Checkinapp = JsonConvert.DeserializeObject(Checkinapp[0].ToString()); // // checkinprocess obj = JsonConvert.DeserializeObject(Checkinapp[0].ToString()); // var next_ordernodata10 = JsonConvert.DeserializeObject(Checkinapp[0].ToString()); // JArray dtparallelnextstage10 = JArray.Parse(next_ordernodata10.ToString()); // checkinprocess obj = JsonConvert.DeserializeObject(dtparallelnextstage10[0].ToString()); // if (obj.ipaddress != null) // { // ServiceAddress = obj.ipaddress + "" + "/" + obj.root_path; // RabbitMQHost = obj.ipaddress; // userName = obj.username; // password = obj.password; // StageType = obj.stagetype; // } // if (StageType != "server") // return false; // } // } // var datetime = DateTime.Now.ToString(); // var message = new PublishMessage(datetime, Tranid, UserId, AppType, StageType, Startup.Configuration.GetValue("AppSettings:envirnmentname"), null, ""); // var task = Publish(message); // if (task.Result != true) // { // return false; // } // return true; //} //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 = "" ) //{ // string RabbitMQHost = ""; // oCommonRepository.WriteLog("RabbitMq", "SendMq_WorkArea"); // using (IDbConnection dbConnection = ConnectionBase.Connection) // { // dbConnection.Open(); // DynamicParameters param = new DynamicParameters(); // param.Add("p_tranid", dbType: DbType.Int32, value: Convert.ToInt32(Tranid), direction: ParameterDirection.Input); // param.Add("p_userid", dbType: DbType.Int32, value: Convert.ToInt32(UserId), direction: ParameterDirection.Input); // param.Add("p_status", dbType: DbType.String, value: "P", direction: ParameterDirection.Input); // param.Add("p_option", dbType: DbType.String, value: AppType, direction: ParameterDirection.Input); // var Checkinapp = dbConnection.Query("public.spusrlottranupdatev1", param, commandType: CommandType.StoredProcedure).ToList(); // if (Checkinapp != null) // { // oCommonRepository.WriteLog("SendMq_WorkArea", Checkinapp[0].ToString()); // var next_ordernodata10 = JsonConvert.DeserializeObject(Checkinapp[0].ToString()); // JArray dtparallelnextstage10 = JArray.Parse(next_ordernodata10.ToString()); // checkinprocess obj = JsonConvert.DeserializeObject(dtparallelnextstage10[0]["json_build_object"].ToString()); // RabbitMQHost = Startup.Configuration.GetValue("AppSettings:chechoutqueue_path"); // oCommonRepository.WriteLog("SendMq_WorkArea:RabbitMQHost", RabbitMQHost); // if (obj.stagetype != null) // { // StageType = obj.stagetype; // } // } // } // oCommonRepository.WriteLog("SendMq_WorkArea:Tranid" + Tranid, AppType); // var datetime = DateTime.Now.ToString(); // var message = new PublishMessage(datetime, Tranid, UserId, AppType, StageType, Startup.Configuration.GetValue("AppSettings:envirnmentname"), extravalues, cflag, Batch, PublicationType, comments, PublicationID); // var task = Publish(message); // if (task.Result != true) // { // return false; // } // return true; //} public bool SendMq_StageComplete(string Tranid, string UserId, string AppType, string StageType, string Enviroment, string cflag, string extravalues) { //string exchangeName = Startup.Configuration.GetValue("AppSettings:chechoutqueue_exchange"); Enviroment = ConfigurationManager.AppSettings["Environment"]; var datetime = DateTime.Now.ToString(); var message = new PublishMessage("", Tranid, UserId, AppType, StageType, Enviroment,null, cflag); var task = Publish(message); if (task.Result != true) { return false; } return true; } public async Task Publish(PublishMessage message) { //string ServiceAddress = Startup.Configuration.GetValue("AppSettings:chechoutqueue_path") + "" + "/" + Startup.Configuration.GetValue("AppSettings:chechoutqueue"); //string QueueName = Startup.Configuration.GetValue("AppSettings:chechoutqueue"); //string RabbitMQHost = Startup.Configuration.GetValue("AppSettings:chechoutqueue_path"); string ServiceAddress = ConfigurationManager.AppSettings["chechoutqueue_path"] + "" + "/" + ConfigurationManager.AppSettings["chechoutqueue"]; string QueueName = ConfigurationManager.AppSettings["chechoutqueue"]; string RabbitMQHost = ConfigurationManager.AppSettings["chechoutqueue_path"]; string userName = "admin"; //obj.username; string password = "admin"; //obj.password; var _busControl = Bus.Factory.CreateUsingRabbitMq(x => { var host = x.Host(new Uri(RabbitMQHost), h => { h.Username(userName); h.Password(password); }); }); try { //TaskUtil.Await(() => _busControl.StartAsync()); Uri ServiceAddress_uri = new Uri(ServiceAddress); var endPoint = await _busControl.GetSendEndpoint(ServiceAddress_uri); await endPoint.Send(message); //await _busControl.Publish(message); } catch (Exception ex) { //oCommonRepository.ErrorLog(ex); // Console.WriteLine($"Error publishing message: {ex.Message}"); return false; } finally { _busControl.Stop(); } return true; } //public async Task Publish_MailQueue(PublishMessage message) //{ // string ServiceAddress = Startup.Configuration.GetValue("AppSettings:mailqueue_path") + "" + "/" + Startup.Configuration.GetValue("AppSettings:mailqueue"); // string RabbitMQHost = Startup.Configuration.GetValue("AppSettings:mailqueue_path"); // string userName = "admin"; //obj.username; // string password = "admin"; //obj.password; // var _busControl = Bus.Factory.CreateUsingRabbitMq(x => // { // var host = x.Host(new Uri(RabbitMQHost), h => // { // h.Username(userName); // h.Password(password); // }); // }); // try // { // TaskUtil.Await(() => _busControl.StartAsync()); // Uri ServiceAddress_uri = new Uri(ServiceAddress); // var endPoint = await _busControl.GetSendEndpoint(ServiceAddress_uri); // await endPoint.Send(message); // // _busControl.Publish(message); // } // catch (Exception ex) // { // oCommonRepository.ErrorLog(ex); // return false; // } // finally // { // _busControl.Stop(); // } // return true; //} //public bool SendMq_Stage_Call_Web(string Tranid, string UserId, string AppType, string StageType, string Enviroment, string cflag, object extravalues = null) //{ // var datetime = DateTime.Now.ToString(); // var message = new PublishMessage(datetime, Tranid, UserId, AppType, StageType, Startup.Configuration.GetValue("AppSettings:envirnmentname"), extravalues, cflag); // var task = Publish(message); // if (task.Result != true) // { // return false; // } // return true; //} //public bool SendMqMail(string Tranid, string JobWorkflowid, string flag, string curtranid, string userid) //{ // oCommonRepository.WriteLog("SendMqMail started:", "RabbitMq"); // DynamicParams dynamicparams = new DynamicParams() // { // jobworkflowid = JobWorkflowid, // nexttranid = curtranid // }; // List dynamicparams_ = new List(); // dynamicparams_.Add(dynamicparams); // var datetime = DateTime.Now.ToString(); // var message = new PublishMessage(DateTime.Now.ToString(), Tranid, userid, "", "", Startup.Configuration.GetValue("AppSettings:envirnmentname"), dynamicparams, flag); // oCommonRepository.WriteLog("JobWorkflowid:" + JobWorkflowid, "RabbitMq-SendMqMail"); // oCommonRepository.WriteLog("curtranid:" + curtranid, "RabbitMq-SendMqMail"); // var task = Publish_MailQueue(message); // if (task.Result != true) // { // return false; // } // return true; //} //public bool SendMqTest(string Tranid, string JobWorkflowid, string flag, string curtranid, string userid) //{ // DynamicParams dynamicparams = new DynamicParams() // { // jobworkflowid = JobWorkflowid, // nexttranid = curtranid // }; // List dynamicparams_ = new List(); // dynamicparams_.Add(dynamicparams); // var message = new PublishMessage(DateTime.Now.ToString(), "123", userid, "", "", Startup.Configuration.GetValue("AppSettings:envirnmentname"), dynamicparams, flag); // oCommonRepository.WriteLog("JobWorkflowid:" + JobWorkflowid, "RabbitMq-SendMqMail"); // oCommonRepository.WriteLog("curtranid:" + curtranid, "RabbitMq-SendMqMail"); // var task = Publish_MailQueue(message); // if (task.Result != true) // { // return false; // } // return true; //} public IRequestClient CreateRequestClient(IBusControl busControl, string ServiceAddress) { var serviceAddress = new Uri(ServiceAddress); IRequestClient client = busControl.CreateRequestClient(serviceAddress, TimeSpan.FromSeconds(30)); return client; } public IBusControl CreateBus(string RabbitMQHost, string userName, string password) { var currentBus = Bus.Factory.CreateUsingRabbitMq(x => { var Host = x.Host(new Uri(RabbitMQHost), h => { h.Username(userName); h.Password(password); }); }); return currentBus; } static void ConfigureLogger() { // const string logConfig = @" // // // // // // // // // // //"; // using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(logConfig))) // { // XmlConfigurator.Configure(stream); // } } } //public class PublishMessage //{ // public PublishMessage(string customerId, string Tranid, string UserId, string AppType, string StageType, string Enviroment, object DynamicList, string Flag) // { // _customerId = customerId; // _TransId = Tranid; // _UserId = UserId; // _AppType = AppType; // _StageType = StageType; // _Enviroment = Enviroment; // _timestamp = DateTime.UtcNow; // _DynamicList = DynamicList; // _Flag = Flag; // } // public string _customerId { get; set; } // public string _TransId { get; set; } // public string _UserId { get; set; } // public string _AppType { get; set; } // public string _StageType { get; set; } // public string _Enviroment { get; set; } // public DateTime _timestamp { get; set; } // public object _DynamicList { get; set; } // public string _Flag { get; set; } //} class SimpleRequest : ISimpleRequest { readonly string _customerId; readonly string _TransId; readonly string _UserId; readonly string _AppType; readonly string _StageType; readonly string _Enviroment; readonly DateTime _timestamp; readonly object _DynamicList; readonly string _Flag; public SimpleRequest(string customerId, string Tranid, string UserId, string AppType, string StageType, string Enviroment, object DynamicList, string Flag) { _customerId = customerId; _TransId = Tranid; _UserId = UserId; _AppType = AppType; _StageType = StageType; _Enviroment = Enviroment; _timestamp = DateTime.UtcNow; _DynamicList = DynamicList; _Flag = Flag; } public DateTime Timestamp { get { return _timestamp; } } public string TransId { get { return _TransId; } } public string CustomerId { get { return _customerId; } } public string UserId { get { return _UserId; } } public string AppType { get { return _AppType; } } public string StageType { get { return _StageType; } } public string Enviroment { get { return _Enviroment; } } public object DynamicList { get { return _DynamicList; } } public string Flag { get { return _Flag; } } string ISimpleRequest.TransId { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } string ISimpleRequest.UserId { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } string ISimpleRequest.AppType { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } string ISimpleRequest.StageType { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } string ISimpleRequest.Enviroment { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } object ISimpleRequest.DynamicList { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } string ISimpleRequest.Flag { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } } public class checkinprocess { public Int32? jobworkflowid { get; set; } public Int32? isexe { get; set; } public Int32? isservice { get; set; } public Int32? applicationserverid { get; set; } public string paramss { get; set; } public string ipaddress { get; set; } public string appname { get; set; } public string stagetype { get; set; } public string file_movement { get; set; } public string root_path { get; set; } public Int32? stageorder { get; set; } public string checkout_app { get; set; } public Int32? appid { get; set; } public string username { get; set; } public string password { get; set; } } public interface ISimpleRequest { DateTime Timestamp { get; } string CustomerId { get; } string TransId { get; set; } string UserId { get; set; } string AppType { get; set; } string StageType { get; set; } string Enviroment { get; set; } object DynamicList { get; set; } string Flag { get; set; } } public interface ISimpleResponse { string CusomerName { get; } } public class DynamicParams { public string jobworkflowid { get; set; } public string nexttranid { get; set; } public string extravalues { get; set; } } }