有两个用户:ManagerOne
和ManagerTwo
。在使用我的服务之前,他们必须登录
public class MyService : IMyService
{
int _userID = -1;
public bool LogIn(int userID, string password)
{
if( UserCheck(userID, password) )
_userID = userID;
else
throw new FaultException<InvalidUserFault>(new InvalidUserFault());
}
}
登录后,他们可以运行GetSome()
操作。这个operation
会返回DataContract
。此operation
只能运行ManagerOne
。如果ManagerTwo
运行GetSome()
,则operation
必须返回错误。问题是:什么是最佳做法 - return OperationResult<T>
或FaultException
(在此方案中)?
查看一些代码:
public class MyService : IMyService
{
int _userID = -1;
public bool LogIn(int userID, string password){...}
public OperationResult<SomeDataContract> GetSome()
{
if(_userID != 101)
return new OperationResult<SomeDataContract>()
{
Result = false,
ResultMessage = "User hasn't permission",
ReturnValue = null;
};
.....
}
}
或者:
public class MyService : IMyService
{
int _userID = -1;
public bool LogIn(int userID, string password){...}
public SomeDataContract GetSome()
{
if(_userID != 101)
throw new FaultException<MyFaultContract>(new MyFaultContract("User hasn't permission"));
}
}
在经典应用程序中,我会使用GenericResult,因为我认为它不是exception
。但是WCF
呢?我的方案是FaultException
正常做法吗?