开发者

WCF IParameterInspector return response object

I'm have a WCF service which does input parameter validation by using the IParameterInspector hook, prior to calling the actual service method (WCF provider side). Everything is working well and 开发者_如何转开发I return the validation error by a FaultException exception. My question is whether it is possible to return an object with the validation errors instead of an exception? See the following snippet:

public object BeforeCall(string operationName, object[] inputs)
    {
        var errors = new List<string>();

        // Validate each input parameter
        foreach (var entity in inputs)
        {
            // executing bunch of IValidator<T>'s
        }

        if (errors.Count != 0)
        {
            ////throw new FaultException<List<string>>(errors, "Input parameter validation error");
            return new Response<object>(null, errors);
        }
        else
        {
            return null;
        }
    }

This obviously doesn't work - My question is whether it is possible to return a reponse object to the comsumer, from the BeforeCall (WCF provider side)?


This is interesting why dont you add fault contract to your webservice and let it perform validation without intercepting anything. If something goes wrong it will throw real fault by contract. It would be normal.

For your fault you can create a custom fault with a List<string> inside and it can be easily handled by your clients to display errors.


As far as I now, you can't return a custom object.Because return value's purpose is to allow BeforeCall methods to be correlated with AfterCall methods. The value returned from a BeforeCall will be passed into the corresponding AfterCall. Specifically, it’s in the correlationState parameter.

However, you can use FaultException<T> where T is your custom object with validation errors.

You need to define you custom class as follows:

public class DemoFault
{
[DataMember()]
public string ErrorText;
public DemoFault(string errorMessage)
{
ErrorText = errorMessage;
}
}

try
{
//blah blah blah
}
catch (FaultException<DemoFault> helloFault)
{
Console.WriteLine(hellofault.Detail.ErrorText);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