简体   繁体   English

在asp.net Web服务(asmx)中,是从代码调用时应用的CacheDuration

[英]In asp.net webservices (asmx), is the CacheDuration applied when called from code

When I set CacheDuration over a method in asmx service it gets applied correctly. 当我通过asmx服务中的方法设置CacheDuration ,它会正确应用。 Now if I called this method from another method in the same website or even from the same service, does this duration get applied too? 现在,如果我从同一网站甚至是同一服务中的另一个方法调用了此方法,那么是否也可以应用此持续时间?

When you call the method which has cache duration: 当您调用具有缓存持续时间的方法时:

  • Call from the same web site like a normal method → Calls without cache 像正常方法一样从同一个网站拨打电话→没有缓存的电话
  • Call from the same service like a normal method → Calls without cache 像正常方法一样从同一服务进行调用→没有缓存的调用
  • Call as a web service method (using service proxy) → Calls with cache 作为Web服务方法调用(使用服务代理)→带缓存的调用

The cache applies just when the call goes through ASP.NET pipeline. 缓存仅在调用通过ASP.NET管道时适用。 But if you call the method like a normal method, it doesn't use cache. 但是,如果像普通方法一样调用该方法,则它不会使用缓存。 In fact WebMethod attribute doesn't have any impact on calls when the call is a normal method call. 实际上,当调用是常规方法调用时, WebMethod属性不会对调用产生任何影响。

Example

I suppose you are looking for a simple test scenario, so you can create such web service to test: 我想您正在寻找一个简单的测试方案,因此您可以创建这样的Web服务进行测试:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
    [WebMethod(CacheDuration=10)]
    public string GetDate1()
    {
        return DateTime.Now.ToString();
    }

    [WebMethod]
    public string GetDate2()
    {
        return this.GetDate1();
    }
}

1) To test calling method from the same project, in an aspx page you can write such code in a button click and run it 3-4 times to see the result: 1)要测试来自同一项目的调用方法,在aspx页面中,您可以单击按钮编写此类代码,然后运行3-4次以查看结果:

var svc = new WebService1();
this.Label1.Text = string.Format("{0} | {1}", svc.GetDate1(), svc.GetDate2());

In all executions you will see the time without any cache and you can see no difference between calling from the same site and calling from the same service. 在所有执行中,您将看到没有任何缓存的时间,并且您看到从相同站点进行调用和从相同服务进行调用之间没有区别。

2) Also to test calling as web service method, add web reference to a windows forms project for example, in a button click write: 2)还要测试调用作为Web服务的方法,例如,将Web引用添加到Windows窗体项目中,在按钮中单击write:

var svc = new localhost.WebService1();
MessageBox.Show(string.Format("{0} | {1}", svc.GetDate1(), svc.GetDate2()));

Then you can see the svc.GetDate1() shows cached data and svc.GetDate2() shows current time while it uses svc.GetDate1() internally. 然后您可以看到svc.GetDate1()显示缓存的数据,而svc.GetDate2()显示内部使用svc.GetDate1()当前时间。 So calling as a web service uses cache, but calling from the same web service doesn't use cache. 因此,作为Web服务进行调用会使用缓存,但是从同一Web服务进行调用不会使用缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM