4

.svc ファイルに次のコードを含む非常に基本的な WCF サービス アプリケーションを作成しました。

using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

namespace NamesService
{
    [ServiceContract]
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class NamesService
    {
        List<string> Names = new List<string>();

        [OperationContract]
        [AspNetCacheProfile("CacheFor60Seconds")]
        [WebGet(UriTemplate="")]
        public List<string> GetAll()
        {
            return Names;
        }

        [OperationContract]
        public void Save(string name)
        {
            Names.Add(name);
        }
    }
 }

そして、web.config は次のようになります。

<?xml version="1.0"?>
<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.web>
    <caching>
        <outputCache enableOutputCache="true"/>
        <outputCacheSettings>
            <outputCacheProfiles>
                <add name="CacheFor60Seconds" location="Server" duration="60" varyByParam="" />
            </outputCacheProfiles>
        </outputCacheSettings>
    </caching>
</system.web>

ご覧のとおり、GetAll メソッドは AspNetCacheProfile で修飾されており、cacheProfileName は web.config の「ChacheFor60Seconds」セクションを参照しています。

WCF テスト クライアントで次のシーケンスを実行します。

1) パラメータ "Fred" を指定して Save を呼び出します

2) GetAll を呼び出します -> 予想どおり、"Fred" が返されます。

3) パラメータ "Bob" を指定して Save を呼び出す

4) GetAll を呼び出す -> 今度は "Fred" と "Bob" が返されます。

手順 (2) からキャッシュされた結果を返す必要があるため、GetAll の 2 回目の呼び出しで "Fred" のみが返されることを期待していました。

何が問題なのかわからないので、助けていただければ幸いです。

4

2 に答える 2

1

パラメータなしで完全な結果をキャッシュしようとしているため、設定は

 <outputCacheSettings>
    <outputCacheProfiles>
        <add name="CacheFor60Seconds" location="Server" duration="60"
        varyByParam="none" />
    </outputCacheProfiles>
 </outputCacheSettings> 

編集:

[OperationContract]
[AspNetCacheProfile("CacheFor60Seconds")]
[WebGet]
public List<string> GetAll()
{
    return Names;
}
于 2012-06-03T22:15:02.877 に答える
0

web.config構成ファイルに 2 つの<system.web>セクションがあります。それらをマージしてみてください。

于 2014-04-29T12:24:48.847 に答える