Error when making call to WCF from Silverlight application using HttpWebRequest
-
I am trying to make a call from a Silverlight application to a WCF service returning JSON. It's simply returning an integer. I have used Fiddler to verify that it is never making the call to my webservice. I am getting an error that says "Operation is not valid due to the current state of the object." It occurs on the line, HttpWebResponse response = (HttpWebResponse) _webRequest.EndGetResponse(result); Stacktrace can be provided if needed. public MainPage() { InitializeComponent(); StartWebRequest(); } void StartWebRequest() { HttpWebRequest _webRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://www.example.com/MyJSON.svc/onlineusercount")); _webRequest.ContentType = "text/json"; _webRequest.Method = "GET"; _webRequest.BeginGetResponse(FinishWebRequest, _webRequest); } void FinishWebRequest(IAsyncResult result) { HttpWebRequest _webRequest = (HttpWebRequest)result.AsyncState; HttpWebResponse response = (HttpWebResponse)_webRequest.EndGetResponse(result); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); string responseString = streamRead.ReadToEnd(); needle.Value = Convert.ToInt32(responseString); // Close the stream object streamResponse.Close(); streamRead.Close(); // Release the HttpWebResponse response.Close(); } } UPDATE: I have commented out the line above that says _webRequest.ContentType = "text/json"; My new error says: SecurityException unhandled by user code. I believe this means I should use a try catch, but I am not sure what type of exception to catch. My stack trace is as follows: at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at FuelizerGuage.MainPage.FinishWebRequest(IAsyncResult result) at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassd.<InvokeGetResponseCallback>b__b(Object state2) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() Also according to Fiddler, my silverlight application is now making at call to my webservice domain looking for clientaccesspolicy.xml and then looks for crossdomain.xml, neither of which exist.
-
Answer:
"GET" requests cannot have a Content-Type header (they cannot have content). The HttpWebRequest implementation in Silverlight is more strict than the one in the desktop framework. Try removing the ling which defines that property and it should work. Update: you're hitting a cross-domain problem in your application. To prevent some kinds of cross-domain attacks, SL requires that any requests going to a domain other than the one where the SL applicaiton (the .xap file) originated to be subject to a cross-domain policy - they're disallowed by default. You can find more information about this at http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx. To solve this problem you'll essentially have to add a cross-domain policy to your service to allow SL apps to consume it.
puddinman13 at Stack Overflow Visit the source
Related Q & A:
- How to dispaly HTML page in Silverlight application?Best solution by devproconnections.com
- How to call a web service using HttpWebRequest?Best solution by Stack Overflow
- Is it possible to develop iPhone application using C#?Best solution by Stack Overflow
- How to choose random transition for each picture when making a video with windows movie maker?Best solution by Yahoo! Answers
- When Should I Call back about my Job Application?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.