Przeglądaj źródła

初始化 HTTP通讯

林豪 左 2 lat temu
rodzic
commit
b25255d7eb

+ 1 - 0
ServiceCenter/ServiceCenter.csproj

@@ -4,6 +4,7 @@
     <TargetFramework>net7.0</TargetFramework>
     <ImplicitUsings>enable</ImplicitUsings>
     <Nullable>enable</Nullable>
+    <GenerateDocumentationFile>True</GenerateDocumentationFile>
   </PropertyGroup>
 
   <ItemGroup>

+ 1 - 0
WCS.Entity.Protocol/WCS.Entity.Protocol.csproj

@@ -3,6 +3,7 @@
   <PropertyGroup>
     <TargetFramework>netstandard2.1</TargetFramework>
     <Nullable>enable</Nullable>
+    <GenerateDocumentationFile>True</GenerateDocumentationFile>
   </PropertyGroup>
 
   <ItemGroup>

+ 21 - 0
WCS.Service/AppSettings.cs

@@ -0,0 +1,21 @@
+namespace WCS.Service
+{
+    /// <summary>
+    /// 程序设置
+    /// </summary>
+    public class AppSettings
+    {
+        /// <summary>
+        /// 配置信息
+        /// </summary>
+        public static IConfiguration Config { get; private set; }
+
+        /// <summary>
+        /// 静态构造函数
+        /// </summary>
+        static AppSettings()
+        {
+            Config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json").Build();
+        }
+    }
+}

+ 58 - 6
WCS.Service/Program.cs

@@ -1,17 +1,69 @@
+using DBHelper.Redis;
+using Microsoft.AspNetCore.Hosting;
+using System.Runtime.InteropServices;
+using WCS.WorkEngineering.WebApi;
+
 namespace WCS.Service
 {
     public class Program
     {
         public static void Main(string[] args)
         {
-            IHost host = Host.CreateDefaultBuilder(args)
-                .ConfigureServices(services =>
+            #region 接入Redis
+
+            RedisHub.CreateContext(AppSettings.Config.GetConnectionString("Redis"), "default", true);
+
+            #endregion 接入Redis
+
+            //互斥锁检测
+            var mutexName = RedisHub.Default.Check("Mutex") ?? throw new Exception("请在Redis中配置互斥量值");
+
+            using var mt = new Mutex(true, mutexName);
+            if (mt.WaitOne())
+            {
+                CreateHostBuilder(args).Build().Run(); mt.ReleaseMutex();
+            }
+            else
+            {
+                Console.WriteLine("请勿重复运行");
+                //InfoLog.INFO_INIT("请勿重复运行");
+                Task.Delay(2000).Wait();
+            }
+        }
+
+        /// <summary>
+        /// 创建一个主机构建器
+        /// </summary>
+        /// <param name="args"></param>
+        /// <returns></returns>
+        public static IHostBuilder CreateHostBuilder(string[] args)
+        {
+            //是否是win平台
+            var isWin = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
+            Console.WriteLine($"win:{isWin}");
+            if (isWin)
+            {
+                var useUrls = RedisHub.Default.Check("UseUrls") ?? throw new Exception("请在Redis中配置网络访问端口");
+                //"http://*:8080"
+                return Host.CreateDefaultBuilder(args)
+                    .UseWindowsService()//win
+                    .ConfigureWebHostDefaults(web => //网络访问配置
+                    {
+                        web.UseUrls(useUrls); //设备访问端口
+                        web.UseStartup<Startup>(); //调用启动服务
+                    })
+                    .ConfigureServices((_, services) =>
+                    {
+                        services.AddHostedService<Worker>();
+                        services.AddHttpClient();
+                    });
+            }
+            return Host.CreateDefaultBuilder(args)
+                .UseSystemd()//linux
+                .ConfigureServices((_, services) =>
                 {
                     services.AddHostedService<Worker>();
-                })
-                .Build();
-
-            host.Run();
+                });
         }
     }
 }

+ 10 - 2
WCS.Service/WCS.Service.csproj

@@ -8,7 +8,15 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
-    <PackageReference Include="WCS.Core" Version="1.0.0.1" />
+    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
+    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
+    <PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="7.0.0" />
+    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.0" />
+    <PackageReference Include="ServiceCenter" Version="1.0.0.1" />
+    <PackageReference Include="WCS.Entity" Version="1.0.4" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\WCS.WorkEngineering\WCS.WorkEngineering.csproj" />
   </ItemGroup>
 </Project>

+ 5 - 1
WCS.Service/appsettings.json

@@ -2,7 +2,11 @@
   "Logging": {
     "LogLevel": {
       "Default": "Information",
+      "Microsoft": "Warning",
       "Microsoft.Hosting.Lifetime": "Information"
     }
+  },
+  "ConnectionStrings": {
+    "Redis": "127.0.0.1,database=0,prefix=Aging:"
   }
-}
+}

+ 0 - 7
WCS.WorkEngineering/Class1.cs

@@ -1,7 +0,0 @@
-namespace WCS.WorkEngineering
-{
-    public class Class1
-    {
-
-    }
-}

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

@@ -7,7 +7,8 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="WCS.Core" Version="1.0.0.1" />
+    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
+    <PackageReference Include="ServiceCenter" Version="1.0.0.1" />
   </ItemGroup>
 
 </Project>

+ 50 - 0
WCS.WorkEngineering/WebApi/Startup.cs

@@ -0,0 +1,50 @@
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+
+namespace WCS.WorkEngineering.WebApi
+{
+    public class Startup
+    {
+        public Startup(IConfiguration configuration)
+        {
+            Configuration = configuration;
+        }
+
+        public IConfiguration Configuration { get; }
+
+        // This method gets called by the runtime. Use this method to add services to the container.
+        public void ConfigureServices(IServiceCollection services)
+        {
+            services.AddControllers();
+            //services.AddSwaggerGen(c =>
+            //{
+            //    c.SwaggerDoc("v1", new OpenApiInfo { Title = "WCSAPI", Version = "v1" });
+            //});
+        }
+
+        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
+        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
+        {
+            if (env.IsDevelopment())
+            {
+                app.UseDeveloperExceptionPage();
+                //app.UseSwagger();
+                //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication1 v1"));
+            }
+
+            app.UseHttpsRedirection();
+
+            app.UseRouting();
+
+            app.UseAuthorization();
+
+            app.UseEndpoints(endpoints =>
+            {
+                endpoints.MapControllers();
+            });
+        }
+    }
+}