我创建了一个类,该类具有一个计时器,该计时器调用在我的Azure帐户中创建的函数以返回天蓝色的状态,该返回值被缓存。在该类中,我有一个返回缓存值的WEB API。
我有Windows表单软件,该软件每分钟调用一次WEB API以获取状态。
这一切都可以在本地主机上进行开发,但不能在生产环境中使用。
[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);
}
}
有人知道我可能会缺少什么吗?
答案 0 :(得分:1)
最终我发现在我的ConfigurationManager.AppSettings调用的末尾添加.ToString()会在生产中引起问题,但在开发中不会引起问题。我没有花更多的时间在这个问题上,而是使用了以下代码:
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);
}
}