Introduction:
In this article,i am going to explain about how to do transactions in web services using Webmethod.Transactionoption property.
Main:
we can easily manage tranctions using webmethod.transaction property,
[WebMethod(TransactionOption= TransactionOption.RequiresNew)]
It Indicates that the XML Web service method requires a new transaction. When a request is processed, the XML Web
service is created within a new transaction.
for ex,
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod(TransactionOption =
System.EnterpriseServices.TransactionOption.RequiresNew)]
//[WebMethod]
public string execute_transaction()
{
SqlConnection con1 = new SqlConnection("server=my; user id=sa; password=; database=master");
SqlCommand com1 = new SqlCommand("insert abc values('A01','ABC1')", con1);
con1.Open();
com1.ExecuteNonQuery();
con1.Close();
SqlConnection con2 = new SqlConnection("server=my; user id=sa; password=; database=master");
SqlCommand com2 = new SqlCommand("insert def values('2ldwkjskl sj','ABCDEF"), con2);
con2.Open();
com2.ExecuteNonQuery();
con2.Close();
//throw new Exception("Hello");
return "Successfully Executed";
}
[WebMethod(CacheDuration=10)]
public string getdata(int i)
{
return System.DateTime.Now.ToLongTimeString();
}
}
using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Data; using System.Data.SqlClient; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { public Service () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod(TransactionOption = System.EnterpriseServices.TransactionOption.RequiresNew)] //[WebMethod] public string execute_transaction() { SqlConnection con1 = new SqlConnection("server=my; user id=sa; password=; database=master"); SqlCommand com1 = new SqlCommand("insert abc values('A01','ABC1')", con1); con1.Open(); com1.ExecuteNonQuery(); con1.Close(); SqlConnection con2 = new SqlConnection("server=my; user id=sa; password=; database=master"); SqlCommand com2 = new SqlCommand("insert def values('2ldwkjskl sj','ABCDEF"), con2); con2.Open(); com2.ExecuteNonQuery(); con2.Close(); //throw new Exception("Hello"); return "Successfully Executed"; } [WebMethod(CacheDuration=10)] public string getdata(int i) { return System.DateTime.Now.ToLongTimeString(); } } |
Conclusion:
Hope this helps,
Happy Coding.
This is a fantastic post about design. I’m a student just trying to learn more about trends and I really enjoyed it. Keep up the great job!