如何从Silverlight应用程序读取localhost上的XML文件?

时间:2021-01-19 09:12:50

I have Vista with IIS7.

我有IIS7的Vista。

I want to create a simple Silverlight application that reads an xml file from localhost.

我想创建一个从localhost读取xml文件的简单Silverlight应用程序。

I created this file (which I had to copy and click "allow" as administrator):

我创建了这个文件(我必须复制并点击“允许”作为管理员):

C:\inetpub\wwwroot\data\customers.xml

and can see it when I go here in a browser:

当我在浏览器中访问时可以看到它:

http://localhost/data/customers.xml

But when I run the following code, I get a target invocation exception:

但是,当我运行以下代码时,我得到一个目标调用异常:

using System;
using System.Net;
using System.Windows.Controls;
using System.IO;

namespace TestXmlRead234
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            WebClient client = new WebClient();
            client.OpenReadAsync(new Uri("http://localhost/data/customers.xml", UriKind.Absolute));
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            StreamReader myReader = new StreamReader(e.Result);
            Output.Text = myReader.ReadLine();
            myReader.Close();
        }
    }
}

So I created C:\inetpub\wwwroot\crossdomainpolicy.xml:

所以我创建了C:\ inetpub \ wwwroot \ crossdomainpolicy.xml:

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
    <cross-domain-access>
        <policy >
            <allow-from http-request-headers="Content-Type">
                <domain uri="*"/>
            </allow-from>
            <grant-to>
                <resource path="/" include-subpaths="true"/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>

But I still get the target invocation exception error.

但我仍然得到目标调用异常错误。

Here is the full inner exception:

这是完整的内部异常:

{System.Security.SecurityException ---> System.Security.SecurityException: Sicherheitsfehler bei System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) bei System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.b__4(Object sendState) bei System.Net.Browser.AsyncHelper.<>c__DisplayClass2.b__0(Object sendState) --- Ende der internen Ausnahmestapelüberwachung --- bei System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) bei System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) bei System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) bei System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult result)}

{System.Security.SecurityException ---> System.Security.SecurityException:Sicherheitsfehler贝System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult的asyncResult)贝System.Net.Browser.BrowserHttpWebRequest <> c__DisplayClass5.b__4(对象sendState)贝System.Net.Browser.AsyncHelper。<> c__DisplayClass2.b__0(对象sendState)---恩德DER internenAusnahmestapelüberwachung---贝System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态)贝System.Net.Browser .BrowserHttpWebRequest.EndGetResponse(IAsyncResult的asyncResult)贝System.Net.WebClient.GetWebResponse(WebRequest的请求,IAsyncResult的结果)贝System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult的结果)}

update 1: In windows explorer, I then right clicked C:\inetpub\wwwroot\data and made IIS_USERS a co-owner of that directory. But still get the same error. :-(

更新1:在Windows资源管理器中,我然后右键单击C:\ inetpub \ wwwroot \ data并使IIS_USERS成为该目录的共同所有者。但仍然得到相同的错误。 :-(

update 2: also made "everyone" co-owner of C:\inetpub\wwwroot\data, same error. :-(

更新2:还使“大家”共同拥有者C:\ inetpub \ wwwroot \ data,同样的错误。 :-(

update 3: opened command window as administrator and executed this command: netsh http add urlacl url=http://+:80/ user=MYDOMAIN\MyUserName

更新3:以管理员身份打开命令窗口并执行以下命令:netsh http add urlacl url = http:// +:80 / user = MYDOMAIN \ MyUserName

What else do I have to be able to read a text file from localhost from a Silverlight application?

还有什么能够从Silverlight应用程序中读取localhost中的文本文件?

PRAGMATIC ANSWER:

For testing locally just publish to the temporary localhost webserver port for which you don't even need a cross-domain file, then make necessary changes when you publish live:

要在本地进行测试,只需发布​​到您甚至不需要跨域文件的临时localhost网络服务器端口,然后在发布实时时进行必要的更改:

using System;
using System.Linq;
using System.Net;
using System.Windows.Controls;
using System.IO;
using System.Xml.Linq;

namespace TestWeb124
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            WebClient wc = new WebClient();
            wc.OpenReadAsync(new Uri("http://localhost:49512/customers.xml", UriKind.Absolute));
            wc.OpenReadCompleted += wc_OpenReadCompleted;
        }

        private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Output.Text = e.Error.Message;
                return;
            }
            using (Stream s = e.Result)
            {
                XDocument doc = XDocument.Load(s);
                Output.Text = doc.ToString(SaveOptions.OmitDuplicateNamespaces);
                var customers = from c in doc.Descendants("customer")
                                select new
                                {
                                    FirstName = c.Element("firstName").Value
                                };

                foreach (var customer in customers)
                {
                    Output.Text += customer.FirstName;
                }

            }
        }        



    }
}

