2017-10-16 2 views
0

Ich habe durch dieses Microsoft-Dokument Caching Support for WCF Web HTTP Services gehen und ich habe es geschafft, eine Zwischenspeicherung für eine Liste von Benutzern zu erstellen. Dies ist
, wie es aussieht:WCF REST-Cache-Liste in Abhängigkeit von der ID

public List<ForJson.User> UserList() 
{ 
    bool nil; 
    WebOperationContext.Current.IncomingRequest.CheckConditionalRetrieve(this.idGenerator.GetId(this.usersEtag, out nil)); 
    var userList = this.UnitOfWork.UserRepository.GetAll() 
     .Result.Select(u => new ForJson.User 
     { 
      FirstName = u.FirstName, 
      LastName = u.LastName, 
      UserId = u.UserId, 
      CardNumber = u.CardNumber 
     }) 
     .ToList(); 
    WebOperationContext.Current.OutgoingResponse.SetETag(this.idGenerator.GetId(this.usersEtag, out nil)); 
    return userList; 
} 

Ich wollte auch dies für bestimmten Benutzer für Liste von Vermögenswerten implementieren, aber ohne Glück. Ich finde es schwierig zu verstehen, was ich falsch mache. Wenn mir jemand die Fehler zeigen könnte:

public List<ForJson.Asset> AssetList(int? userId) 
{ 
    bool nil; 
    List<ForJson.Asset> assetList; 
    if (userId != null) 
    { 
     if (!this.assetEtag.TryGetValue(userId.Value, out object eTag)) 
     { 
      eTag = new object(); 
      this.assetEtag.Add(userId.Value, eTag); 
     } 
     WebOperationContext.Current.IncomingRequest.CheckConditionalRetrieve(this.idGenerator.GetId(eTag, out nil)); 
     assetList = this.UnitOfWork.UserRepository.Get(userId.Value) 
      .Result.Assets.Select(a => new ForJson.Asset 
      { 
       AssetId = a.AssetId, 
       Barcode = a.Barcode, 
       Type = a.Type.Name, 
       UserId = userId.Value 
      }) 
      .ToList(); 
     WebOperationContext.Current.OutgoingResponse.SetETag(this.idGenerator.GetId(eTag, out nil)); 
     return assetList; 
    } 
    WebOperationContext.Current.IncomingRequest.CheckConditionalRetrieve(this.idGenerator.GetId(this.assetEtag, out nil)); 
    assetList = this.UnitOfWork.AssetRepository.Find(a => a.UserId == null) 
     .Result.Select(a => new ForJson.Asset 
     { 
      AssetId = a.AssetId, 
      Barcode = a.Barcode, 
      Type = a.Type.Name 
     }) 
     .ToList(); 
    WebOperationContext.Current.OutgoingResponse.SetETag(this.idGenerator.GetId(this.assetEtag, out nil)); 
    return assetList; 
} 

Wie funktioniert das gerade? So, dass userID bei den nächsten Methodenaufrufen nicht berücksichtigt wird. Wenn er zum ersten Aufruf für mit userId ist dann für die nächsten 5 Minuten gibt es Vermögen für diesen Benutzer (unabhängig vom Benutzer null sein oder jede andere id)

<outputCacheSettings> 
    <outputCacheProfiles> 
     <add name="CacheFor5Minutes" duration="300" varyByParam="none" sqlDependency="AssetsSystem:Users;AssetsSystem:Assets"/> 
    </outputCacheProfiles> 
    </outputCacheSettings> 

[OperationContract] 
[WebGet(ResponseFormat = WebMessageFormat.Json, 
    BodyStyle = WebMessageBodyStyle.Bare, 
    UriTemplate = "userList")] 
[AspNetCacheProfile("CacheFor5Minutes")] 
List<ForJson.User> UserList(); 

[OperationContract] 
[WebGet(ResponseFormat = WebMessageFormat.Json, 
    BodyStyle = WebMessageBodyStyle.Bare, 
    UriTemplate = "assetList?userId={userId}")] 
[AspNetCacheProfile("CacheFor5Minutes")] 
List<ForJson.Asset> AssetList(int? userId); 

Antwort

0

Die soultion userId als veryByParam in web.config

<add name="CacheFor5MinutesByUser" duration="300" varyByParam="userId" sqlDependency="AssetsSystem:Users;AssetsSystem:Assets"/> 
hinzuzufügen war
Verwandte Themen