【问题标题】:The URI prefix is not recognized OnDownloadStringCompletedURI 前缀无法识别 OnDownloadStringCompleted
【发布时间】:2023-03-05 11:11:01
【问题描述】:

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

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.TargetInvocationException 未被用户代码处理 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 Message=无法识别 URI 前缀。 堆栈跟踪: w System.Net.WebRequest.Create(Uri requestUri) w System.Net.WebClient.GetWebRequest(Uri 地址) w System.Net.WebClient.DownloadStringAsync(Uri 地址,对象 userToken) 内部异常:

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

【问题讨论】:

  • 你知道我会问这个... URI 前缀是什么? _baseAddress 长什么样子?
  • 当你在它的时候。 “检查 InnerException 以获取异常详细信息”中的内容(如果有的话)
  • serviceAddress 是 "file:////PluginsService.ashx?634485607882333736"
  • {System.Net.WebException:WebClient 请求期间发生异常。 ---> System.NotSupportedException:无法识别 URI 前缀。 w System.Net.WebRequest.Create(Uri requestUri) w System.Net.WebClient.GetWebRequest(Uri address) w System.Net.WebClient.DownloadStringAsync(Uri address, Object userToken) --- Koniec śladu stosu wyjątków wewnętrznych --- }

标签: c# web-services ihttphandler


【解决方案1】:

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

当您的启动项目是 MefPlugins 项目(一个 Silverlight 项目)t 时,项目设置指示应动态创建一个网页来托管您的 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 页面。监听器可以是 IIS 之类的 Web 服务器或开发 Web 服务器,也可以是您自己实现的某个监听器。

为了解决这个问题,将 MefPlugins.Web 项目作为您的启动项目并将MefPluginsTestPage.aspx 设置为您的启动页面。现在_baseAddress 应该类似于http://localhost:6584/

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 问题在于 xap 文件的基本 URI,这取决于启动项目是哪个解决方案。看我的回答。
    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多