OnDownloadStringCompleted 无法识别 URI 前缀

发布于 2024-11-29 03:52:30 字数 3040 浏览 1 评论 0原文

我有一个简单的方法问题,该方法从 asp.net 4 ashx 获取字符串。我正在运行来自 silverlight 应用程序的以下方法,该应用程序由该 asp.net 应用程序托管。

private void LoadPlugins()
{
    var serviceAddress = _baseAddress
        + "PluginsService.ashx?"
        + DateTime.Now.Ticks;

    var client = new WebClient();
    client.DownloadStringCompleted += client_DownloadStringCompleted;
    client.DownloadStringAsync(new Uri(serviceAddress));
}

void client_DownloadStringCompleted(
    object sender,
    DownloadStringCompletedEventArgs e)
{
    var plugins = e.Result.Split(
        new string[] { Environment.NewLine },
        StringSplitOptions.RemoveEmptyEntries);
    foreach (var plugin in plugins)
    {
        AddXap(_baseAddress + plugin);
    }
}

PluginsService.ashx.cs:

namespace MefPlugins.Web
{
    /// <summary>
    /// Summary description for PluginsService
    /// </summary>
    public class PluginsService : IHttpHandler
    {
        private const string PluginsFolderName = "Plugins/";

        public void ProcessRequest(HttpContext context)
        {
            //var pluginFolder = new DirectoryInfo(
            //    HttpContext.Current.Server.MapPath(
            //    PluginsFolderName));
            //var response = new StringBuilder();

            //if (pluginFolder.Exists)
            //{
            //    foreach (var xap in pluginFolder.GetFiles("*.xap"))
            //    {
            //        response.AppendLine(
            //            PluginsFolderName + xap.Name);
            //    }
            //}

            var response = new StringBuilder();
            response.Append("test");
            context.Response.ContentType = "text/plain";
            context.Response.Write(response);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

我收到错误:

System.Reflection.TargetInitationException 未由用户代码处理 Message=操作过程中发生异常,导致结果无效。检查 InnerException 以获取异常详细信息。 堆栈跟踪: w System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() w System.Net.DownloadStringCompletedEventArgs.get_Result() w MefPlugins.MainPage.client_DownloadStringCompleted(对象发送者,Dow​​nloadStringCompletedEventArgs e) w System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) w System.Net.WebClient.DownloadStringOperationCompleted(对象 arg) 内部异常:System.Net.WebException Message=WebClient 请求期间发生异常。 内部异常:System.NotSupportedException 消息=无法识别 URI 前缀。 堆栈跟踪: w System.Net.WebRequest.Create(Uri requestUri) w System.Net.WebClient.GetWebRequest(Uri地址) w System.Net.WebClient.DownloadStringAsync(Uri 地址,对象 userToken) InnerException:

哪里可能有错误?这是来自 http://www.galasoft.ch/ 的示例sl4u/code/chapter20/20.02-Mef/MefPlugins.zip

I've got a problem with simply method whch gets string from asp.net 4 ashx. I'm running below methods from silverlight application which is being hosted by this asp.net application.

private void LoadPlugins()
{
    var serviceAddress = _baseAddress
        + "PluginsService.ashx?"
        + DateTime.Now.Ticks;

    var client = new WebClient();
    client.DownloadStringCompleted += client_DownloadStringCompleted;
    client.DownloadStringAsync(new Uri(serviceAddress));
}

void client_DownloadStringCompleted(
    object sender,
    DownloadStringCompletedEventArgs e)
{
    var plugins = e.Result.Split(
        new string[] { Environment.NewLine },
        StringSplitOptions.RemoveEmptyEntries);
    foreach (var plugin in plugins)
    {
        AddXap(_baseAddress + plugin);
    }
}

PluginsService.ashx.cs:

namespace MefPlugins.Web
{
    /// <summary>
    /// Summary description for PluginsService
    /// </summary>
    public class PluginsService : IHttpHandler
    {
        private const string PluginsFolderName = "Plugins/";

