status code on async webresponse task - c#

I want to hit a plethora (100k+) of JSON files as rapidly as possible, serialize them, and store the HTTP response status code of the request (whether it succeeded or failed). (I am using System.Runtime.Serialization.Json and a DataContract). I intend to do further work with the status code and serialized object, but as a test bed I have this snippet of code:
List<int> ids = new List<int>();
for (int i = MIN; i < MAX; i++)
ids.Add(i);
var tasks = ids.Select(id =>
{
var request = WebRequest.Create(GetURL(id));
return Task
.Factory
.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, id)
.ContinueWith(t =>
{
HttpStatusCode code = HttpStatusCode.OK;
Item item = null;
try
{
using (var stream = t.Result.GetResponseStream())
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Item));
item = ((Item)jsonSerializer.ReadObject(stream));
}
}
catch (AggregateException ex)
{
if (ex.InnerException is WebException)
code = ((HttpWebResponse)((WebException)ex.InnerException).Response).StatusCode;
}
});
}).ToArray();
Task.WaitAll(tasks);
Using this approach I was able to process files much more quickly than the synchronous approach I was doing before.
Though, I know that the GetResponseStream() throws the WebException when the status code is 4xx or 5xx. So to capture those status codes I need to catch this exception. However, in the context of this TPL it is nested in an InnerException on an AggregateException. This makes this line really confusing:
code = ((HttpWebResponse)((WebException)ex.InnerException).Response).StatusCode;
Though, this works... I was wondering if there is a better/clearer way to capture such an exception in this context?

Take a look at MSDN article: Exception Handling (Task Parallel Library)
For example, you might want to rewrite your code as follows:
try
{
using (var stream = t.Result.GetResponseStream())
{
DataContractJsonSerializer jsonSerializer = new
DataContractJsonSerializer(typeof(Item));
item = ((Item)jsonSerializer.ReadObject(stream));
}
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
bool isHandled = false;
if (e is WebException)
{
WebException webException = (WebException)e;
HttpWebResponse response = webException.Response as HttpWebResponse;
if (response != null)
{
code = response.StatusCode;
isHandled = true;
}
}
if (!isHandled)
throw;
}
}

Try this on for size. GetBaseException returns the exception that caused the problem.
try
{
}
catch (System.AggregateException aex)
{
var baseEx = aex.GetBaseException() as WebException;
if (baseEx != null)
{
var httpWebResp = baseEx.Response as HttpWebResponse;
if (httpWebResp != null)
{
var code = httpWebResp.StatusCode;
// Handle it...
}
}
throw;
}

Related

Get EventMessage from message in MS Graph API with C#

I am trying to Get EventMessage from Message in MS Graph API with C# but every time it is showing type as a message instead of EventMessage. Below are the code:-
public static Graph.MailFolderMessagesCollectionPage ReadInbox()
{
GetAuthenticatedClient();
var result = new Graph.MailFolderMessagesCollectionPage();
List<Graph.QueryOption> options = new List<Graph.QueryOption>
{
new Graph.QueryOption("$expand","microsoft.graph.eventMessage/event"),
new Graph.QueryOption("$filter","isread eq false")
};
try
{
var response = graphClient.Me.MailFolders.Inbox.Messages.Request(options).OrderBy("receivedDateTime DESC").GetAsync();
result = response.Result as Graph.MailFolderMessagesCollectionPage;
}
catch (Exception ex)
{ }
Call the above method ReadInbox to get type and perform some action.
var appointments = ReadInbox();
if (appointments != null)
{
foreach (dynamic request in appointments)
{
try
{
if (request.GetType().Name.Contains("EventMessage"))
{
}
else if (request.GetType().Name == "Message")
{
}
}
catch (Exception ex)
{
}
}
}
Use the IsInstanceOfType method to identify if its an eventMessage. You can also remove the expand option from the query option since eventMessages are fetched anyway as part of the get Messages call.
if (appointments != null)
{
foreach (dynamic request in appointments)
{
try
{
if (typeof(EventMessage).IsInstanceOfType(request))
{
Console.WriteLine("Is an event");
Console.WriteLine(request);
}
}
catch (Exception ex)
{
}
}
}

How can I refactor the code in order to remove the break statement in a C# application

I have a C# Windows application from where I'm making calls to a API using below code:
while (true)
{
try
{
using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "Some URL"))
{
requestMessage.Headers.Add("Accept", "application/json");
response = await myHttpHelper.SendHttpRequest(requestMessage).ConfigureAwait(false);
}
break; // where the code smells is shown
}
catch (TaskCanceledException )
{
if (++attemptCount > 3)
{
throw;
}
Thread.Sleep(10000);
}
catch (Exception ex2)
{
throw;
}
}
Usually what happens is the get request to APIs gets cancelled when ever there is some network issue. So what I have done is whenever the task is getting cancelled, I attempt it for three times. If it doesn't work , then I throw an exception to the calling method. If it is successful with in this 3 attempts, I'm breaking the loop.
Now when I run Sonar analysis on my code, it is showing to remove break statement and refactor the code. How can I do that?
while (true) is an infinite loop.
Instead, I would rather use a (boolean) variable to check in while().
That gives you the opportunity to set this variable to false and avoid the break.
You shouldn't generally use while (true) (that's a potential infinite loop), even if you have somewhere a break statement it can sometimes happen that this statement might not be reached.
I would recommend a for loop:
for (int i = 0; i < 3; i++)
{
try
{
using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "Some URL"))
{
requestMessage.Headers.Add("Accept", "application/json");
response = await myHttpHelper.SendHttpRequest(requestMessage).ConfigureAwait(false);
}
}
catch (TaskCanceledException)
{
Thread.Sleep(10000);
}
catch (Exception ex2)
{
throw;
}
}
Create a boolean variable called success and use that as the while loop condition:
boolean success = false;
while (!success) {
try
{
using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "Some URL"))
{
requestMessage.Headers.Add("Accept", "application/json");
response = await myHttpHelper.SendHttpRequest(requestMessage).ConfigureAwait(false);
}
success = true; <--- set to true here
}
...
}
If you don't go with Polly I would recommend a combination of #Sweeper's and #Magnus' reply:
const int retryLimit = 3;
boolean success = false;
int retryCounter = 0;
while (!success
&& retryCounter++ < retryLimit) {
try
{
// http request as is
success = true;
}
// Exception handling
}
This way you controll the number of retries and jump out of the loop when the request is successful.

