我在asp.net中写了一个webservice,它只是通过存储过程将值插入到表中。 Webservice是通过Android应用程序访问的。我的服务应该返回TRUE(在插入的情况下)或FALSE(在插入失败的情况下)但它也可能引起看起来很奇怪的错误(例外),普通用户无法理解技术术语,所以如何隐藏这些细节并显示正确的消息(或重定向)。这可能是很多错误,例如Outofrange异常,NullException等。
WebServive:
public GPSWebservice () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public bool insertCoordinates(string param)
{
BLL bll = new BLL();
return bll.InsertCoordinates(param);
}
}
BLL
public bool InsertCoordinates(string param)
{
String[] parts = param.Split(',');
sqlCom.CommandText = "InsertCoordinates";
sqlCom.CommandType = CommandType.StoredProcedure;
bool result ;
sqlCom.Parameters.Add("@AddedDateTime", SqlDbType.VarChar).Value = Convert.ToDateTime(parts[0].ToString());
sqlCom.Parameters.Add("@IMEI", SqlDbType.VarChar).Value = parts[1].ToString();
sqlCom.Parameters.Add("@RecordedDateTime", SqlDbType.VarChar).Value = Convert.ToDateTime(parts[2].ToString());
sqlCom.Parameters.Add("@Latitude", SqlDbType.VarChar).Value = parts[3].ToString(); ;
sqlCom.Parameters.Add("@Longitude", SqlDbType.VarChar).Value = parts[4].ToString(); ;
sqlCom.Parameters.Add("@IsParking ", SqlDbType.Bit).Value = ((parts[5].ToString().Trim()).Equals("0")) ? false : true;
SqlParameter sqlParam = new SqlParameter("@result", SqlDbType.Int);
sqlCom.Parameters.Add(sqlParam);
sqlCom.Parameters["@result"].Direction = ParameterDirection.Output;
try
{
sqlCon.Open();
sqlCom.ExecuteNonQuery();
return result = Convert.ToBoolean(sqlParam.Value);
}
catch (Exception ex)
{
return false;
}
finally
{
sqlCon.Close();
}
}
}
SP:
ALTER PROCEDURE [dbo].[InsertCoordinates]
@AddedDateTime varchar(50),
@IMEI varchar(50),
@RecordedDateTime varchar(50),
@Latitude varchar(50),
@Longitude varchar(50),
@IsParking bit,
@result int output
AS
BEGIN
SET NOCOUNT ON;
Begin Try
If Not exists(select IMEI from dbo.Coordinates where IMEI =@IMEI AND Latitude=@Latitude AND @Longitude=@Longitude)
Begin
insert into dbo.Coordinates
values (@AddedDateTime, @IMEI, @RecordedDateTime, @Latitude, @Longitude, @IsParking)
Set @result = @@ROWCOUNT
IF(@result = 1)
Select @result
Else
Set @result=0
Select @result
End
Else
Begin
Set @result=0
Select @result
End
End Try
Begin Catch
Set @result=0
End Catch
END
答案 0 :(得分:1)
取决于。
您可以捕获特定类型的异常并将其“翻译”为人类可读的内容。
try
{
// do stuff
}
catch (OracleException ex)
{
if (ex.Code == 15) throw new MyApplicationException("Something your users might make sense of", ex); // Code==15 is made-up
}
// Catch more specific errors where you know what happened and can give meaningful information to the user
catch (Exception ex)
{
throw new MyApplicationException("General error message: Something went horribly wrong", ex);
}
替代方法是不返回布尔值,而是返回自定义类型:
public class ServiceResult
{
public bool Success { get; set; }
public string Message { get; set; }
}
public ServiceResult InsertCoordinates(string param)
{
// ... code
try
{
sqlCon.Open();
sqlCom.ExecuteNonQuery();
return new ServiceResult() { Success = True };
}
catch (Exception ex)
{
return new ServiceResult() { Success = False, Message = "Crash & Burn!" };
}
// ... more code
}
哪个更好取决于您的体系结构以及您希望如何使用服务的结果。
答案 1 :(得分:0)
你可以这样做:
public enum ErrorCode
{
NoError,
DatabaseError,
UnknownError,
OutofrangeError
}
[WebMethod]
public ErrorCodes insertCoordinates(string param)
{
ErrorCodes result = ErrorCodes.NoError;
try
{
BLL bll = new BLL();
bool dbResult = bll.InsertCoordinates(param);
if (!dbResult) {result = ErrorCodes.DatabaseError;}
}
catch (OutOfRangeException ex) {result = ErrorCodes.OutofrangeError;}
catch (Exception ex) {result = ErrorCodes.UnknownError;}
return result;
}