Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 缓存web服务结果_C#_Asp.net_Web Services_Caching_Iis - Fatal编程技术网

C# 缓存web服务结果

C# 缓存web服务结果,c#,asp.net,web-services,caching,iis,C#,Asp.net,Web Services,Caching,Iis,我有一个3-5分钟长时间运行的web服务,它返回一个大的自定义对象 我像这样声明web方法 public class MyService : System.Web.Services.WebService { [WebMethod(CacheDuration=86400)] [XmlInclude(typeof(MyObject))] public MyObject Items(string myParam) { return new MyObjec

我有一个3-5分钟长时间运行的web服务,它返回一个大的自定义对象

我像这样声明web方法

public class MyService : System.Web.Services.WebService
{
    [WebMethod(CacheDuration=86400)]
    [XmlInclude(typeof(MyObject))]
    public MyObject Items(string myParam)
    {
        return new MyObject(myParam);            
    }
}
protected MyService.MyObject servObj = new MyService.MyObject();
protected void Page_Load(object sender, EventArgs e) 
{  
    if (!Page.IsPostBack) 
    {
        if (Cache["elems1"] == null) 
        {
            MyObject items = servObj.Items("myParam");
            Cache.Insert("elems1", servObj.elems1, null, DateTime.MaxValue, TimeSpan.FromHours(24));
            Cache.Insert("elems2", servObj.elems2, null, DateTime.MaxValue, TimeSpan.FromHours(24));
        }
    }
}
然后,我在一个经典的ASP.NET 4.0应用程序中使用该服务,并缓存如下项目的元素

public class MyService : System.Web.Services.WebService
{
    [WebMethod(CacheDuration=86400)]
    [XmlInclude(typeof(MyObject))]
    public MyObject Items(string myParam)
    {
        return new MyObject(myParam);            
    }
}
protected MyService.MyObject servObj = new MyService.MyObject();
protected void Page_Load(object sender, EventArgs e) 
{  
    if (!Page.IsPostBack) 
    {
        if (Cache["elems1"] == null) 
        {
            MyObject items = servObj.Items("myParam");
            Cache.Insert("elems1", servObj.elems1, null, DateTime.MaxValue, TimeSpan.FromHours(24));
            Cache.Insert("elems2", servObj.elems2, null, DateTime.MaxValue, TimeSpan.FromHours(24));
        }
    }
}
当我运行应用程序时,web服务缓存似乎有时可用,有时不可用,即它没有持续指定的时间。web服务在IIS上自己的应用程序池中运行


你知道为什么吗?有没有更智能的方法来缓存web服务结果?

结果是缓存。插入。。。在Page类中。它必须是Context.Cache.Insert才能位于应用程序缓存中。啊

只有在没有回发时才填充缓存。您确定在将项目实际放入缓存之前,没有尝试引用缓存中的项目吗?如果您的Web服务抛出错误怎么办?不会将任何内容放入缓存。默认情况下,IIS会在20分钟不活动后关闭应用程序。如果您没有更改该值,它将比您在此处指定的持续时间短得多。我在其他地方处理过web服务失败错误。IIS配置为在20小时时使应用程序超时,但在大约20分钟后似乎会超时。20小时不活动还是每20小时循环一次?有两种不同的设置:。只是再检查一遍,确保这是我要检查的第一件事。