我有bool
通过测试快速查询来检查本地Web服务是否可用,如果失败则运行StartOSRMService()
。
配置:
Config config = new Config
{
localhostNearest = "C:/path/...",
localhostRoute = "C:/path/...",
processLocation = @"C:/path/...",
WorkingDirectory = @"C:/path/...",
DataSource = "file-latest.osrm"
};
StartOSRMService()
private void StartOSRMService()
{
var startInfo = new ProcessStartInfo(config.processLocation);
startInfo.WorkingDirectory = config.WorkingDirectory;
startInfo.UseShellExecute = false;
startInfo.Arguments = config.DataSource;
Process.Start(startInfo);
}
服务现有():
public bool ServiceAvailable()
{
string lon = "-0.00000000";
string lat = "51.0000000000";
try
{
WebRequest request = WebRequest.Create($"{config.localhostNearest}{lon},{lat}");
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Routing point = new JavaScriptSerializer().Deserialize<Routing>(responseFromServer);
if (point.code == "Ok")
{
return true;
}
StartOSRMService();
return false; // Needed but never executed?
}
catch (Exception)
{
StartOSRMService();
// TODO receive routes?
return false;
}
}
运行StartOSRMService()
时,我得到此输出:
[info] starting up engines, v5.3.0[0m
[info] Threads: 8[0m
[info] IP address: 0.0.0.0[0m
从此时起,将[0m
附加到从控制台出来的所有。这意味着如果我然后通过request()
发送字符串,则该字符串附加[0m
,并且无法返回格式正确的response()
。
如果我在Visual Studio中运行应用程序之前从OSRM
运行CMD
,我就不会遇到此问题。
有人可以告诉我[0m
可能出现的原因以及如何摆脱它吗?
答案 0 :(得分:1)
解决方法是Console.ResetColor();"