1
Reply

WebService Log....

rajesh N

rajesh N

Apr 2 2008 8:13 PM
2.7k
Hi Gurus, I'm using VS2005, In that I've the following WebService code
What I need is I want to write a log to a Textfile. Just give me a suggestion how to write a log entry on the code as follows:


1) Class: ValidateImageAuthorization.cs

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
//using System.Web.Services.Configuration;


[WebService(Namespace = "http://webservice.imaging.yellowbook.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//added for testing
//[WebMethod(1,Random,10,10)]

public class ValidateImageAuthorization : System.Web.Services.WebService
{
public ValidateImageAuthorization()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}

#region User accessable function

// Gets user info(userid,roles,displayname,application type)
// Returns session unique identifier to requested user
[WebMethod(EnableSession = true)]


public Response CreateAuthorization(Request UserInfo)
{
string _SessionIdentifier;
string _DbReturnMessage;
Response objReturn = new Response();


try
{
// create new session identifier
_SessionIdentifier = Guid.NewGuid().ToString();

// Submits data (user id,unique session id,roles info,Display Name) into imaging database
_DbReturnMessage = IsUserInfoAddedIntoDB(UserInfo,_SessionIdentifier);

// Check user info successfully submitted to imaging database or not
if (_DbReturnMessage == "Success")
{
//In case of successful database submission, returns unique session id with return code(0).
objReturn.SessionIdentifier = _SessionIdentifier;
objReturn.Message = "Success";
objReturn.Returncode = 0;
}
else
{
//any issue with database updation, returns blank session identifier with appropriate return code(-1) & appropriate error message
objReturn.SessionIdentifier = "";
objReturn.Message = _DbReturnMessage;
objReturn.Returncode = -1;
}
return objReturn;
}
catch (Exception ex)
{
// returns error message to the user while facing any issue with Web Service itself
objReturn.SessionIdentifier = "";
objReturn.Message = ex.Message;
objReturn.Returncode = -1;
return objReturn;
}
}
#endregion



#region function to submits user information into YB imaging database

// Gets session unique identifier,userid,roles and display name as input parameters
public string IsUserInfoAddedIntoDB(Request UserInfo, string SessionID)
{
string IsDataValid;
int CCRole = 0, SSNRole = 0;
SqlCommand cmd = null;
SqlConnection connection = null;
SqlDataReader reader = null;

//Check whether input parameters for database updation is valid or not
IsDataValid=IsInputDataValid(UserInfo);

//In case of all invalid input parameters, returns appropriate error message.
if (IsDataValid != "InputDataValid") return IsDataValid;

//Code commented for updating DB(i.e UniqueSessionID table) for SSN & CC information
////In case of all valid input parameters
////filter Roles fields and search for ImageCC & ImageSSN roles info out of that.
//for (int i = 0; i < UserInfo.Roles.Length; i++)
//{
// //setting CCRole=1 if user has got access to view Credit card information out of contract images
// if (UserInfo.Roles[i].ToLower() == "imagecc")
// {
// CCRole = 1;
// SSNRole = 0;
// i = UserInfo.Roles.Length - 1;
// }
// //setting SSNRole=1 if user has got access to view SSN information out of contract images
// else if (UserInfo.Roles[i].ToLower() == "imagessn")
// {
// CCRole = 0;
// SSNRole = 1;
// i = UserInfo.Roles.Length - 1;
// }
// //setting CCRole=0 and SSNRole = 0, if user has got access to view SSN information out of contract images
// else
// {
// CCRole = 0;
// SSNRole = 0;
// }
//}
//Comment Ends


//Code Added by Prasanna on 1-Aug-2007 for updating DB(i.e UniqueSessionID)for SSN & CC information
//Code Starts
//In case of all valid input parameters
//filter Roles fields and search for ImageCC & ImageSSN roles info out of that.
object objcc = "imagecc";
object objssn = "imagessn";

// converting all elements to lower case
for (int i = 0; i < UserInfo.Roles.Length; i++)
{
UserInfo.Roles[i] = UserInfo.Roles[i].ToLower().Trim();
}

// Sorting all elements of Roles Array
Array.Sort(UserInfo.Roles);

// Searching for imagecc and imagessn in Roles array.(If array has both elements)
//ImageCC, ImageSSN
if (Array.BinarySearch(UserInfo.Roles, objcc) > -1 && Array.BinarySearch(UserInfo.Roles, objssn) > -1)
{
CCRole = 1; SSNRole = 1;
}
else
{
//Searching for imagecc in Roles array.
//If array has imagecc and other elements and not imagessn
if (Array.BinarySearch(UserInfo.Roles, objcc) > -1)
{
CCRole = 1; SSNRole = 0;
}

//Searching for imagessn in Roles array.
//If array has imagessn and other elements and not imagecc
else if (Array.BinarySearch(UserInfo.Roles, objssn) > -1)
{
CCRole = 0; SSNRole = 1;
}
//If array has other than imagecc and imagessn elements
else
{
CCRole = 0; SSNRole = 0;
}
}
//Code Ends

//gets Stored procedure name from Web.Config file
string cmdString = ConfigurationManager.AppSettings["SPDetails"];
//gets SQL Connection string from Web.Config file
string connectionInfo = ConfigurationManager.AppSettings["DbConstr"];
connection = new SqlConnection(connectionInfo);

try
{
connection.Open();
cmd = new SqlCommand(cmdString, connection);
cmd.CommandType = CommandType.StoredProcedure;

//passing i/p parameters to Stored procedure for database submission
cmd.Parameters.Add("@USID", SqlDbType.VarChar).Value = SessionID;
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = UserInfo.UserId;
cmd.Parameters.Add("@DISPLAYNAME", SqlDbType.NVarChar).Value = UserInfo.DisplayName;
cmd.Parameters.Add("@SSROLE", SqlDbType.Bit).Value = SSNRole;
cmd.Parameters.Add("@CCROLE", SqlDbType.Bit).Value = CCRole;

reader = cmd.ExecuteReader();
reader.Read();

//check whether stored procedure face any issue while inserting/updating data to imaging database
//Returns Errorcode=-1 in case of issues else returns 0
if (reader["ERRORCODE"].ToString() == "-1")
{
return reader["MESSAGE"].ToString();
}
else
{
return "Success";
}
}

catch (Exception e)
{
return e.Message;
}

finally
{
//Close the database connection
if (connection != null)
{
connection.Close();
}
//close the reader
if (reader != null)
{
reader.Close();
}
}

}

//Function validates input parameters for database submission
private string IsInputDataValid(Request UserInfo)
{
//check if Web service is not supplied any input parameters
if (
UserInfo.Roles.Length == 0 &&
UserInfo.UserId.Length == 0 &&
UserInfo.DisplayName.Length == 0 &&
UserInfo.ApplicationType.Length == 0
)
{
//return error message
return "Web Service is not supplied with any single input parameters";
}

//check length of Userid whether valid or not
if (UserInfo.UserId.Trim().Length == 0)
{
return "Input Parameter - Invalid UserID";
}

//check length of Display name whether valid or not
if (UserInfo.DisplayName.Trim().Length == 0)
{
return "Input Parameter - Invalid Display Name";
}

//check length of Roles info whether valid or not
if (UserInfo.Roles.Length == 0)
{
return "Input Parameter- Invalid Roles info";
}
else
{
//Check whether Array has not got string with space value
bool HasValue = false;

for (int counter = 0; counter < UserInfo.Roles.Length; counter++)
{
if (UserInfo.Roles[counter].Trim().Length != 0)
{
HasValue = true;
counter = UserInfo.Roles.Length - 1;
}
}
if (HasValue == false) return "Input Parameter - Roles must have least one value";
}

//check length of Application Type whether valid or not
if (UserInfo.ApplicationType.Trim().Length == 0)
{
return "Input Parameter - Invalid Application Type";
}
return "InputDataValid";
}
#endregion

#region web service input and output information structure
public class Request
{
public string UserId;
public string[] Roles;
public string DisplayName;
public string ApplicationType;
}

public class Response
{
public string SessionIdentifier;
public string Message;
public int Returncode;
}
#endregion
}

Answers (1)