5 个解决方案

#1


From what I've seen, the usual behaviour is to create a webservice that can get around Silverlight's cross domain issues entirely, then have Silverlight code communicate through that web service.

从我所看到的,通常的行为是创建一个可以完全解决Silverlight的跨域问题的Web服务,然后让Silverlight代码通过该Web服务进行通信。

#3


Try removing the attribute http-request-headers="Content-Type", I'm not sure what that is acheiving.

尝试删除属性http-request-headers =“Content-Type”,我不确定这是什么。

#4


I had similar issues being able to access pages on my local IIS from a Silverlight application. I ended up configuring a HTTP proxy to see what was going on. In my case Silverlight was sending the request for crossdomainpolicy.xml and IIS was correctly returning it so there were definitely no ACL issues but for some reason Silverlight just wouldn't accept it.

我有类似的问题能够从Silverlight应用程序访问本地IIS上的页面。我最终配置了HTTP代理以查看发生了什么。在我的情况下,Silverlight发送了crossdomainpolicy.xml的请求,IIS正确地返回它,所以肯定没有ACL问题,但由于某种原因Silverlight不接受它。

After trying endless options in the crossdomainpolicy file I decided I'd test out the alternative option of the Flash crossdomain.xml policy file which Silverlight also understands and it worked first time.

在crossdomainpolicy文件中尝试无穷无尽的选项后,我决定测试一下Flash crossdomain.xml策略文件的替代选项,该文件Silverlight也理解并且它第一次工作。

Might work for you.

可能会为你工作。

<?xml version=""1.0""?>
<!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
<cross-domain-policy>
    <allow-http-request-headers-from domain=""*"" headers=""*""/>
</cross-domain-policy>

Edit: To try this out you will need to remove the crossdomainpolicy.xml file as Silverlight requests that first and if it finds it won't request crossdomain.xml.

编辑:要尝试此操作,您需要删除crossdomainpolicy.xml文件,因为Silverlight首先请求它,如果它发现它不会请求crossdomain.xml。

#5


Your xml is illformed and has an extra > near the top.

您的xml格式错误,并且附近有一个额外的>顶部。

#1


From what I've seen, the usual behaviour is to create a webservice that can get around Silverlight's cross domain issues entirely, then have Silverlight code communicate through that web service.

从我所看到的,通常的行为是创建一个可以完全解决Silverlight的跨域问题的Web服务,然后让Silverlight代码通过该Web服务进行通信。

#2


#3


Try removing the attribute http-request-headers="Content-Type", I'm not sure what that is acheiving.

尝试删除属性http-request-headers =“Content-Type”,我不确定这是什么。

#4


I had similar issues being able to access pages on my local IIS from a Silverlight application. I ended up configuring a HTTP proxy to see what was going on. In my case Silverlight was sending the request for crossdomainpolicy.xml and IIS was correctly returning it so there were definitely no ACL issues but for some reason Silverlight just wouldn't accept it.

我有类似的问题能够从Silverlight应用程序访问本地IIS上的页面。我最终配置了HTTP代理以查看发生了什么。在我的情况下,Silverlight发送了crossdomainpolicy.xml的请求,IIS正确地返回它,所以肯定没有ACL问题,但由于某种原因Silverlight不接受它。

After trying endless options in the crossdomainpolicy file I decided I'd test out the alternative option of the Flash crossdomain.xml policy file which Silverlight also understands and it worked first time.

在crossdomainpolicy文件中尝试无穷无尽的选项后,我决定测试一下Flash crossdomain.xml策略文件的替代选项,该文件Silverlight也理解并且它第一次工作。

Might work for you.

可能会为你工作。

<?xml version=""1.0""?>
<!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
<cross-domain-policy>
    <allow-http-request-headers-from domain=""*"" headers=""*""/>
</cross-domain-policy>

Edit: To try this out you will need to remove the crossdomainpolicy.xml file as Silverlight requests that first and if it finds it won't request crossdomain.xml.

编辑:要尝试此操作,您需要删除crossdomainpolicy.xml文件,因为Silverlight首先请求它,如果它发现它不会请求crossdomain.xml。

#5


Your xml is illformed and has an extra > near the top.

您的xml格式错误,并且附近有一个额外的>顶部。