gpt4 book ai didi

iphone - 使用 IParameterInspector 验证 WCF 参数并在客户端处理FaultException

转载 作者:行者123 更新时间:2023-12-03 20:50:56 24 4
gpt4 key购买 nike

我有一个 WCF Rest 服务,它使用 IParameterInspector 进行输入参数验证。 ,在调用实际的服务方法之前。现在这些休息服务都被 iPhone 使用了。如果参数无效,那么我抛出了我想在 iPhone(或者可能是 Android)端处理的错误异常。嗯,我在 stackoverflow 中引用了很多链接,并且在我的代码中使用了以下链接作为引用。

WCF Parameter Validation with Interceptor
以下是我的分步代码片段。

=> 在FaultException<T> 中使用的FaultExceptionResponse 类

[DataContract]
public class FaultExceptionResponse
{
[DataMember]
public bool Success { get; set; }

[DataMember]
public string ResponseString { get; set; }
}

=> 下面的类验证参数。

public class ValidationParameterInspectorAttribute : Attribute, IParameterInspector, IOperationBehavior
{

public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
}

public object BeforeCall(string operationName, object[] inputs)
{
if (operationName == "GetCommunicationDetailById")
{
var communicationChatViewModel = inputs.FirstOrDefault() as CommunicationChatViewModel;

if (communicationChatViewModel != null &&
(communicationChatViewModel.iConsultCommunicationID <= 0))
{
//ErrorLogger.LogErrorMessageToElmah(new ArgumentException("API Name: GetCommunicationDetailById Parameter cannot be less than zero.", "iConsultCommunicationID"));
var fc = new FaultExceptionResponse { Success = false, ResponseString = "Invalid parameter found while fetching communication detail !" };
throw new FaultException<FaultExceptionResponse>(fc, new FaultReason(fc.ResponseString));
}
}
return null;
}

public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}

public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}

public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.ParameterInspectors.Add(this);
}

public void Validate(OperationDescription operationDescription)
{
}
}

=> 然后我像这样装饰我的 API

[OperationContract]
[ValidationParameterInspector]
[FaultContract(typeof(FaultExceptionResponse))]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/GetCommunicationDetailById")]
CommunicationChatDetailList GetCommunicationDetailById(CommunicationChatViewModel communicationParaViewModel);

一切正常,但当参数无效时,iPhone 端会抛出此错误异常,它仅显示以下错误信息。

Error: {
    AFNetworkingOperationFailingURLResponseErrorKey = "<NSHTTPURLResponse: 0x7faa605faa90> { URL: http://192.168.151.40/MyWCF/Service1.svc/GetCommunicationDetailById } { status code: 400, headers {\n    \"Cache-Control\" = private;\n    \"Content-Length\" = 3319;\n    \"Content-Type\" = \"text/html\";\n    Date = \"Fri, 25 Sep 2015 15:45:14 GMT\";\n    Server = \"Microsoft-IIS/7.5\";\n    \"X-AspNet-Version\" = \"4.0.30319\";\n    \"X-Powered-By\" = \"ASP.NET\";\n} }";
    NSErrorFailingURLKey = "http://192.168.151.40/MyWCF/Service1.svc/GetCommunicationDetailById";
    NSLocalizedDescription = "Request failed: bad request (400)";
    NSUnderlyingError = "Error Domain=AFNetworkingErrorDomain Code=-1016 \"Request failed: unacceptable content-type: text/html\" UserInfo={AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7faa605faa90> { URL: http://192.168.151.40/LKPracooWCF/Service1.svc/GetCommunicationDetailById } { status code: 400, headers {\n    \"Cache-Control\" = private;\n    \"Content-Length\" = 3319;\n    \"Content-Type\" = \"text/html\";\n    Date = \"Fri, 25 Sep 2015 15:45:14 GMT\";\n    Server = \"Microsoft-IIS/7.5\";\n    \"X-AspNet-Version\" = \"4.0.30319\";\n    \"X-Powered-By\" = \"ASP.NET\";\n} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html, NSErrorFailingURLKey=http://192.168.151.40/LKPracooWCF/Service1.svc/GetCommunicationDetailById
};

我还没有找到我的自定义错误消息!!!!现在,如果我在高级休息客户端应用程序中测试相同的测试用例,那么我会收到如下所示的自定义错误消息。

Status - 400 Bad Request
<p class="heading1">Request Error</p>
<p>The server encountered an error processing the request. The exception message is 'Invalid parameter found while fetching communication detail !'. See server logs for more details. The exception stack trace is: </p>
<p>......</p>

所以我想要的是如何处理这个faultException FaultException<FaultExceptionResponse>在客户端(iPhone)端???

最佳答案

只需扩展MattC给出的答案即可。非常感谢马特。我发布这个详细代码作为答案,仅供将来访问此链接的读者引用。还有一件事,我还在我的博客里写过博文——krishnrajrana.wordpress.com

这里是如何处理客户端异常的方法(例如 iPhone 设备或 Android 设备)

public object BeforeCall(string operationName, object[] inputs)
{
if (operationName == "GetCommunicationDetailById")
{
var model = inputs.FirstOrDefault() as TestViewModel;

if (model != null &&
(model.iConsultCommunicationID <= 0))
{
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
var wfc = new WebFaultException<Response>(new Response(false, "Invalid parameter found while fetching detail !"), System.Net.HttpStatusCode.OK);
throw wfc;
}
}
else if (operationName == "AnotherMethod")
{
............
}

// OR you can use switch case
switch(operationName)
{
Case "Method":
// your logic
}

return null;
}

关于iphone - 使用 IParameterInspector 验证 WCF 参数并在客户端处理FaultException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32822489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com