        public void ProcessRequest(HttpContext context)
        {
            //var pluginFolder = new DirectoryInfo(
            //    HttpContext.Current.Server.MapPath(
            //    PluginsFolderName));
            //var response = new StringBuilder();

            //if (pluginFolder.Exists)
            //{
            //    foreach (var xap in pluginFolder.GetFiles("*.xap"))
            //    {
            //        response.AppendLine(
            //            PluginsFolderName + xap.Name);
            //    }
            //}

            var response = new StringBuilder();
            response.Append("test");
            context.Response.ContentType = "text/plain";
            context.Response.Write(response);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

I get an error:

System.Reflection.TargetInvocationException was unhandled by user code
Message=An exception occurred during the operation, making the result invalid. Check InnerException for exception details.
StackTrace:
w System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
w System.Net.DownloadStringCompletedEventArgs.get_Result()
w MefPlugins.MainPage.client_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
w System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)
w System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
InnerException: System.Net.WebException
Message=An exception occurred during a WebClient request.
InnerException: System.NotSupportedException
Message=The URI prefix is not recognized.
StackTrace:
w System.Net.WebRequest.Create(Uri requestUri)
w System.Net.WebClient.GetWebRequest(Uri address)
w System.Net.WebClient.DownloadStringAsync(Uri address, Object userToken)
InnerException:

Where could be a bug? this is an example from http://www.galasoft.ch/sl4u/code/chapter20/20.02-Mef/MefPlugins.zip

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

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

发布评论

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

评论(2

偏闹i 2024-12-06 03:52:30

错误的原因在于选择哪个项目作为启动项目的启动对象。

当您的启动项目是 MefPlugins 项目(Silverlight 项目)时,项目设置指示应动态创建一个网页来托管您的 Silverlight 应用程序(右键单击项目,选择“属性”并转到调试选项卡)。

您遇到的问题与用作 PluginsService.ashx 文件前缀的位置有关。 _baseAddress 在主页面构造函数中通过以下代码设置:

var xapUri = App.Current.Host.Source;
_baseAddress = xapUri.AbsoluteUri
                     .Substring(0, xapUri.AbsoluteUri.IndexOf(xapUri.AbsolutePath)) 
               + "/";

这意味着 xap 文件的 Uri 用于确定基本 URI。现在,由于该项目正在使用动态生成的容器页面 - 该页面位于您的硬盘驱动器上并从那里启动 - 上面的代码采用的文件系统根目录是 _baseAddress。显然,代码不会找到 PluginsService.ashx 页面,因为它不在那里。

此外,.ashx 文件需要某种形式的 http 侦听器,将请求从端口路由到 .ashx 页面。侦听器可以是 Web 服务器(例如 IIS)或开发 Web 服务器,也可以是您自己实现的某个侦听器。

要解决此问题,请将 MefPlugins.Web 项目设置为启动项目,并将 MefPluginsTestPage.aspx 设置为启动页面。现在 _baseAddress 应该类似于 http://localhost:6584/

现在使用此基地址查找 PluginsService.ashx 页面时,它将生成正确的资源 URI (http://localhost:6584/PluginsService.ashx 在我们的例子中)。

一般来说,.ashx 文件是 Web 服务(IIS、调试 Web 服务器,甚至某些自己的实现)的扩展,它们是 silverlight 客户端的一部分。

The reason for the error lies in which project is startup object selected as your startup project.

When your startup project is the MefPlugins projec (a Silverlight Project)t, the project settings indicate that a web page should be dynamically created to host your Silverlight application (right click on project, select Properties and go to the Debug tab).

The problem you runing into has to do with the location that is used as a prefix for the PluginsService.ashx file. The _baseAddress is set by in the main page constructor by the following code:

var xapUri = App.Current.Host.Source;
_baseAddress = xapUri.AbsoluteUri
                     .Substring(0, xapUri.AbsoluteUri.IndexOf(xapUri.AbsolutePath)) 
               + "/";

This means that the Uri of the xap file is used to determine the base uri. Now, as the project is using a dynamically generated container page - which is located on your hard drive and started from there - the above code takes the filesystem root is the _baseAddress. Obviously, the code will not find the PluginsService.ashx page, as it is not there.

Additionally, an .ashx file needs some form of http listener that routes the request from a port to your .ashx page. The listener can be a web server like IIS or the development web server, or some listener you implemented yourself.

To solve the problem make the MefPlugins.Web project your startup project and set MefPluginsTestPage.aspx as your startup page. Now _baseAddress should be something simlilar to http://localhost:6584/.

When this base address is now used to find the PluginsService.ashx page, it will result in a correct URI for the resource (http://localhost:6584/PluginsService.ashx in our case).

In general .ashx files are extensions to a web service (IIS, debug web server, or even some own implementation) they are not part of the silverlight client.

李白 2024-12-06 03:52:30

我怀疑问题在于您传递给 DownloadStringAsync 的 Uri 是相对 Uri。即:“file://PluginsService.ashx”是相对于当前目录的。您可能需要一个绝对 Uri(即完全限定路径名),如“file://C:\projects\test\PluginsService.ashx”。

I suspect the problem is that the Uri you're passing to DownloadStringAsync is a relative Uri. That is: "file://PluginsService.ashx" is relative to the current directory. You probably want an absolute Uri (i.e. fully-qualified path name) as in "file://C:\projects\test\PluginsService.ashx".

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