April 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Develop Transactional .NET Web Services, Page 3

  • July 26, 2004
  • By Thiru Thangarathinam, Thiru Thangarathinam
  • Send Email »
  • More Articles »
Implementation of EmployeeService Web Service

Similar to the DeptService class, the EmployeeService class performs operations related to an employee. This article considers only the addition of a new employee using the EmployeeService class. (Click here to download the code for this article.)

Because most of the code is similar to the DeptService class, I will explain only the lines that are unfamiliar to you.

In the AddDept method, you controlled the outcome of the transaction by using the ContextUtil.SetComplete method (if everything went fine) and the ContextUtil.SetAbort method (if any exceptions occurred during the execution). You can achieve a similar result by using an attribute named AutoComplete. Instead of calling the SetComplete and SetAbort methods, you can delegate this responsibility to the AutoComplete attribute, which automatically calls either SetComplete (if no exceptions occur) or SetAbort (if an exception is thrown). For the AddEmp method, you will use the AutoComplete method:

[WebMethod(false,TransactionOption.Required)]
   [AutoComplete]
   public int AddEmp(string empName,string empAddress,int deptNo)
   {
      try
      {
      string connString =
         System.Configuration.ConfigurationSettings.AppSettings
         ["connectionString"];
      int empNo;
      SqlConnection sqlConn = new SqlConnection(connString);
      //Open the Connection to the database
      sqlConn.Open();

      //Create and set the SqlCommand object options
      SqlCommand sqlComm  = new SqlCommand("AddEmp",sqlConn);
      sqlComm.CommandType = CommandType.StoredProcedure;

      //Set the EmpName parameter
      SqlParameter paramName = new
         SqlParameter("@EmpName",SqlDbType.VarChar,50);
      paramName.Direction    = ParameterDirection.Input;
      paramName.Value        = empName;
      sqlComm.Parameters.Add(paramName);

      //Set the Address parameter
      SqlParameter paramAddress =
         new SqlParameter("@Address",SqlDbType.VarChar,250);
      paramAddress.Direction = ParameterDirection.Input;
      paramAddress.Value = empAddress;
      sqlComm.Parameters.Add(paramAddress);

      //Set the DeptNo parameter
      SqlParameter paramDeptNo =
         new SqlParameter("@DeptNo",SqlDbType.Int,4);
      paramDeptNo.Value = deptNo;
      paramDeptNo.Direction = ParameterDirection.Input;
      sqlComm.Parameters.Add(paramDeptNo);

      //Set the EmpNo parameter as the Stored Procedure return value
      SqlParameter paramEmpNo =
         new SqlParameter("@EmpNo",SqlDbType.Int,4);
      paramEmpNo.Direction = ParameterDirection.ReturnValue;
      sqlComm.Parameters.Add(paramEmpNo);

      //Execute the sql stored procedure
      sqlComm.ExecuteNonQuery();
      empNo = (int)sqlComm.Parameters["@EmpNo"].Value;
      return empNo;
   }
   catch(Exception e)
   {
      throw e;
   }
}

Now that you have seen the code required for implementing transactions in Web services, you can move on to the implementation of the client applications that invoke these Web services.

Implementation of client applications

To demonstrate the transactional behavior—and usefulness—of the Web services, you will create client applications that can initiate a transaction and then include your EmployeeService and DeptService Web services as part of that transaction. You'll learn how these transactional Web services can all be part of a single transaction, hence the operations they perform either commit in their entirety or completely roll back. The two types of client applications you will create are:

  • ASP.NET Web service application
  • ASP.NET Web application

You will start by creating an ASP.NET Web service client application for your DeptService and EmployeeService Web services.

Implementation of ASP.NET Web Service Client Application

To start, add a new ASP.NET Web service class named DeptEmployeesService to your WebServiceTransactions project.

Once you've created the project, rename the default Web service to DeptEmployeesService. Add a new method called AddDeptEmployees to the code-behind file of the Web service. As the name of the method suggests, it will invoke the methods of your transactional Web services (DeptService and EmployeeService) to first add a new Dept and then add employees to that Dept. By making these operations part of a single transaction initiated by the Web service, you ensure that either both the Dept and Employee details are added or nothing is added. The code for the AddDeptEmployees method looks like the following:

[WebMethod(false,TransactionOption.Required)]
public bool AddDeptEmployees(string deptName,string deptLocation,
string empName,string empAddres)
{

   try
   {
   int deptNo;
      Dept deptDetails = new Dept();
      Emp empDetails   = new Emp();
      //Add the Dept details to the database
      deptNo = deptDetails.AddDept(deptName,deptLocation);

      //Add the Employee details to the database
      int empNo = empDetails.AddEmp(empName,empAddres,deptNo);
      return true;
   }
   catch(Exception e)
   {
      throw e;
   }
}

The WebMethod attribute indicates that the AddDeptEmployees method is to be exposed as a Web service, and when it is deployed, the Visual Studio.NET runtime provides all the plumbing required to make this method discoverable to the clients that wish to use this Web service. The code also specifies that you want your Web service to run in a transaction by using the TransactionOption.Required enum constant.

Next, create instances of the DeptService and EmployeeService classes, and then call the AddDept and AddEmp methods to add the dept and employee details to the database:

      //Add the Dept details to the database
      deptNo = deptDetails.AddDept(deptName,deptLocation);
      //Add the Employee details to the database
      int empNo = empDetails.AddEmp(empName,empAddres,deptNo);
      return true;
   }
   catch(Exception e)
   {
      throw e;
   }
}

You can test the above code by navigating to the path http://localhost/MyProjects/WebServiceTransactions/DeptEmployeesService.asmx. As a result, you should see a screen similar to Figure 2.



Click here for a larger image.

Figure 2. Test Harness Generated by ASP.NET

Using the above test harness (generated by the ASP.NET), you can test your Web service by supplying the required parameters. When you invoke the Web service, you get the screen displayed in Figure 3.



Click here for a larger image.

Figure 3. Test Your Web Service by Supplying the Required Parameters

Now, if you go back to the database, you will find that both the dept and employee details have been added successfully. You can make a slight modification to your code to see whether the DeptService and EmployeeService objects are running part of the single transaction initiated by the Web service. In the AddDeptEmployees method, you'd modify the following line of code:

int empNo = empDetails.AddEmp(empName,empAddres,deptNo);

In the above line, you replace the deptNo variable with the value of 100 (assuming no dept has the DeptNo 100 in the Dept table). After modification, it looks like the following:

int empNo = empDetails.AddEmp(empName,empAddres,100);

Now, if you execute the Web service, you will get the exception message in Figure 4, which cites a Foreign Key Violation. And, if you check the database, you will find that no values have been added to either the Dept table or the Emp table. This clearly shows that both the objects are running part of the same transaction initiated by the Web service.



Click here for a larger image.

Figure 4. Exception Message Citing a Foreign Key Violation





Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel