使用 HttpWebRequest 从 Silverlight 应用程序调用 WCF 时出错
我正在尝试从 Silverlight 应用程序调用返回 JSON 的 WCF 服务。它只是返回一个整数。我已经使用 Fiddler 来验证它永远不会调用我的网络服务。我收到一条错误消息“由于对象的当前状态,操作无效。”它发生在线上,HttpWebResponse response = (HttpWebResponse) _webRequest.EndGetResponse(result);如果需要,可以提供堆栈跟踪。
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();
}
}
更新:我已经注释掉了上面的行,上面写着“
_webRequest.ContentType = "text/json";
我的新错误说:SecurityException unhandled by user code”。我相信这意味着我应该使用 try catch,但我不确定要捕获什么类型的异常。
我的堆栈跟踪如下:
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()
另外,根据 Fiddler,我的 silverlight 应用程序现在正在调用我的 Web 服务域,查找 clientaccesspolicy.xml,然后查找 crossdomain.xml,两者都不存在。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“GET”请求不能有 Content-Type 标头(它们不能有内容)。 Silverlight 中的 HttpWebRequest 实现比桌面框架中的实现更严格。尝试删除定义该属性的 ling,它应该可以工作。
更新:您的应用程序遇到了跨域问题。为了防止某些类型的跨域攻击,SL 要求任何发送到 SL 应用程序(.xap 文件)所在域以外的域的请求都必须遵守跨域策略 - 默认情况下不允许它们。您可以在 http://msdn 中找到有关此内容的更多信息。 microsoft.com/en-us/library/cc645032(VS.95).aspx。
要解决此问题,您实际上必须向您的服务添加跨域策略,以允许 SL 应用程序使用它。
"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.