0

I'm trying to do a simple request to a web service. Code executes in a background, not UI thread. Uri contains correct address which always return result with 200 HTTP code. Everything goes on emulator.

var request = WebRequest.Create(uri);
try
{
    var ar = request.BeginGetResponse(_ => { }, null);
    using (var response = request.EndGetResponse(ar)) //NullReferenceException rises here
    using (var stream = response.GetResponseStream())
    {
        var serializer = new DataContractJsonSerializer(typeof(AuthResult));
        return (AuthResult)serializer.ReadObject(stream);
    }
}
catch (Exception e)
{
    return new AuthResult { Error = e.Message };
}

Without debugger code always fails. When debugger attached it fails quite rarely.

System.NullReferenceException occurred
  Message=NullReferenceException
  StackTrace:
   at System.Net.Browser.ClientHttpWebRequest.InvokeGetResponseCallback(Object state)
   at System.Net.Browser.ClientHttpWebRequest.Completed(Object sender, EventArgs e)
   at MS.Internal.InternalWebRequest.OnCompleted(Object sender, EventArgs args)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

And the next one:

System.Net.WebException occurred
Message=The remote server returned an error: NotFound.
StackTrace:
   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at Contest.InteropApi.SignIn(Uri uri)
   at Contest.ViewModel.LoginViewModel.<PerformSignIn>b__4()
   at System.Reactive.Linq.QueryLanguage.<>c__DisplayClass3b`1.<>c__DisplayClass3d.<ToAsync>b__3a()
   at System.Reactive.Concurrency.Scheduler.Invoke(IScheduler scheduler, Action action)
   at System.Reactive.Concurrency.DefaultScheduler.<>c__DisplayClass1`1.<Schedule>b__0(Object _)
   at System.Reactive.Concurrency.ConcurrencyAbstractionLayerImpl.<>c__DisplayClasse.<QueueUserWorkItem>b__d(Object _)
   at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadPool.WorkItem.doWork(Object o)
   at System.Threading.Timer.ring()
Cœur
  • 37,241
  • 25
  • 195
  • 267
lorond
  • 3,856
  • 2
  • 37
  • 52

1 Answers1

0

Http is used asynchronously - you have to call EndGetResponse in the callback (first parameter) of the BeginGetResponse method. Your code calls EndGetResponse at a time the response is not ready.

Check out the sample at the end of this website: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.95%29.aspx

Maybe this http classes help you making http calls: http://mytoolkit.codeplex.com/wikipage?title=Http

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
  • Thanks! I thought `EndGetResponse` would wait for completion. Turns out, in mobile SDK this is not supported. It seems that it was enough time to load a page while stepping from `BeginGetResponse` to `EndGetResponse` in debug mode. – lorond Aug 22 '12 at 19:31