1

asp.net4ashxから文字列を取得する単純なメソッドに問題があります。この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を確認してください。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) Net.WebClient.DownloadStringOperationCompleted(Object arg)InnerException:System.Net.WebExceptionメッセージ=WebClient要求中に例外が発生しました。InnerException:System.NotSupportedExceptionメッセージ=URIプレフィックスが認識されません。

どこにバグがあるのでしょうか?これはhttp://www.galasoft.ch/sl4u/code/chapter20/20.02-Mef/MefPlugins.zipの例です

4

2 に答える 2

3

エラーの理由は、どのプロジェクトがスタートアッププロジェクトとして選択されたスタートアップオブジェクトであるかにあります。

スタートアッププロジェクトがMefPluginsプロジェクト(Silverlightプロジェクト)tの場合、プロジェクト設定は、SilverlightアプリケーションをホストするためにWebページを動的に作成する必要があることを示します(プロジェクトを右クリックし、[プロパティ]を選択して、[デバッグ]タブに移動します)。

発生した問題は、ファイルのプレフィックスとして使用されている場所に関係してい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ファイルには、ポートから.ashxページにリクエストをルーティングする何らかの形式のhttpリスナーが必要です。リスナーは、IISや開発WebサーバーなどのWebサーバー、または自分で実装したリスナーにすることができます。

この問題を解決するには、MefPlugins.WebプロジェクトをスタートアッププロジェクトにMefPluginsTestPage.aspxして、スタートアップページとして設定します。これ_baseAddressで、に似たものになるはずhttp://localhost:6584/です。

このベースアドレスを使用してPluginsService.ashxページを検索すると、リソースのURIが正しくなります(http://localhost:6584/PluginsService.ashxこの場合)。

一般.ashxに、ファイルはWebサービス(IIS、デバッグWebサーバー、または独自の実装)の拡張機能であり、Silverlightクライアントの一部ではありません。

于 2011-08-11T23:02:23.240 に答える
1

問題は、渡したURIがDownloadStringAsync相対URIであるということだと思います。つまり、「file://PluginsService.ashx」は現在のディレクトリを基準にしています。「file:// C:\ projects \ test \ PluginsService.ashx」のように、絶対URI(つまり完全修飾パス名)が必要になる場合があります。

于 2011-08-11T21:16:47.943 に答える