在C#中如何清除REST服务响应的缓存?

时间:2015-04-14 15:27:47

标签: c# rest model-view-controller

我有一个调用Web服务的MVC应用程序来获取Web服务的状态。但我发现我的应用服务器将响应存储在缓存中,因此未来的请求不会更新Web服务状态?如何强制它始终更新状态?

[DataContract]
public class AppStatus
{
    [DataMember]
    public string ServiceAddr;
    [DataMember]
    public string AppName;
    [DataMember]
    public string Database;
    [DataMember]
    public TimeSpan RunningTime;
    [DataMember]
    public string VersionInfo;
    [DataMember]
    public string TotalMemory;
    [DataMember]
    public double ResponseTime;
}

public AppStatus Status()
{
    DateTime start = DateTime.Now;
    Assembly assembly = Assembly.GetExecutingAssembly();
    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
    var strDBStatus = GetDBStatus();
    int count = 0;
    bool numConverted = Int32.TryParse(strDBStatus, out count);


    AppStatus status = new AppStatus() 
    { 
        ServiceAddr = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString,
        VersionInfo = fvi.FileVersion,
        AppName = fvi.ProductName,
        TotalMemory = FormatBytes(GC.GetTotalMemory(false)),
        RunningTime = DateTime.Now - Process.GetCurrentProcess().StartTime,
        Database = numConverted && count > 0? "OK" : strDBStatus
    };
    status.ResponseTime = (DateTime.Now - start).TotalMilliseconds;
    return status;
}

以下是我目前正在拨打该服务的方式:

public class StatusController : Controller
{
    // GET: Status
    public ActionResult Index()
    {
        DateTime start = DateTime.Now;
        string srvAddr =  ConfigurationManager.AppSettings["ServiceAddr"]; 
        try
        {
            var chan = WSCom.GetClientChannel();
            var srvStatus = chan.Status();
            ViewBag.SrvStatus = srvStatus;
        }
        catch (Exception e)
        {
            ExceptionLogger.LogException(e);

            var srvStatus = new AppStatus(){
                ServiceAddr =   srvAddr,
                AppName = "Service Error"
            };
            ViewBag.SrvStatus = srvStatus;
        }

        Assembly assembly = Assembly.GetExecutingAssembly();
        FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
        AppStatus _appStatus = new AppStatus()
        {
            ServiceAddr = HttpContext.Request.RawUrl,
            VersionInfo = fvi.FileVersion,
            AppName = fvi.ProductName,
            TotalMemory = FormatBytes(GC.GetTotalMemory(false)),
            RunningTime = DateTime.Now - Process.GetCurrentProcess().StartTime
        };
        _appStatus.ResponseTime = (DateTime.Now - start).TotalMilliseconds;
        ViewBag.AppStatus = _appStatus;
        return View();
    }

    private string FormatBytes(long bytes)
    {
        string[] sizes = { "B", "KB", "MB", "GB" };

        int order = 0;
        while (bytes >= 1024 && order + 1 < sizes.Length)
        {
            order++;
            bytes = bytes / 1024;
        }

        // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
        // show a single decimal place, and no space.
        string result = String.Format("{0:0.##} {1}", bytes, sizes[order]);
        return result;
    }
}

    // Get client channel
    public static IService GetClientChannel()
    {
        var srvAddr = ConfigurationManager.AppSettings["ServiceAddr"].ToString();

        if (string.IsNullOrEmpty(srvAddr))
            throw new NullReferenceException();

        Uri uri = new Uri(srvAddr);
        WebChannelFactory<IService> cf =
            new WebChannelFactory<IService>(uri);

        IService tc = cf.CreateChannel();

        if (tc == null)
            Trace.TraceError("Unable to connect service address ( " + srvAddr + ")");

        return tc;
    }

0 个答案:

没有答案