开发者

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.BrowserHttpWebReques开发者_如何学运维t.<>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.


"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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