I try to wait for an HttpWebRequest to finish without writing a dozen AsyncCallbacks. For that I tried to handle the call in a Task and use WaitOne within it --> so the ui thread will not be blocked.
The Problem now is that there appears a NotSupportedException everytime I call it and I don´t understand why. Can someone tell me more about that and maybe how to fix this issue?
Here the code:
Task.Factory.StartNew((x)=>
{
HttpWebRequest request = WebRequest.CreateHttp(baseUri + "/api/" + ControllerName);
request.Method = "POST";
request.ContentType = "application/json";
request.Headers["Session"] = SessionKey;
IAsyncResult GetRequestStreamResult = request.BeginGetRequestStream(null, null);
GetRequestStreamResult.AsyncWaitHandle.WaitOne(); //<-- That causes the exception
using (Stream RequestStream = request.EndGetRequestStream(GetRequestStreamResult))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(Parameter.GetType());
serializer.WriteObject(RequestStream, Parameter);
}
Best regards
Christoph
I found this article. That pointed me to the direction that maybe it is no common silverlight issue, but a Problem with the actual implementation of the IAsyncResult which is of System.Net.Browser.OHWRAsyncResult. And this simply throws a NotSupportedException in any case when accessing the AsyncWaitHandle getter.
I helped me out by writing this small Extension method:
private static void WaitForIt(this IAsyncResult result)
{
while (!result.IsCompleted)
{
Thread.Sleep(50);
}
}
Not really pretty but it works...
Regards
Christoph
Use this handler...
while ((GetRequestStreamResult.AsyncWaitHandle.WaitOne(1000, true) == false)
&& (GetRequestStreamResult.IsCompleted == false))
{
// Do nothing
}
Related
I am using the below code to read JSON from an endpoint in my Xamarin crossplatform project and I am getting error
Cannot read disposed object exception or it fires ObjectDisposedException
IS it something wrong with code Can I write it in a better way ?
public async Task<APISchoolDetailModel> GetSchooDetailsAsync()
{
APISchoolDetailModel api_data = new APISchoolDetailModel();
try
{
var client = new System.Net.Http.HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
var web_client = await client.GetAsync("http://appapitest.net/APIs/Student/Schooldetails");
var response_string= web_client.Content.ReadAsStringAsync().Result;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(api_data.GetType());
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(response_string));
api_data = serializer.ReadObject(ms) as APISchoolDetailModel;
}
catch (Exception ex) { }
return api_data;
}
The controller comes till the line var web_client = await client.GetAsync(" and then its not going further and after few seconds I am getting exception
Is any better way to write this code for reading and parsing JSON
#Gserg pointed out something important you should not do this:
var response_string= web_client.Content.ReadAsStringAsync().Result;
in stead of that use:
var response_string= await web_client.Content.ReadAsStringAsync();
within an async Task method:
is you use .Result this may be causing deadlocks within threads or the same stuff that you are experiencing because a thread may be trying to update or use a variable that is already collected from the GC.
Hi everyone im trying to create a method that will always return a url source, if for example internet goes off it will continue working until it comes up and return the url source and so on if something else occurs. So far in my method when i "turn off" the internet and "turn it on" back procedures continue normaly but im having an issue when a timeout occurs and im "falling" in a loop i know that the while(true) is not the right approach but im using it for my tests.
So how can i skip the timeout exception and "retry" my method?
public static async Task<string> GetUrlSource(string url)
{
string source = "";
while (true)
{
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(url);
hwr.AllowAutoRedirect = true;
hwr.UserAgent = UserAgent;
hwr.Headers.Add(hd_ac_lang[0], hd_ac_lang[1]);
hwr.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
hwr.Timeout = 14000;
try
{
using (var response = hwr.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
source = await reader.ReadToEndAsync();
if (check_source(source))
{
return source;
}
}
}
}
catch (WebException ex)
{
hwr.Abort();
if (ex.Status == WebExceptionStatus.ProtocolError)
{
if (((HttpWebResponse)ex.Response).StatusCode == HttatusCode.NotFound)
{
// handle the 404 here
return "404";
}
}
else
{
Console.WriteLine(ex.Status.ToString());
}
}
}
}
Note: i used to have the hwr.Abort(); into a finnaly clause but it didnt help.
Edit: the console is writting this message every 14 seconds as my timeout i think its something related with that.
ِAn alternative solution to get rid of timeout problem can be is to use WebBrowser component and to navigate to the required url(webbrowser1.Navigate(url);) ,and to wait in a loop until the documentcompleted event is raised and then to get the source code by this line :
string source = webbrowser1.DocumentText;
Well it seems that i found a solution that was related with the service point of the request.
So in my catch when a timeout occurs im using this to release the connection.
hwr.ServicePoint.CloseConnectionGroup(hwr.ConnectionGroupName);
I'll keep this updated.
I'm somewhat new to C# and I'm having some issues retrieving a textbox value in an asynchronous method. I the thread to retrieve the text input in the UI and use it in the code. VS 2010 accepts my code but when I start to debug it gives me the following exception
Invalid cross-thread access. Ideas? Am I missing something?
public void Response_Completed(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
JObject rootObject = JObject.Load(new JsonTextReader(streamReader));
string tracknum = trackid.Text; // Invalid cross-thread access exception
string source = rootObject[tracknum]["source"].ToString();
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
//removed
});
}
}
Note: I'm attempting to do this on the Windows Phone 7 Platform
You should to get textbox value on UI thread, instead of other threads. Invoke method executes the specified delegate on the UI thread:
string tracknum = (string)trackid.Invoke(new Func<string>(() => trackid.Text));
Edit:
On Windows Phone:
string tracknum = string.Empty;
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
tracknum = trackid.Text;
string source = rootObject[tracknum]["source"].ToString();
});
You'll receive such exception because you have tried to access an object from a different thread than that it was created on (tried to access the thread asynchronously)
To get around this, we'll need to execute a delegate on the thread that owns the control's underlying window handle. In such cases, we may use Invoke(Delegate method)
Example
public void Response_Completed(IAsyncResult result)
{
this.Invoke((MethodInvoker)delegate
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
JObject rootObject = JObject.Load(new JsonTextReader(streamReader));
string tracknum = trackid.Text; // Invalid cross-thread access exception
string source = rootObject[tracknum]["source"].ToString();
}
});
}
MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list. This delegate can be used when making calls to a control's Invoke method, or when you need a simple delegate but do not want to define one yourself.
Thanks,
I hope you find this helpful :)
I have the following bits of code, scattered throughout my application. I'd really like to boilerplate it, and place it in either a static class, or some utility set of classes so I don't have all this duplication.
However, the small bits of the function are unique in such a way that I don't know how to refactor it.
private void callResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
string responseData = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
ExpectedResponseType regResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ExpectedResponseType>(responseData);
if (regResponse.ok == "0")
{
//error - handle the msg
//whether the user not loggin or not exist
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(CustomErrorMessage);
});
}
else
{
//check the variables unique to the ExpectedResponseType and do Stuff here;
}
}
catch (WebException e)
{
// Error treatment
// ...
Debug.WriteLine("error " + e);
}
I am most curious how to pass in "ExpectedResponseType", such that it might be any Class, (i.e., is there a way to pass in T?) or possibly how to fire events that can then be executed by the UI thread and handled appropriately.
Thanks.
edit: "ExpectedResponseType" or "T" is a large collection of classes for each type of server call. For example I have LoginResponse, RegisterResponse, GetFilesResponse, UpdateResponse, DownloadResponse, etc..
EDIT: I have removed earlier example as it would not work with the delegate signature.
In order to handle the checking of the parameters specific to the type T you will need to add a little abstraction, the cleanest way is probably to wrap your code in a templated class that allows the registration of a delegate for handling the checking, I'm sure this is a specific pattern but cannot recall which one:
public class ResponseHandler<T>
{
public ResponseHandler(Action<T> typeSpecificCheckFunction)
{
this.CheckVariables = typeSpecificCheckFunction;
}
Action<T> CheckVariables;
public void callResponseCallback(IAsyncResult asynchronousResult)
{
// stuff
T regResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseData);
CheckVariables(response);
// stuff
}
}
In response to you question about handling a large variety of T, perhaps the cleaned up code above clears it up, if not then this is what generics are for - provided you know what you are expecting in each case. So for each type you were expecting you would call it something along the lines of:
var handler = new ResponseHandler<ExpectedResponseType>( response =>
{
// code to check your response properties here
});
xxx.RegisterResponseCallback(handler.callResponseCallback);
I have some C# code that pulls down a remote website using the HttpWebRequest class. I'm handling errors with a try/catch, but some errors (like Webrequest and IOException) don't seem to be getting "caught" with the way I have it setup:
try
{
StartScrap("http://www.domain.com");
}
catch (Exception ex)
{
LogError(ex.ToString();
}
private void StartScrap(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseText = String.Empty;
using (StreamReader readerStream = new StreamReader(responseStream, System.Text.Encoding.UTF8))
{
responseText = readerStream.ReadToEnd(); <-- I will sometimes get a Webexception error here that won't get caught above and stops the code
}
}
}
Update: There is more to the code, so maybe it is something outside of the code I posted? I am basically using this code in a Windows Application on a form that has a NotifyIcon. I'm using the Timer class to run the code at a certain timer interval. This is how I have it setup:
public TrayIcon()
{
InitializeComponent();
}
private void TrayIcon_Load(object sender, EventArgs e)
{
try
{
StartScrap("http://www.domain.com");
}
catch (Exception ex)
{
LogError(ex.ToString());
}
finally
{
StartTimer();
}
}
private void StartTimer()
{
Timer Clock = new Timer();
Clock.Interval = 600000;
Clock.Start();
Clock.Tick += new EventHandler(TrayIcon_Load);
}
What exactly do you mean by "stops the code"? Are you running in a debugger by any chance? My guess is that if you run outside the debugger - or just hit "run" again in the debugger - you'll get into the catch block with no problems. Alternatively, go into the debugger settings and change at which point the debugger steps in.
Of course, if this isn't happening in the debugger, we just need more information about exactly what you're seeing.
Could it be that LogError is throwing an exception?
Frankly speaking I am not sure what exactly happening but I will suggest you to go with ELMAH.(Error Logging Modules and Handlers)
Here is a step by step How to for ELMAH.
Nevermind, I found out I was calling the wrong function my Timer class and it was bypassing the event handler.