gpt4 book ai didi

c# - Moonlight、WebClient 和 "Exception has been thrown by the target of an invocation"

转载 作者:行者123 更新时间:2023-11-30 18:40:54 29 4
gpt4 key购买 nike

我正在编写一个小引擎来从一些 .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 上执行我得到的代码异常 Exception has been thrown by the target of an invocation , lbl1.Text = ea.Result; 的结果在 CompleteDownload .为什么?以及,知道原因后,如何解决?

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

更新

我已经按照您的推荐将我的系统更新到 MonoDevelop 2.6。现在,做同样的事情,我在 ea.Error 上遇到错误.消息是(西类牙语):

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

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

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 .

最佳答案

您的 .xap 位于何处?

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

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

关于c# - Moonlight、WebClient 和 "Exception has been thrown by the target of an invocation",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7602218/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com