[英]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.