C# Cannot await in the body of a catch clause [duplicate]

I have the following code:
WebClient wc = new WebClient();
string result;
try
{
result = await wc.DownloadStringTaskAsync( new Uri( "http://badurl" ) );
}
catch
{
result = await wc.DownloadStringTaskAsync( new Uri( "http://fallbackurl" ) );
}
Basically I want to download from a URL and when it fails with an exception I want to download from another URL. Both time async of course. However the code does not compile, because of
error CS1985: Cannot await in the body of a catch clause
OK, it's forbidden for whatever reason but what's the correct code pattern here?
EDIT:
The good news is that C# 6.0 will likely allow await calls both in catch and finally blocks.
Update: C# 6.0 supports await in catch
Old Answer: You can rewrite that code to move the await from the catch block using a flag:
WebClient wc = new WebClient();
string result = null;
bool downloadSucceeded;
try
{
result = await wc.DownloadStringTaskAsync( new Uri( "http://badurl" ) );
downloadSucceeded = true;
}
catch
{
downloadSucceeded = false;
}
if (!downloadSucceeded)
result = await wc.DownloadStringTaskAsync( new Uri( "http://fallbackurl" ) );
Awaiting in a catch block is now possible as of the End User Preview of Roslyn as shown here (Listed under Await in catch/finally) and will be included in C# 6.
The example listed is
try … catch { await … } finally { await … }
Update: Added newer link, and that it will be in C# 6
This seems to work.
WebClient wc = new WebClient();
string result;
Task<string> downloadTask = wc.DownloadStringTaskAsync(new Uri("http://badurl"));
downloadTask = downloadTask.ContinueWith(
t => {
return wc.DownloadStringTaskAsync(new Uri("http://google.com/")).Result;
}, TaskContinuationOptions.OnlyOnFaulted);
result = await downloadTask;
Give this a try:
try
{
await AsyncFunction(...);
}
catch(Exception ex)
{
Utilities.LogExceptionToFile(ex).Wait();
//instead of "await Utilities.LogExceptionToFile(ex);"
}
(See the Wait() ending)
Use C# 6.0. see this Link
public async Task SubmitDataToServer()
{
try
{
// Submit Data
}
catch
{
await LogExceptionAsync();
}
finally
{
await CloseConnectionAsync();
}
}
You could put the await after the catch block followed by a label, and put a goto in the try block.
(No, really! Goto's aren't that bad!)
The pattern I use to rethrow the exception after await on a fallback task:
ExceptionDispatchInfo capturedException = null;
try
{
await SomeWork();
}
catch (Exception e)
{
capturedException = ExceptionDispatchInfo.Capture(e);
}
if (capturedException != null)
{
await FallbackWork();
capturedException.Throw();
}
You can use a lambda expression as follows:
try
{
//.....
}
catch (Exception ex)
{
Action<Exception> lambda;
lambda = async (x) =>
{
// await (...);
};
lambda(ex);
}
In a similar instance, I was unable to await in a catch block. However, I was able to set a flag, and use the flag in an if statement (Code below)
---------------------------------------...
boolean exceptionFlag = false;
try
{
do your thing
}
catch
{
exceptionFlag = true;
}
if(exceptionFlag == true){
do what you wanted to do in the catch block
}

Null values while updating DB in Parallel.Foreach

