WCF parameter validation using IParameterInspector and handle FaultException at client - iphone

I'm having a WCF Rest service which does input parameter validation by using the IParameterInspector, before calling the actual service method. Now these rest services are consumed by iPhone. If parameter is not valid then i have thrown faultexception which i want to handle at iPhone (or may be Android) side.
Well i have refer so many links in stackoverflow and i have used below link as reference in my code.
WCF Parameter Validation with Interceptor
Following is my step-by-step code snippet.
=> FaultExceptionResponse class which is used in FaultException<T>
[DataContract]
public class FaultExceptionResponse
{
[DataMember]
public bool Success { get; set; }
[DataMember]
public string ResponseString { get; set; }
}
=> Below class validate parameters.
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)
{
}
}
=> and then i have decorate my API like this
[OperationContract]
[ValidationParameterInspector]
[FaultContract(typeof(FaultExceptionResponse))]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/GetCommunicationDetailById")]
CommunicationChatDetailList GetCommunicationDetailById(CommunicationChatViewModel communicationParaViewModel);
Everything works fine but when parameter is not valid then this faultException thrown at iPhone side, it shows only below error information.
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
};
I have not found my custom error message!!!! Now if i test the same test case in Advanced Rest Client Application then i got my custom error messge like below.
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>
So what i want is how to handle this faultException FaultException<FaultExceptionResponse> at client (iPhone) side???.

Just extend the answer given by MattC. Big Thanks Matt. I'm posting this detail code as answer just for the future reader who'll come to this link in SO. One more thing, I have also written blog post in my blog - krishnrajrana.wordpress.com
So guys here is how you handle exception at client (like iPhone devices Or android devices)
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;
}

The problem is your service is supposed to return Json but the exception is causing the response content-type to be text/html. You could drop using FaultException and switch to WebFaultException along with explicitly setting the content-type for the response. For example:
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
var exception = new WebFaultException<string>(
"{ \"Success\" = \"false\", " +
"\"ResponseString\" = \"Invalid parameter found while fetching communication detail !\" }",
HttpStatusCode.BadRequest);
throw exception;

Related

count records in get method REst Apex Services Json Response

#RestResource(urlMapping='/sg/0.1/subscription/')
global with sharing class SubsRecord1 {
Public Integer count {get;set;}
#HttpGet
global static List<ONB2__Subscription__c> getSubsById() {
RestRequest request = RestContext.request;
List<ONB2__Subscription__c> result = [select id , name FROM ONB2__Subscription__c where ONB2__Status__c =:'Active'];
return result;
}
}
I want the Json response something like this { "ONB2__Subscription__c": "[[n]]"}
Please try below sample code, hope this helps
#RestResource(urlMapping='/sg/0.1/subscription/')
global with sharing class SubsRecord1 {
#HttpGet
global static void getSubsById() {
RestRequest request = RestContext.request;
RestResponse response =RestContext.response;
AggregateResult result = [select count(id)subcount FROM ONB2__Subscription__c where ONB2__Status__c =:'Active'];
response.statusCode = 200;
string jsonResponse = '{"ONB2__Subscription__c":'+'"'+result.get('subcount')+'"'+'}';
response.responseBody = Blob.valueOf(jsonResponse);
return;
}
}

Why Outbound Soap Request is Empty When to Handle Message at PRE_STREAM Phase?

