我是 WCF 的新手,我有以下问题:
1)我想创建一个大的 WCF 服务,而不是几个小的 WCF 服务。这就像外观设计模式的实现。
为什么?我需要为客户端进行会话,我需要存储有关客户端的敏感信息(访问令牌、用户名、API 密钥)并对其进行验证。通过这种方式,我只需创建私有字段:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class ApplicationService : IApplicationService
{
private string _accessToken;
private string _userName;
public OrderResponse GetOrder(OrderRequest request)
{
var response = new OrderResponse();
var result = Validate(request, response);
... other code
}
public CustomerResponse GetCustomer(CustomerRequest request)
{
var response = new CustomerResponse();
var result = Validate(request, response);
... other code
}
public LoginResponse Login(LoginRequest request)
{
var response = new LoginResponse();
var result = Validate(request, response);
... other code
}
public LogoutResponse Logout(LogoutRequest request)
{
var response = new LogoutResponse();
var result = Validate(request, response);
... other code
}
public bool Validate(BaseRequest request, BaseResponse response)
{
if(request.Token != _accessToken)
return false;
... validate other fields
return true
}
// many other methods
}
2)如果我创建一个包含所有其他 WCF 服务的大服务。此外,它就像外观设计模式的实现。:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class ApplicationService : IApplicationService
{
private string _accessToken;
private string _userName;
private LoginService _loginService;
private CustomerService _customerService;
private OrderService _orderService;
public ApplicationService()
{
_loginService = new LoginService();
_orderService = new OrderService();
_customerService = new CustomerService();
}
public OrderResponse GetOrder(OrderRequest request)
{
var response = new OrderResponse();
var result = Validate(request, response);
if(!result) return Something;
return _orderService.GetOrder(request);
}
public CustomerResponse GetCustomer(CustomerRequest request)
{
var response = new CustomerResponse();
var result = Validate(request, response);
if(!result) return Something;
return _customerService.GetCustomer(request);
}
public LoginResponse Login(LoginRequest request)
{
var response = new LoginResponse();
var result = Validate(request, response);
if(!result) return Something;
return _loginService.Login(request);
}
public LogoutResponse Logout(LogoutRequest request)
{
var response = new LogoutResponse();
var result = Validate(request, response);
if(!result) return Something;
return _LoginService.Logout(request);
}
// many other methods
}
你怎么看这件事?
3)还有一个问题:验证失败时我必须返回什么?抛出FaultException
或者我可以在响应对象中返回错误消息吗?什么是好的做法?
谢谢。