I use following script to get data from external service and store in dB. In certain rare cases less than 1% records gets updated with null values. In below code, the "re.status=fail" we see null. let us know if any thots.
public void ProcessEnquiries()
{
List<req> request = new List<req>();
var options = new ParallelOptions { MaxDegreeOfParallelism = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MaxDegreeOfParallelism"]) };
try
{
Parallel.ForEach(request, options, currentRequest =>
{
ProcessedRequest processedRequest = null;
processedRequest = CommunicateToWS(currentRequest); // Here we call to webservice
});
}
catch (AggregateException exception)
{
foreach (Exception ex in exception.InnerExceptions)
{
// Handle Exception
}
}
}
public ProcessedRequest CommunicateToWS(req objReq)
{
ProcessedRequest re = new ProcessedRequest();
using (WebCall obj = new WebCall())
{
re.no = refnu;
try
{
retval = obj.getValue(inval);
objProxy.Close();
//get data
// parse and store to DB
}
catch (Exception e)
{
re.status = "fail";
//update DB that request has failed
//Handle Exception
obj.Close();
}
}
}

FacebookOAuthException was unhandled

I'm new in these things and I've been testing the api ...
and came to me a situation that is:
if the user changes the password of Facebook
the Access token is renewed ... and try to post the API Launches
an exception and the application crashes...
how to resolve this situation?
try {
FacebookApp app = new FacebookApp(FacebookToken);
var args = new Dictionary<string, object>();
args["message"] = "hi";
args["caption"] = "appcaption";
args["description"] = "appdescription";
args["name"] = "appname";
args["picture"] = "apppicture";
args["link"] = "applink";
app.ApiAsync((X) => { calback(X); }, null, "/me/feed", args, HttpMethod.Post);
}
catch (Exception ex) {
Uri url = new Uri("/MyFacebook.xaml", UriKind.Relative);
NavigationService.Navigate(url);
}
this is Api code, and it's crashing when throwing the OAuthExcepion at the line marked with "Exception HERE"
private static void ResponseCallback(IAsyncResult asyncResult, FacebookAsyncCallback callback, object state)
{
object result = null;
FacebookApiException exception = null;
try
{
var request = (HttpWebRequest)asyncResult.AsyncState;
var response = (HttpWebResponse)request.EndGetResponse(asyncResult);
using (Stream responseStream = response.GetResponseStream())
{
result = JsonSerializer.DeserializeObject(responseStream);
}
}
catch (FacebookApiException)
{
// Rest API Errors
throw;
}
catch (WebException ex)
{
// Graph API Errors or general web exceptions
exception = ExceptionFactory.GetGraphException(ex);
if (exception != null)
{
// Thow the FacebookApiException
throw exception;
}
throw; //Exception HERE
}
finally
{
// Check to make sure there hasn't been an exception.
// If there has, we want to pass null to the callback.
object data = null;
if (exception == null)
{
data = result;
}
#if SILVERLIGHT
callback(new FacebookAsyncResult(data, state, null, asyncResult.CompletedSynchronously, asyncResult.IsCompleted, exception));
#else
callback(new FacebookAsyncResult(data, state, asyncResult.AsyncWaitHandle, asyncResult.CompletedSynchronously, asyncResult.IsCompleted, exception));
#endif
}
}
thanks
The behavior of the SDK is intended. An exception is not "crashing" the application, but rather telling you when an error state has occurred. You are basically doing it correctly, but instead of catching Exception you should only catch FacebookOAuthException like this:
try {
FacebookApp app = new FacebookApp(FacebookToken);
var args = new Dictionary<string, object>();
args["message"] = "hi";
args["caption"] = "appcaption";
args["description"] = "appdescription";
args["name"] = "appname";
args["picture"] = "apppicture";
args["link"] = "applink";
app.ApiAsync("/me/feed", args, (X) => { calback(X); }, HttpMethod.Post);
}
catch (FacebookOAuthException) {
// This is where you would reauthorize the user or send them to a 'login' page
Uri url = new Uri("/MyFacebook.xaml", UriKind.Relative);
NavigationService.Navigate(url);
}
I would also suggest reading up on .Net exception handling to give you a better understanding of when and why they are used. http://msdn.microsoft.com/en-us/library/ms229005.aspx
Using the FacebookAuthenticationResult you can check if there was an error and then avoid to do the request:
private void FacebookLoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
FacebookAuthenticationResult authResult;
if (FacebookAuthenticationResult.TryParse(e.Uri, out authResult))
{
if (authResult.ErrorReason == "user_denied")
{
// Do something knowing that this failed (cancel). }
else
{
fbApp.Session = authResult.ToSession();
loginSucceeded();
}
}
}
Have you seen this post? http://dotnetslackers.com/articles/net/wFace-windows-phone-7-facebook-integration-part-1.aspx
Specifically, take a look at part 3 - http://dotnetslackers.com/articles/net/wp7Gesicht-windows-phone-7-facebook-integration-part-3.aspx
Could you store the access token, and then on error, show the login page again?

Categories

Resources