I would like to handle soap message at Pre_stream phase.But i cant get soap message. In addition byte data with a size of 1 mb is send by this request.
public class MessageChangeInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
public MessageChangeInterceptor() {
super(Phase.PRE_STREAM);
addBefore(SoapPreProtocolOutInterceptor.class.getName());
}
public void handleMessage(SoapMessage message) {
boolean isOutbound = false;
isOutbound = message == message.getExchange().getOutMessage() || message == message.getExchange().getOutFaultMessage();
if (isOutbound) {
OutputStream os = message.getContent(OutputStream.class);
CachedStream cs = new CachedStream();
message.setContent(OutputStream.class, cs);
message.getInterceptorChain().doIntercept(message);
try {
cs.flush();
IOUtil.closeQuietly(cs);
CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);
String currentEnvelopeMessage = IOUtil.toString(csnew.getInputStream(), "UTF-8");// currentEnvelopeMessage is empty ?
....
}

Best practices to handle Web API status codes

I have a web API project done with .NETCore.
My web API receives a request from another Service A, with the information I have I need to do some conversion on the data and send it to another Service B.
I am expecting that Service B send back some response: like OK or NOK. As the number of codes I can get back from Service B are so much. I would like to know which is the best practices to handle those codes?
As you will see in my code, I get the status code in this way:
var status = (int)response.StatusCode;
And the I have some if to handle this. Looking at my code it looks like a very poor status code Handling but at moment it is the best I can do. I am kindly asking suggestions to improve this.
I am using RestSharp.
Following my code:
[HttpPost]
[Produces("application/json", Type = typeof(MyModel))]
public async Task<IActionResult> Post([FromBody]MyModel myModel)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var response = (RestResponse) await _restHelper.GetResponse("ServiceB:url", myModel);
if (response != null)
{
var status = (int)response.StatusCode;
//2xx status OK
if (status >= 200 && status < 300)
{
return Ok(response.Content);
}
//Catch all status code
return StatusCode(status, response.Content);
}
//If for some reason, I don't get any response from ServiceB
return NotFound("No response from ServiceB");
}
catch (Exception ex)
{
_logger.LogError("POST_ERROR", "ServiceB-relay/Post UNEXPECTED ERROR", ex.Message);
return StatusCode(500, "Server error, not able to process your request");
}
}
and this is my restHelper
public class RestHelper: IRestHelper
{
private readonly IConfigurationRoot _config;
public RestHelper(IConfigurationRoot config)
{
_config = config;
}
public async Task<IRestResponse> GetResponse(string configKey, object dtoObject)
{
//Get the URL from the config.json
var url = _config[configKey];
//Create rest client and rest request
var restClient = new RestClient(url);
var request = new RestRequest {Timeout = 30000, Method = Method.POST};
//Add header
request.AddHeader("Accept", "application/json");
//convert the dto object to json
var jsonObject = JsonConvert.SerializeObject(dtoObject.ToString(), Formatting.Indented);
request.AddParameter("application/json", jsonObject, ParameterType.RequestBody);
var taskCompletion = new TaskCompletionSource<IRestResponse>();
//Execute async
restClient.ExecuteAsync(request, r => taskCompletion.SetResult(r));
//await the task to finish
var response = (RestResponse) await taskCompletion.Task;
return response;
}
Thanks

MVC 2.0 - Custom handling of all errors to return json

I have an MVC 2 app that I want all requests to return json. I have overridden a HandleErrorAttribute and an AuthorizeAttribute. My goal is that all errors (even 403 and 404) are returned as json.
Here is my error handler. ExceptionModel is a simple class defining any error returned by my application. The Exception handler is a class that translates the error details into a formatted e-mail and sends it to me.
public class HandleErrorJsonAttribute : System.Web.Mvc.HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
context.ExceptionHandled = true;
RaiseErrorSignal(context.Exception);
context.RequestContext.HttpContext.Response.ContentType = "application/json";
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(context.HttpContext.Response.Output, new ExceptionModel(context.Exception));
}
private static void RaiseErrorSignal(Exception ex)
{
IExceptionHandler handler = Resolve();
handler.HandleError(ex.GetBaseException());
}
private static IExceptionHandler Resolve()
{
return ServiceLocator.Locate<IExceptionHandler>();
}
}
Here is the Exception model for clarification
public class ExceptionModel
{
public int ErrorCode { get; set; }
public string Message { get; set; }
public ExceptionModel() : this(null)
{
}
public ExceptionModel(Exception exception)
{
ErrorCode = 500;
Message = "An unknown error ocurred";
if (exception != null)
{
if (exception is HttpException)
ErrorCode = ((HttpException)exception).GetHttpCode();
Message = exception.Message;
}
}
public ExceptionModel(int errorCode, string message)
{
ErrorCode = errorCode;
Message = message;
}
}
and finally, my custom authorize attribute. I an using forms auth, but I did not want any of the automatic redirection. I simply want the error to show on the screen and stop any further processing.
public class AuthorizeTokenAttribute : System.Web.Mvc.AuthorizeAttribute
{
public bool SuperAdminOnly { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
bool authorized = base.AuthorizeCore(httpContext);
if(!SuperAdminOnly)
return authorized;
if(!authorized)
return authorized;
return SessionHelper.UserIsSuperAdmin(httpContext.User.Identity.Name);
}
protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
{
throw new HttpException(403, "Access Denied");
}
}
This all works great for most errors, but it is missing one thing. I have a controller action like this.
[AuthorizeToken]
[HttpPost]
public JsonResult MyAction()
{
return new JsonResult();
}
It works fine when you submit via post, but on a get I receive an unhandled 404 error.
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource
you are looking for (or one of its
dependencies) could have been removed,
had its name changed, or is
temporarily unavailable. Please
review the following URL and make sure
that it is spelled correctly.
Requested URL: /MyController/MyAction
Version Information: Microsoft .NET
Framework Version:4.0.30319; ASP.NET
Version:4.0.30319.1
This happens on a GET, which is to be expected as default behavior. However, how can I handle for this condition so that I could instead return json like this
{"ErrorCode":404,"Message":"Page Not Found"}
To handle errors personally I prefer the Application_Error event in Global.asax:
protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
Response.Clear();
Server.ClearError();
var httpException = exception as HttpException;
var routeData = new RouteData();
routeData.Values["controller"] = "Errors";
routeData.Values["action"] = "Index";
routeData.Values["error"] = exception;
IController errorController = new ErrorsController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
and then have an ErrorsController:
public class ErrorsController : Controller
{
public ActionResult Index(Exception exception)
{
var errorCode = 500;
var httpException = exception as HttpException;
if (httpException != null)
{
errorCode = httpException.ErrorCode;
}
return Json(new
{
ErrorCode = errorCode,
Message = exception.Message
}, JsonRequestBehavior.AllowGet);
}
}

