What is the best practice for Wcf service, in case we need to send fail/success result?
For example:
We have Wcf service, behind service we have some BussinesLogic.
Our service have one operation contract:
[OperationContract]
/*SomeResult*/ SendImages(Bitmap[] images);
Possible scenarios:
BussinesLogic is in "bad" state. Logic is in error state and we
dont know when it will avalible for work again.Should i make custom fault? In wich case? Should i use generic fault? Should i make enum OperationResult? Or maby in case of success any result is like overkill?
Number of possible scenarios will be constant.
I like to use this approach:
[DataContract]
public class OperationResult
{
public OperationResult()
{
Errors = new List<OperationError>();
Success = true;
}
[DataMember]
public bool Success { get; set; }
[DataMember]
public IList<OperationError> Errors { get; set; }
}
[DataContract(Name = "OperationResultOf{0}")]
public class OperationResult<T> : OperationResult
{
[DataMember]
public T Result { get; set; }
}
[DataContract]
public class OperationError
{
[DataMember]
public string ErrorCode { get; set; }
[DataMember]
public string ErrorMessage { get; set; }
}
Also I have some extensions:
public static OperationResult WithError(this OperationResult operationResult, string errorCode,
string error = null)
{
return operationResult.AddErrorImpl(errorCode, error);
}
public static OperationResult<T> WithError<T>(this OperationResult<T> operationResult, string errorCode,
string error = null)
{
return (OperationResult<T>) operationResult.AddErrorImpl(errorCode, error);
}
private static OperationResult AddErrorImpl(this OperationResult operationResult, string errorCode,
string error = null)
{
var operationError = new OperationError {Error = error ?? string.Empty, ErrorCode = errorCode};
operationResult.Errors.Add(operationError);
operationResult.Success = false;
return operationResult;
}
public static OperationResult<T> WithResult<T>(this OperationResult<T> operationResult, T result)
{
operationResult.Result = result;
return operationResult;
}
The extensions make possible to return error with the one line of code:
return retValue.WithError(ErrorCodes.RequestError);
From my wcf service I never throw an exception.
Sorry for the wall of code
Caller's code is something like that. But it all depends on your requirements
OperationResult res = _service.Register(username, password);
if(!res.Success)
{
if(res.Errors.Any(x => ErrorCodes.UsernameTaken)
{
// show error for taken username
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With