Reputation: 325
I have created a very basic WCF service application with the following code in the .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 the 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 has been decorated with the AspNetCacheProfile and the cacheProfileName refers to the "ChacheFor60Seconds" section of the web.config.
I run the following sequence in the WCF Test Client:
1) Call Save with the parameter "Fred"
2) Call GetAll -> "Fred" is returned, as expected.
3) Call Save with the parameter "Bob"
4) Call GetAll -> this time "Fred" and "Bob" are returned.
I was expecting only "Fred" to be returned on the second call to GetAll, because it should be returning the cached result from step (2).
I can't figure out what the problem is, so would be grateful for some help please.
Upvotes: 4
Views: 1621
Reputation: 5766
Your web.config
configuration file has a double <system.web>
section, try merging them.
Upvotes: 0
Reputation: 3179
You are trying to cache the complete result without any parameter hence your setting 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;
}
Upvotes: 1