Failed to get AspNetCacheProfile to work in WCF 4.0 service

I created a very basic WCF service application with the following code in a .svc file:

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); } } } 

And web.config looks like this:

 <?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> 

As you can see, the GetAll method was decorated with AspNetCacheProfile, and cacheProfileName refers to the ChacheFor60Seconds section in the web.config file.

I run the following sequence in the WCF test client:

1) Call Save with the parameter "Fred"

2) GetAll is called β†’ "Fred", as expected.

3) Call Save with the parameter "Bob"

4) Call GetAll β†’ this time "Fred" and "Bob" return.

I expected that in the second call to GetAll only "Fred" would be returned, because it should return the cached result from step (2).

I can’t understand what the problem is, so it would be grateful for the help.

+4
source share
2 answers

You are trying to cache the full result without any parameters, so your setup should be

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

EDIT:

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

There is a double <system.web> section in the web.config configuration file, try merging them.

0
source

Source: https://habr.com/ru/post/1415853/


All Articles