Bladeren bron

新增接口访问相关

林豪 左 2 jaren geleden
bovenliggende
commit
1f95c78d0f

+ 2 - 1
ServiceCenter/ServiceCenter.csproj

@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
     <TargetFramework>net7.0</TargetFramework>
@@ -9,6 +9,7 @@
 
   <ItemGroup>
     <PackageReference Include="DBHelper" Version="1.0.0.2" />
+    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
     <PackageReference Include="PlcSiemens" Version="1.0.0.2" />
     <PackageReference Include="WCS.Core" Version="1.0.0.4" />
     <PackageReference Include="WCS.Entity" Version="1.0.0.9" />

+ 106 - 0
ServiceCenter/WebApi/APICaller.cs

@@ -0,0 +1,106 @@
+using Newtonsoft.Json;
+using System.Collections.Concurrent;
+using System.Diagnostics;
+using System.Net;
+using System.Text;
+using WCS.Core;
+
+namespace ServiceCenter.WebApi
+{
+    public class APICaller
+    {
+        private class Result
+        {
+            public bool Succsess;
+            public string Exception;
+            public object Data;
+        }
+
+        private static ConcurrentDictionary<string, Result> Results = new ConcurrentDictionary<string, Result>();
+
+        public static T CallApi2<T>(string url, object parameter, string type = "POST")
+        {
+            var content = JsonConvert.SerializeObject(parameter);
+            var result = HttpApi(url, content, type);
+            return JsonConvert.DeserializeObject<T>(result);
+        }
+
+        public static T CallApi<T>(string url, object parameter, string type = "Post")
+        {
+            var content = JsonConvert.SerializeObject(parameter);
+            var key = url + content;
+            if (Results.ContainsKey(key))
+            {
+                var res = Results[key];
+                if (res == null) throw new KnownException("接口调用中", LogLevel.Low);
+
+                try
+                {
+                    if (!res.Succsess) throw new KnownException(res.Exception, LogLevel.Mid);
+
+                    return (T)res.Data;
+                }
+                finally
+                {
+                    Results.Remove(key, out res);
+                }
+            }
+            else
+            {
+                Results[key] = null;
+                var task = Task.Run(() =>
+                 {
+                     var res = new Result();
+                     try
+                     {
+                         var result = HttpApi(url, content, type);
+                         res.Data = JsonConvert.DeserializeObject<T>(result);
+                         res.Succsess = true;
+                     }
+                     catch (Exception ex)
+                     {
+                         res.Exception = ex.Message;
+                     }
+                     finally
+                     {
+                         Results[key] = res;
+                     }
+                 });
+                task.Wait(50);
+                if (task.IsCompleted)
+                {
+                    return CallApi<T>(url, parameter, type);
+                }
+                else
+                {
+                    throw new KnownException("接口调用中", LogLevel.Low);
+                }
+            }
+        }
+
+        private static string HttpApi(string url, string jsonstr, string type)
+        {
+            var sw = new Stopwatch();
+            sw.Start();
+            Encoding encoding = Encoding.UTF8;
+            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
+            request.Timeout = 60000;//连接超时
+            request.ReadWriteTimeout = 3600000;//读写超时
+            request.Accept = "text/html,application/xhtml+xml,*/*";
+            request.ContentType = "application/json";
+            request.Method = type.ToUpper().ToString();//get或者post
+            byte[] buffer = encoding.GetBytes(jsonstr);
+            request.ContentLength = buffer.Length;
+            request.GetRequestStream().Write(buffer, 0, buffer.Length);
+            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
+            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
+            {
+                var res = reader.ReadToEnd();
+                sw.Stop();
+                if (sw.ElapsedMilliseconds > 500) Ltc.Log($"接口{url}调用耗时{sw.ElapsedMilliseconds}---------{JsonConvert.SerializeObject(res)}", LogLevel.Low, ErrorType.Kown);
+
+                return res;
+            }
+        }
+    }
+}

+ 1 - 1
WCS.WorkEngineering/WebApi/Startup.cs → ServiceCenter/WebApi/Startup.cs

@@ -4,7 +4,7 @@ using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Hosting;
 
-namespace WCS.WorkEngineering.WebApi
+namespace ServiceCenter.WebApi
 {
     public class Startup
     {

+ 1 - 1
WCS.Service/Program.cs

@@ -1,7 +1,7 @@
 using DBHelper.Redis;
 using Microsoft.AspNetCore.Hosting;
+using ServiceCenter.WebApi;
 using System.Runtime.InteropServices;
-using WCS.WorkEngineering.WebApi;
 
 namespace WCS.Service
 {

+ 0 - 1
WCS.Service/Systems/DataCollectionSysyem.cs

@@ -1,5 +1,4 @@
 using DBHelper;
-using MessagePack;
 using Newtonsoft.Json;
 using ServiceCenter.Helpers;
 using System.ComponentModel;

+ 1 - 2
WCS.WorkEngineering/WCS.WorkEngineering.csproj

@@ -7,8 +7,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
-    <PackageReference Include="ServiceCenter" Version="1.0.0.13" />
+    <PackageReference Include="ServiceCenter" Version="1.0.0.15" />
   </ItemGroup>
 
   <ItemGroup>