WCF JSONP条件缓存

时间:2013-02-13 21:26:30

标签: wcf http http-caching

使用跨域WCF服务,我似乎无法通过JSONP响应获得条件缓存。

当请求的If-None-Match值与Etag匹配时,我期望响应状态代码为304 Not Modified&一个空的反应机构。相反,我得到的响应状态代码为200 OK,并且正文中包含垃圾JSONP字符串。

的web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
      <authentication mode="None" />
    <caching>
      <outputCache enableOutputCache="true"/>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="ResourceSet" location="Server" duration="30" varyByParam="skip; take" varyByHeader="Accept"/>
          <add name="Resource" location="Any" duration="30" varyByParam="" varyByHeader="Accept"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>

  <system.serviceModel>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint
          automaticFormatSelectionEnabled="true"
          helpEnabled="true" crossDomainScriptAccessEnabled="true" />
      </webHttpEndpoint>
        <webScriptEndpoint>
            <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint>
        </webScriptEndpoint>
    </standardEndpoints>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
</configuration>

服务方式:

    [WebGet(UriTemplate = "/{key}")]
    public Resource GetResource(string key)
    {
        ValidateArguments(key);

        Resource resource = resourceRepository.Get(ParseResourceKey(key));

        if (resource == null)
            throw new WebFaultException(HttpStatusCode.NotFound);

        WebFaultException(HttpStatusCode.NotModified);
        Request.CheckConditionalRetrieve(resource.Version);

        // If the caller did not provide an ETag (or it was not a match)
        // Set the ETag on the response message
        Response.SetETag(resource.Version);

        return resource;
    }

请求

    GET http://localhost:1128/CannonicalRESTService.svc/1?callback=jsonpCallback HTTP/1.1
    User-Agent: Fiddler
    Host: localhost:1128
    Accept: application/json
    If-None-Match: "9bb40c00-5de9-463e-8b7d-1105c36318c8"

预期回应

    HTTP/1.1 304 Not Modified
    Server: ASP.NET Development Server/10.0.0.0
    Date: Wed, 13 Feb 2013 21:20:05 GMT
    X-AspNet-Version: 4.0.30319
    Content-Length: 15
    ETag: "9bb40c00-5de9-463e-8b7d-1105c36318c8"
    Cache-Control: private
    Content-Type: application/x-javascript
    Connection: Close

实际回复

    HTTP/1.1 200 OK
    Server: ASP.NET Development Server/10.0.0.0
    Date: Wed, 13 Feb 2013 21:20:05 GMT
    X-AspNet-Version: 4.0.30319
    Content-Length: 15
    ETag: "9bb40c00-5de9-463e-8b7d-1105c36318c8"
    Cache-Control: private
    Content-Type: application/x-javascript
    Connection: Close

    jsonpCallback(null,304);

代码来自http://code.msdn.microsoft.com/cannonicalRESTEntity。我所做的只是将crossDomainScriptAccessEnabled =“true”添加到web.config。

感谢您的时间。

0 个答案:

没有答案