HttpWebRequest maintenance and http web errors causing it to return "HRESULT E_FAIL" and "server not found"

I am iterating through a large list of objects (1503) and calling a save method on a ServiceProxy I have written. The service proxy uses the new networking stack in Silverlight 4 to call BeginGetRequestStream to start the process of asynchronously sending my objects to an azure REST service I have written for saving off the objects. The Http method I am using is POST. I know HttpWebClient is smart enough to reuse the Http connection so I am not concurrently opening 1503 connections to the server. Saving works fine and all 1503 objects are saved very quickly. However, when I try to save the same objects again, I expect to recieve an HttpStatus code of forbidden because the objects already exist and that is the code I set my azure web service to return. On small groups of objects, it works as expected. However, when I try saving the entire list of 1503 objects, I receive only 455 correct responses and 1048 errors such as "server not found" and
System.Exception ---> System.Exception:Error HRESULT E_FAIL has been returned from a call to a COM component.
at
System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)...
I wonder if there is some sort of book keeping or maintenance I am supposed to be performing on my HttpWebClient instances that I am neglecting and that is what is causing the http errors to throw exceptions but the new saves to work perfectly. Here is my code for handling the error cases:
private static void SendAncestorResponseCallback(IAsyncResult result)
{
var info = (SendAncestorInfo)result.AsyncState;
try
{
var response = info.Request.EndGetResponse(result);
info.Response = response;
}
catch ( Exception ex)
{
info.Error = ex;
}
info.MainThreadContext.Post(SendAncestorMainThreadCallback, info);
}
private static void SendAncestorMainThreadCallback(object state)
{
var info = (SendAncestorInfo)state;
IAncestor origAncestor = info.Content;
HttpWebResponse response = null;
if (info.Error != null)
{
if ((info.Error as WebException) == null)
{
info.Callback(false, origAncestor, null, info.Error);
return;
}
else //get response from WebException
{
response = (HttpWebResponse)(info.Error as WebException).Response;
}
}
else //get response from info.Response
{
response = info.Response as HttpWebResponse;
}
if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.Forbidden)
{
var stream = response.GetResponseStream();
using (var reader = new StreamReader(stream))
{
IAncestor retAncestor = XMLSerializerHelper.DeserializeObject<Ancestor>(reader.ReadToEnd());
info.Callback(response.StatusCode == HttpStatusCode.Created, origAncestor, retAncestor, null);
}
}
else info.Callback(false, origAncestor, null, info.Error);
}
considering how the web service is written I should only expect http status codes of created or forbidden and like I said with small groups this is the case. The fact that I only start getting the errors mentioned earlier makes me feel like I am doing something wrong with the HttpWebRequest objects etc. Any assistance would be greatly appreciated. Thanks.
--update here is the code that generates the HttpWebRequest:
foreach (IAncestor ancestor in ancestors)
{
AncestorViewModel ancestorVM = new AncestorViewModel(ancestor);
ancestorVM.Status = SaveStatus.Undefined;
ParsedAncestors.Add(ancestorVM);
_service.CreateAncestor(UserSrc, ancestor, (success, origAncestor, retAncestor, exception) =>
{
AncestorViewModel result = ParsedAncestors.First(a => a.Model.IdNo == origAncestor.IdNo);
if (exception == null)//web response was either Created or Forbidden
{
if (success)//Ancestor successfully created
{
savedAncestors++;
SuccessMessage = string.Format("{0} Saved\n", savedAncestors);
result.Status = SaveStatus.Saved;
}
else //Ancestor already existed
{
conflictAncestors.Add(origAncestor, retAncestor);
ConflictMessage = string.Format("{0} Conflicts\n", conflictAncestors.Count);
result.Status = SaveStatus.Conflicted;
}
}
else //Show exception recieved from remote web service
{
//if (exception as WebException != null)
//{
// //if exception is WebException get status code and description
// HttpWebResponse rs = (HttpWebResponse)(exception as WebException).Response;
// Message += string.Format("WebServer returned status code {0}: '{1}'\n", (int)rs.StatusCode, rs.StatusDescription);
//}
errors.Add(origAncestor, exception);
ErrorMessage = string.Format("{0} Errors\n", errors.Count);
result.Status = SaveStatus.Error;
}
});
}
public void CreateAncestor(string userSrc, IAncestor ancestor, Action<bool, IAncestor, IAncestor, Exception> callback)
{
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
var request = (HttpWebRequest)WebRequest.Create(
new Uri(string.Format("{0}/{1}/{2}", rootUri, AncestorsRestPoint, userSrc)));
request.Method = "POST";
request.ContentType = "application/xml";
var info = new SendAncestorInfo
{
Request = request,
Callback = callback,
Content = ancestor,
MainThreadContext = SynchronizationContext.Current
};
request.BeginGetRequestStream(SendAncestorRequestCallback, info);
}