Asp.net Silverlight&x27;s网络客户端是';无法连接到我的服务器

Asp.net Silverlight&x27;s网络客户端是';无法连接到我的服务器,asp.net,httpwebrequest,silverlight-2.0,webclient,Asp.net,Httpwebrequest,Silverlight 2.0,Webclient,我这里有个问题 我有一个ASP.net网站,上面有一个silverlight 2应用程序。 我希望该站点能够通过silverlight应用程序进行来回通信,我正在通过http请求进行通信。顺便说一句,如果有人知道更好的方法,请告诉我 我的服务器设置了以下http侦听器。我从某个教程网站复制了这篇文章,因为它目前主要是实验性的: HttpListener listener = new HttpListener ( ); listener.Prefixes.Add("htt

我这里有个问题

我有一个ASP.net网站,上面有一个silverlight 2应用程序。 我希望该站点能够通过silverlight应用程序进行来回通信,我正在通过http请求进行通信。顺便说一句,如果有人知道更好的方法,请告诉我

我的服务器设置了以下http侦听器。我从某个教程网站复制了这篇文章,因为它目前主要是实验性的:

      HttpListener listener = new HttpListener (  );
      listener.Prefixes.Add("http://localhost:4531/MyApp/");  
      listener.Start(  );                                         

      // Wait for a client request:
      HttpListenerContext context = listener.GetContext(  );

      // Respond to the request:
      string msg = "You asked for: " + context.Request.RawUrl;
      context.Response.ContentLength64 = Encoding.UTF8.GetByteCount (msg);
      context.Response.StatusCode = (int) HttpStatusCode.OK;

      using (Stream s = context.Response.OutputStream)
      using (StreamWriter writer = new StreamWriter (s))
        writer.Write (msg);

      listener.Stop(  );
我正在使用以下代码发送请求:

 private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            b.Content = "Hello World";

            Uri serviceUri = new Uri("http://localhost:4531/MyApp/");
            WebClient downloader = new WebClient();
            downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(TestDownloadStoriesCompleted);
            downloader.DownloadStringAsync(serviceUri);

        }
        void TestDownloadStoriesCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                TextBox1.Text = e.Result;
            }
        }
我的问题是,我可以使用几乎相同的代码从控制台应用程序连接到Web服务器(我通过在代码中设置断点进行了测试),但是在silverlight中单击按钮时什么也没有发生。(我添加了“Hello World”来测试我是否确实将代理连接到按钮。)

我已经读到silverlight需要通过webclient进行连接的策略,但是如果我对服务器和silverlight应用程序使用相同的服务器和相同的域,情况就不应该如此

谢谢你的回复

编辑:我收到此异常:

System.Security.SecurityException-->System.Security.SecurityException:安全错误

另外,根据我显然是源站点的内容,xap的部署URI和请求URI也必须是同一个端口

但是,当我将服务器的属性设置为托管在特定端口上,并将侦听器设置为侦听同一端口时,它会失败,并显示消息,该进程无法访问该文件,因为另一个进程正在使用该文件。我认为这是因为http侦听器无法侦听用于承载它的同一端口:|
但是,我怎样才能让Silverlight执行主机源webclient请求呢?

因为这只是一个测试,所以在TestDownloadStoriesCompleted处理程序中添加一个“else TextBox1.Text=e.Error.ToString();”以查看您得到的错误

编辑:

您不能在同一端口上同时承载asp.net应用程序和侦听器-您可以通过使用不同的端口并从httplistener提供clientaccesspolicy.xml来解决此问题


不过,我认为您应该看看WCF web服务(将svc添加到asp.net应用程序中)。下面是一个例子。

我现在使用HTTP处理程序进行通信。尽管我仍然想尝试一些WCF,但它们似乎可以很好地满足我的需要。

您可以使用以下工具 要实际查看请求过程中发生的情况。。。。 这可以为进一步调试提供一些帮助