Answered by:
How can i call a windows service from my Web App ?

Question
-
User1253338400 posted
Hi ,
I have a web page which dispaly user input fields . "firstname" , "lastname" etc
How can i get a windows service to get the data in the input fields and write the data to a database ?
I have a solution with a web app that gets the data and on a button click it inserts into a database , But i want it so that the button is clicked then the windows service will insert the data.
Thanks
Wednesday, December 12, 2018 4:54 AM
Answers
-
User-330142929 posted
Hi Robby32,
What do you mean calling a windows service? in my opinion, there are some web services hosted in windows service. one of these ideas is that Inserting the record to the database via calling a WCF service hosted in windows service,
I have made a demo, wish it is useful to you.
Windows service.Uri uri = new Uri("http://localhost:1000"); BasicHttpBinding binding = new BasicHttpBinding(); ServiceHost sh = null; protected override void OnStart(string[] args) { sh = new ServiceHost(typeof(MyService), uri); ServiceMetadataBehavior smb; smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (smb==null) { smb = new ServiceMetadataBehavior { HttpGetEnabled = true }; sh.Description.Behaviors.Add(smb); } sh.Open(); WriteLog($"Service is ready at {DateTime.Now.ToString("hh-mm-ss")}"); } protected override void OnStop() { if (sh!=null&&sh.State==CommunicationState.Opened) { sh.Close(); WriteLog($"Service is closed at {DateTime.Now.ToString("hh-mm-ss")}"); } } public static void WriteLog(string text) { using (StreamWriter sw=File.AppendText(@"D:\log.txt")) { sw.WriteLine(text); sw.Flush(); } } } [ServiceContract(Namespace ="mydomain")] public interface IService { [OperationContract] string Insert(Person p); } public class MyService : IService { public string Insert(Person p) { string connectionstring = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MyStore;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; SqlConnection connection = new SqlConnection(connectionstring); string text = "insert into people(FirstName,LastName)values('" + p.FirstName + "','" + p.LastName + "')"; SqlCommand command = new SqlCommand(text, connection); connection.Open(); int result = command.ExecuteNonQuery(); if (result==1) { connection.Close(); Service1.WriteLog("success"); return "success"; } else { connection.Close(); Service1.WriteLog("failed"); return "failed"; } } } [DataContract] public class Person { [DataMember] public string FirstName { get; set; } [DataMember] public string LastName { get; set; } }
Webapplication.protected void Button1_Click1(object sender, EventArgs e) { ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient(); Person p = new Person(); p.FirstName = TextBox1.Text; p.LastName = TextBox2.Text; string result = client.Insert(p); ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + result+ "')", true); }
Web applications invoke WCF services hosted in Windows services via adding references. One thing must be noted is that the account which executes the windows service must have the access to execute the SQL statement.
Or you can write a SQL statement to a message queue by clicking event, and then the windows service extracts the message queue and executes the SQL statement (You can think of it as writing SQL statements to a local file, and then Windows service listens for local file changes).
Feel free to let me know if there is anything I can help with.
Best Regards
Abraham- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 13, 2018 10:52 AM
All replies
-
User-330142929 posted
Hi Robby32,
What do you mean calling a windows service? in my opinion, there are some web services hosted in windows service. one of these ideas is that Inserting the record to the database via calling a WCF service hosted in windows service,
I have made a demo, wish it is useful to you.
Windows service.Uri uri = new Uri("http://localhost:1000"); BasicHttpBinding binding = new BasicHttpBinding(); ServiceHost sh = null; protected override void OnStart(string[] args) { sh = new ServiceHost(typeof(MyService), uri); ServiceMetadataBehavior smb; smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (smb==null) { smb = new ServiceMetadataBehavior { HttpGetEnabled = true }; sh.Description.Behaviors.Add(smb); } sh.Open(); WriteLog($"Service is ready at {DateTime.Now.ToString("hh-mm-ss")}"); } protected override void OnStop() { if (sh!=null&&sh.State==CommunicationState.Opened) { sh.Close(); WriteLog($"Service is closed at {DateTime.Now.ToString("hh-mm-ss")}"); } } public static void WriteLog(string text) { using (StreamWriter sw=File.AppendText(@"D:\log.txt")) { sw.WriteLine(text); sw.Flush(); } } } [ServiceContract(Namespace ="mydomain")] public interface IService { [OperationContract] string Insert(Person p); } public class MyService : IService { public string Insert(Person p) { string connectionstring = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MyStore;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; SqlConnection connection = new SqlConnection(connectionstring); string text = "insert into people(FirstName,LastName)values('" + p.FirstName + "','" + p.LastName + "')"; SqlCommand command = new SqlCommand(text, connection); connection.Open(); int result = command.ExecuteNonQuery(); if (result==1) { connection.Close(); Service1.WriteLog("success"); return "success"; } else { connection.Close(); Service1.WriteLog("failed"); return "failed"; } } } [DataContract] public class Person { [DataMember] public string FirstName { get; set; } [DataMember] public string LastName { get; set; } }
Webapplication.protected void Button1_Click1(object sender, EventArgs e) { ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient(); Person p = new Person(); p.FirstName = TextBox1.Text; p.LastName = TextBox2.Text; string result = client.Insert(p); ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + result+ "')", true); }
Web applications invoke WCF services hosted in Windows services via adding references. One thing must be noted is that the account which executes the windows service must have the access to execute the SQL statement.
Or you can write a SQL statement to a message queue by clicking event, and then the windows service extracts the message queue and executes the SQL statement (You can think of it as writing SQL statements to a local file, and then Windows service listens for local file changes).
Feel free to let me know if there is anything I can help with.
Best Regards
Abraham- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 13, 2018 10:52 AM -
User1253338400 posted
Hi ,
Thanks so much , this was very helpful.
cheers
Sunday, December 16, 2018 9:16 AM