大赵传奇

援引事类,扬搉古今,举要删芜,言辩而理切--QQ276605216

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

controller中异步执行长时间操作:

 1         [HttpGet]
 2         [Route("/")]
 3         public BusinessResult GetxxxxJsonFile() 
 4         {
 5             try
 6             {
 7                 var ret = "";
 8                 //先获取保存在当前Tileset.json文件中的路径
 9                 var file = @"TilesetJsonFilePath.txt";
10                 if (System.IO.File.Exists(file))
11                 {
12                     using (StreamReader tw = new StreamReader(file))
13                     {
14                         var line = tw.ReadLine();
15                         if (!string.IsNullOrEmpty(line))
16                         {
17                             ret = line;
18                         }
19                     }
20                 }
21 
22                 //不行再获取默认路径
23                 if (string.IsNullOrEmpty(ret))
24                 {
25                     var ip = httpContextAccessor.HttpContext.Connection.LocalIpAddress.ToString();
26                     ip = (ip == "::1") ? "localhost" : ip;
27                     var port = httpContextAccessor.HttpContext.Connection.LocalPort.ToString();
28                     ret = $"http://{ip}:{port}/outpath/tileset.json";
29                 }
30 
31                 //var addressList = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList;
32                 //var ips = addressList.Where(address => address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
33                 //        .Select(address => address.ToString()).ToArray();
34                 //if (ips.Length == 1)
35                 //{
36                 //    ip = ips.First();
37                 //}
38                 //ip = ips.Where(address => !address.EndsWith(".1")).FirstOrDefault() ?? ips.FirstOrDefault();
39 
40 
41                 //如果未初始及挂载,这里需要太多时间
42                 //Quadtree4TilesetCreator creator = GetTilesetCreator();
43                 //string p = creator.GetTilesetJsonPath();
44                 //return new BusinessResult
45                 //{
46                 //    IsSuccess = true,
47                 //    CustomerObject = p
48                 //};
49                 
50                 //换这个:
51                 //异步确认一下挂载情况
52                 Action excAction = () =>
53                 {
54                     Quadtree4TilesetCreator creator = GetTilesetCreator();
55                     string p = creator.GetTilesetJsonPath();
56                 };
57                 //excAction.BeginInvoke((a) =>  //System.PlatformNotSupportedException: Operation is not supported on this platform
58                 //{
59                 //    // 回调
60                 //}, "");                
61                 var workTask = Task.Run(() => excAction.Invoke());
62 
63                 //最后返回路径
64                 return new BusinessResult
65                 {
66                     IsSuccess = true,
67                     CustomerObject = ret
68                 };
69             }
70             catch (Exception ex)
71             {
72                 return new BusinessResult
73                 {
74                     IsSuccess = false,
75                     Message = $"获取路径失败错误,{ex.Message}"
76                 };
77             }
78         }

 

posted on 2024-08-21 14:55  赵长青  阅读(1)  评论(0编辑  收藏  举报