catch(Exception ex)
{
//do what you want here
//When type of exception is System.Web.Services.Protocols.SoapException
//if (ex.Code.Name.Equals("Client"))
//{
// msg = "service's function not exist";
//}
//else if (ex.Code.Name.Equals("Server"))
//{
// msg = "function error"
//}
//else
//{
// msg = "unknown";
//}
//MessageBox.Show(msg, "error", MessageBoxButtons.OK);
**But ex is not System.Web.Services.Protocols.SoapException so I cannot call ex.Code.Name.Equals("Client")**
//When System.Net.WebException
//switch (ex.Status)
//{
// case System.Net.WebExceptionStatus.ConnectFailure:
// do some thing
break;
// case System.Net.WebExceptionStatus.Timeout:
//do some thing
break;
// case System.Net.WebExceptionStatus.ProtocolError:
switch (((System.Net.HttpWebResponse)ex.Response).StatusCode)
{
case System.Net.HttpStatusCode.NotFound:
//do some thing
break;
case System.Net.HttpStatusCode.ServiceUnavailable:
//do some thing
break;
case System.Net.HttpStatusCode.Unauthorized:
//do some thing
break;
default:
//do some thing
break;
}
break;
default:
//do some thing
break;
}
}
ただし、例外はSystem.Net.WebExceptionではありません。したがって、ex.Statusを呼び出すことはできません
私の問題:
Smartclientソフトウェアには、クライアントとしてWindowsForm、サーバーとしてWebサービスが含まれています。クライアントとサーバーはどちらも私がテストしたn層アプリケーションであり、クライアントからサービスを呼び出すときに問題があることがわかりました
- app.config内:サービスのパスが間違っています。System.NotSupportedExceptionをキャッチします
- または、サーバーが接続できない場合:System.Net.WebExceptionStatus
- サーバーのwebconfigが間違っています:System.InvalidOperationException
- サービスは例外をスローします:System.Web.Services.Protocols.SoapException ..。
私のアイデア
私は、他のすべての例外タイプの代表として、representativeAlExceptionである例外と呼びます 。私は名前空間を持っています:Commonと2つのクラスrepresentativeAlException.csとBusinessExceptionHandler.cs
paramを(representativeAlException ex)として使用して共通関数を作成します
try
{
Err_LogCheck.Service1.Service1 service = new Err_LogCheck.Service1.Service1();
return service.getDeviceByZero(ZERO);
}
catch (Common.representativeAlException ex)
{
Common.BusinessExceptionHandler.ProcessException(ex);
}
私がやりたいこと
サービスが呼び出される場所。すべてのタイプの例外を処理できるcatchブロックは1つだけです。
ProcessException(representativeAlException ex)関数で
switch (ex)
{
case System.InvalidOperationException:
//Do some thing
break;
case System.NotSupportedException:
//Do some thing
break;
case System.Web.Services.Protocols.SoapException:
//do some thing
break;
...
...