【问题标题】:Error when making call to WCF from Silverlight application using HttpWebRequest使用 HttpWebRequest 从 Silverlight 应用程序调用 WCF 时出错
【发布时间】:2011-08-15 06:47:06
【问题描述】:

我正在尝试从 Silverlight 应用程序调用返回 JSON 的 WCF 服务。它只是返回一个整数。我使用 Fiddler 来验证它永远不会调用我的网络服务。我收到一条错误消息,提示“由于对象的当前状态,操作无效。”发生就行了,HttpWebResponse response = (HttpWebResponse) _webRequest.EndGetResponse(result);如果需要,可以提供 Stacktrace。

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。我相信这意味着我应该使用 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,这两者都不存在。

【问题讨论】:

    标签: silverlight wcf web-services json httpwebrequest


    【解决方案1】:

    “GET”请求不能有 Content-Type 标头(它们不能有内容)。 Silverlight 中的 HttpWebRequest 实现比桌面框架中的更严格。尝试删除定义该属性的 ling,它应该可以工作。

    更新:您在应用程序中遇到了跨域问题。为了防止某些类型的跨域攻击,SL 要求任何去往 SL 应用程序(.xap 文件)所在域以外的域的请求都受制于跨域策略 - 默认情况下不允许使用它们。你可以在http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx找到更多信息。

    要解决此问题,您基本上必须向您的服务添加跨域策略,以允许 SL 应用使用它。

    【讨论】:

    • 我注释掉了设置 ContentType 的行,现在我得到一个不同的错误。这是错误堆栈跟踪的顶部: System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
    • 另外,请求仍未发送到我的网络服务。 Fiddler 仍然没有显示任何呼叫的迹象。新的错误发生在 HttpWebResponse response = (HttpWebResponse)_webRequest.EndGetReponse(result);线。感谢您的回复!
    • 我得到一个 SecurityException 未被用户代码处理。我假设我需要某种 catch 语句,但我不知道要捕获哪种类型的异常。我将使用我的错误截图和堆栈跟踪的副本发布更新帖子。我也注意到它现在正在调用我的网络服务的域来寻找 accesspolicy.xml
    • 谢谢,跨域问题是我的问题。
    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多