mar*_*raz 31 .net c# wcf web-services
我正在开发分布式应用程序.在其中,我必须验证角色和权限集.在例如未经授权的访问中
抛出异常是一个很好的实践吗?
或者我应该向客户发送一些消息?
小智 55
在您的服务操作中,您可以指定一个FaultContract,它将同时满足这两个目的:
[OperationContract]
[FaultContract(typeof(MyServiceFault))]
void MyServiceOperation();
Run Code Online (Sandbox Code Playgroud)
请注意,MyServiceFault必须使用与复杂类型相同的方式标记DataContract和DataMember属性:
[DataContract]
public class MyServiceFault
{
private string _message;
public MyServiceFault(string message)
{
_message = message;
}
[DataMember]
public string Message { get { return _message; } set { _message = value; } }
}
Run Code Online (Sandbox Code Playgroud)
在服务方面,您可以:
throw new FaultException<MyServiceFault>(new MyServiceFault("Unauthorized Access"));
Run Code Online (Sandbox Code Playgroud)
在客户端:
try
{
...
}
catch (FaultException<MyServiceFault> fault)
{
// fault.Detail.Message contains "Unauthorized Access"
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*son 12
好吧,您可以捕获WCF服务实现方法中的所有异常,并将它们重新抛出为FaultExceptions.通过这种方式,将在客户端上重新抛出异常,并显示您选择的消息:
[OperationContract]
public List<Customer> GetAllCustomers()
{
try
{
... code to retrieve customers from datastore
}
catch (Exception ex)
{
// Log the exception including stacktrace
_log.Error(ex.ToString());
// No stacktrace to client, just message...
throw new FaultException(ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
为了避免将意外错误转发回客户端,从不在服务器端的代码中抛出异常实例也是一种好习惯.而是创建一个或多个自己的异常类型并抛出它们.通过这样做,您可以区分意外的服务器处理错误和由于无效请求而引发的错误等:
public List<Customer> GetAllCustomers()
{
try
{
... code to retrieve customers from datastore
}
catch (MyBaseException ex)
{
// This is an error thrown in code, don't bother logging it but relay
// the message to the client.
throw new FaultException(ex.Message);
}
catch (Exception ex)
{
// This is an unexpected error, we need log details for debugging
_log.Error(ex.ToString());
// and we don't want to reveal any details to the client
throw new FaultException("Server processing error!");
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
32076 次 |
最近记录: |