我在使用从 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);
}
}
插件服务.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(Object sender, DownloadStringCompletedEventArgs e) w System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) w System.Net.WebClient.DownloadStringOperationCompleted(对象参数) 内部异常: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 的示例
请您参考如下方法:
错误的原因在于选择了哪个项目是启动对象作为你的启动项目。
当您的启动项目是 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 客户端的一部分。
