简体   繁体   English

以下内容在本地主机上进行开发,但在生产中不起作用

[英]The following works in development on a local host but in production does not

I created a class that has a timer that calls a function created in my Azure account to return the status of azure, the return value is cached. 我创建了一个具有计时器的类,该计时器调用在我的Azure帐户中创建的函数以返回azure的状态,该返回值被缓存。 In the class I have an WEB API that returns the cached value. 在该类中,我有一个返回缓存值的WEB API。

I have Windows form software that calls the WEB API once a minute to get the status. 我有Windows表单软件,该软件每分钟调用一次WEB API以获取状态。

This all works fine in development on a local host but not in production. 所有这些都可以在本地主机上进行开发,但不能在生产环境中使用。

[EnableCorsAttribute("*", "*", "*")]
public class AzureStatusController : ApiController
{
    private readonly int statusTimerInterval = 60 * 1000; // every 60 seconds

    private static bool cloudStatus = false;

    static HttpClient client;
    private static string url = "";
    private static string code = "";

    private System.Threading.Timer cloudTimer;

    /// <summary>
    /// Classes Ctor
    /// </summary>
    public AzureStatusController()
    {
        cloudTimer = new System.Threading.Timer(OnAzureStatusCallback, null, 0, statusTimerInterval);

        url = ConfigurationManager.AppSettings["CloudFunctionsBaseUrl"].ToString();
        code = ConfigurationManager.AppSettings["CloudStatusCode"].ToString();
        client = new HttpClient();
        client.BaseAddress = new Uri(url);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    }

    private void OnAzureStatusCallback(object state)
    {
        var status = false;
        if (client != null && client.BaseAddress.ToString().Length > 30 && code.Length > 70)
        {
            try
            {
                var response = client.GetAsync(code);
                if (response != null && !response.IsFaulted)
                {
                    var result = response.Result;
                    if (result != null && result.IsSuccessStatusCode && result.Content != null)
                    {
                        string responseString = result.Content.ReadAsStringAsync().Result.Trim().ToLower();
                        status = (responseString.Contains("ok"));
                    }
                }
            }
            catch
            {
                status = false;
            }
        }
        cloudStatus = status;
    }

    /// <summary>
    /// Returns the cached value of the Azure status
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    [HttpGet]
    [Route("api/AzureStatus")]
    public IHttpActionResult Get()
    {
        return Ok(cloudStatus);
    }
}

Does anyone have any ideas what I may be missing? 有人对我可能缺少的想法有什么想法吗?

I ended up finding that adding .ToString() to the end of my ConfigurationManager.AppSettings call was causing problem in production but not in development. 我最终发现,将.ToString()添加到ConfigurationManager.AppSettings调用的末尾会在生产中引起问题,但在开发中不会引起问题。 Rather than spend any more time on this problem I ended up going with the following code: 我没有花更多的时间在这个问题上,而是使用了以下代码:

public class AzureStatusController : ApiController
{
    private static bool cloudStatus = false;
    private static DateTime expiration;

    /// <summary>
    /// Classes Ctor
    /// </summary>
    public AzureStatusController()
    {
        expiration = DateTime.Now.AddHours(-1);
    }

    private void CheckAzureStatus()
    {
        var status = false;
        using (var client = new HttpClient())
        {
            // the following 2 lines should be changed to retrieve the info from 
            var url = ConfigurationManager.AppSettings["CloudFunctionsBaseUrl"];
            var code = ConfigurationManager.AppSettings["CloudStatusCode"];
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            try
            {
                var response = client.GetAsync(code);
                if (response != null && !response.IsFaulted)
                {
                    var result = response.Result;
                    if (result != null && result.IsSuccessStatusCode && result.Content != null)
                    {
                        string responseString = result.Content.ReadAsStringAsync().Result.Trim().ToLower();
                        status = (responseString.Contains("ok"));
                    }
                }
            }
            catch
            {
                status = false;
            }
        }
        cloudStatus = status;
        expiration = DateTime.Now.AddMinutes(1);
    }

    /// <summary>
    /// Returns the cached value of the Azure status
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    [HttpGet]
    [Route("api/AzureStatus")]
    public IHttpActionResult Get()
    {
        if (expiration < DateTime.Now)
        {
            CheckAzureStatus();
        }
        return Ok(cloudStatus);
    }
}

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

相关问题 Elmah在生产时给出错误代码500,但在旧主机/本地开发中却给出错误代码500 - Elmah giving error code 500 on production, but not on old host/local development ELMAH 适用于本地机器,但不适用于生产 - ELMAH works on local machine, but not on production RouteMapping在开发服务器上工作,但在生产中产生404 - RouteMapping works on development server but results in 404 in production IP和Ping类.Net-在开发中工作而不在生产中? - IP and Ping Class .Net - Works in Development not in Production? MS Access Connection在本地工作,但在生产环境中不工作 - MS Access Connection works in local but not working in production NetworkCredential可在本地计算机上工作,但不能在生产服务器上工作 - NetworkCredential works in local computer but not production server ASP.NET UDP套接字代码在开发中工作,但在IIS上不生产 - ASP.NET UDP socket code works in development, but not in production on IIS 无法连接到远程服务器(ftp 在开发中有效,但在生产中无效) - Unable to connect to the remote server (ftp works in development but not in production) 以下quicksort方法如何工作? - How does the following quicksort method works? C#代码适用于本地构建,但不适用于实时生产构建 - C# code works on local build, but not on live production build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM