Moonlight、WebClient 和“调用目标已引发异常”

发布于 2024-12-07 00:18:09 字数 3664 浏览 0 评论 0 原文

我正在编写一个小引擎来从一些 .php 文件下载文本,我已经在 Visual c# 中完成了这个引擎,并且没有遇到问题。

我正在这样做:

        [ ... ]
        WebClient client = null;

        try {
            client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler( CompleteDownload );
            client.AllowReadStreamBuffering = true;
            client.DownloadStringAsync( new Uri( "http://blabla/bla.php" ) );
        }
        catch (Exception e) {
            lbl1.Text = e.Message;
        } 
        [ ... ]

这是为了“捕获”下载的数据:

public void CompleteDownloadPcops( object sender, DownloadStringCompletedEventArgs ea ) {
    if ( ea.Error == null ) {
        try{
            lbl1.Text = ea.Result;
        }
        catch(Exception e) {
            lbl2.Text = e.Message;
        }
    }
}

执行此代码,我在 lbl1 上得到异常异常已由调用的目标抛出CompleteDownloadlbl1.Text = ea.Result; 的结果。为什么?那么,知道原因后,该如何解决呢?

更多信息:我在 Ubuntu 11.04 平台上的 monodevelop 2.4 中使用 Moonlight。

更新

我已经按照您的推荐将我的系统更新到 MonoDevelop 2.6。现在,执行同样的操作,我在 ea.Error 上收到错误。该消息是(西班牙语):

System.Security.SecurityException ---> System.Security.SecurityException:安全错误。 System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) en System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.b__4(Object sendState) en System.Net.Browser.AsyncHelper.<>c__DisplayClass4.b__1(Object sendState) --- 内部例外情况的最终解决 --- System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, 对象状态) zh System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) System.Net.WebClient.GetWebResponse(WebRequest 请求, IAsyncResult 结果) zh_cn System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult 结果)

我现在使用的完整代码是:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        WebClient client = null;
        try
        {
            client = new WebClient();
            client.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.AllowReadStreamBuffering = true;
            client.DownloadStringAsync(new Uri("http://carles.lambdafunction.com/a/try.php"));
        }
        catch (Exception ex)
        {
            lbl1.Text = ex.Message;
            btn1.Content = "A";
        }
    }

    void client_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            try
            {
                lbl2.Text = e.Result;
            }
            catch (Exception ex)
            {
                lbl1.Text = ex.Message;
                lbl2.Text = ex.InnerException.ToString();
                btn1.Content = "C";
            }
        }
        else
        {
            lbl1.Text = e.Error.ToString();
            btn1.Content = "B";
            txt1.Text = e.Error.ToString();
        }
    }
}

您可以看到网络调用的输出(到虚拟页面/p/try.php),这非常简单。真的,现在,我迷路了,因为我正在遵循本教程: http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx .

I'm writing a little engine to download text from some .php files, I've done this engine in Visual c# and I haven't got problems.

I'm doing this:

        [ ... ]
        WebClient client = null;

        try {
            client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler( CompleteDownload );
            client.AllowReadStreamBuffering = true;
            client.DownloadStringAsync( new Uri( "http://blabla/bla.php" ) );
        }
        catch (Exception e) {
            lbl1.Text = e.Message;
        } 
        [ ... ]

And this to "catch" the downloaded data:

public void CompleteDownloadPcops( object sender, DownloadStringCompletedEventArgs ea ) {
    if ( ea.Error == null ) {
        try{
            lbl1.Text = ea.Result;
        }
        catch(Exception e) {
            lbl2.Text = e.Message;
        }
    }
}

Executing this code I get, on lbl1 the exception Exception has been thrown by the target of an invocation, result of lbl1.Text = ea.Result; in CompleteDownload. Why? And, after knowing the reason, how I can solve it?

More info: I'm using moonlight in monodevelop 2.4 on Ubuntu 11.04 platform.

UPDATE

I've update my system to MonoDevelop 2.6 as you recomended me. Now, doing the same, I've get an error on ea.Error. The message is (in spanish):

System.Security.SecurityException ---> System.Security.SecurityException: Error de seguridad.
en System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
en System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
en System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)
--- Fin del seguimiento de la pila de excepciones internas ---
en System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
en System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
en System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
en System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
.

The full code I'm using now is:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        WebClient client = null;
        try
        {
            client = new WebClient();
            client.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.AllowReadStreamBuffering = true;
            client.DownloadStringAsync(new Uri("http://carles.lambdafunction.com/a/try.php"));
        }
        catch (Exception ex)
        {
            lbl1.Text = ex.Message;
            btn1.Content = "A";
        }
    }

    void client_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            try
            {
                lbl2.Text = e.Result;
            }
            catch (Exception ex)
            {
                lbl1.Text = ex.Message;
                lbl2.Text = ex.InnerException.ToString();
                btn1.Content = "C";
            }
        }
        else
        {
            lbl1.Text = e.Error.ToString();
            btn1.Content = "B";
            txt1.Text = e.Error.ToString();
        }
    }
}

You can see the output of the web call (to the dummy page /p/try.php), it's really simple. Really, now, I'm lost because I'm following this tutorial: http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2024-12-14 00:18:09

您的 .xap 位于哪里?

如果您尝试从不同的网站(地址/端口)读取数据,请务必阅读
"Silverlight 中的网络安全访问限制 " 并提供允许应用程序访问您的 Web 服务器的策略文件。

例如,以下 URL 均返回 404(这意味着没有策略允许 Silverlight 或 Moonlight 应用程序访问服务器)

Where is your .xap located ?

If you're trying to read data from a different web site (address / port) then be sure to read
"Network Security Access Restrictions in Silverlight" and provide a policy file that allow the application to access your web server.

E.g. both the following URL returns 404 (which means there's no policy to allow Silverlight or Moonlight applications to access the server)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文