Bläddra i källkod

添加SignalR

zoule 8 månader sedan
förälder
incheckning
7c81286f80

+ 56 - 0
XdCxRhDW.App/App.config

@@ -32,4 +32,60 @@
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
   </startup>
+	<runtime>
+		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
+			<dependentAssembly>
+				<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-4.2.2.0" newVersion="4.2.2.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-4.2.2.0" newVersion="4.2.2.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-5.3.0.0" newVersion="5.3.0.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="System.Web.Http.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-5.3.0.0" newVersion="5.3.0.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-5.3.0.0" newVersion="5.3.0.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" culture="neutral" />
+				<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
+			</dependentAssembly>
+		</assemblyBinding>
+	</runtime>
 </configuration>

+ 16 - 0
XdCxRhDW.WebApi/05.XdCxRhDW.WebApi.csproj

@@ -46,6 +46,12 @@
     <Reference Include="Autofac.Integration.WebApi.Owin, Version=6.2.1.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
       <HintPath>..\packages\Autofac.WebApi2.Owin.6.2.1\lib\net472\Autofac.Integration.WebApi.Owin.dll</HintPath>
     </Reference>
+    <Reference Include="Microsoft.AspNet.SignalR.Client, Version=2.4.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+      <HintPath>..\packages\Microsoft.AspNet.SignalR.Client.2.4.3\lib\net45\Microsoft.AspNet.SignalR.Client.dll</HintPath>
+    </Reference>
+    <Reference Include="Microsoft.AspNet.SignalR.Core, Version=2.4.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+      <HintPath>..\packages\Microsoft.AspNet.SignalR.Core.2.4.3\lib\net45\Microsoft.AspNet.SignalR.Core.dll</HintPath>
+    </Reference>
     <Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
       <HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.9.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
     </Reference>
@@ -55,6 +61,9 @@
     <Reference Include="Microsoft.Owin.Cors, Version=4.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
       <HintPath>..\packages\Microsoft.Owin.Cors.4.2.2\lib\net45\Microsoft.Owin.Cors.dll</HintPath>
     </Reference>
+    <Reference Include="Microsoft.Owin.Diagnostics, Version=4.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+      <HintPath>..\packages\Microsoft.Owin.Diagnostics.4.2.2\lib\net45\Microsoft.Owin.Diagnostics.dll</HintPath>
+    </Reference>
     <Reference Include="Microsoft.Owin.FileSystems, Version=4.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
       <HintPath>..\packages\Microsoft.Owin.FileSystems.4.2.2\lib\net45\Microsoft.Owin.FileSystems.dll</HintPath>
     </Reference>
@@ -64,6 +73,9 @@
     <Reference Include="Microsoft.Owin.Hosting, Version=4.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
       <HintPath>..\packages\Microsoft.Owin.Hosting.4.2.2\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
     </Reference>
+    <Reference Include="Microsoft.Owin.Security, Version=4.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+      <HintPath>..\packages\Microsoft.Owin.Security.4.2.2\lib\net45\Microsoft.Owin.Security.dll</HintPath>
+    </Reference>
     <Reference Include="Microsoft.Owin.StaticFiles, Version=4.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
       <HintPath>..\packages\Microsoft.Owin.StaticFiles.4.2.2\lib\net45\Microsoft.Owin.StaticFiles.dll</HintPath>
     </Reference>
@@ -108,6 +120,7 @@
     <Reference Include="System.Net.Http.Formatting, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
       <HintPath>..\packages\Microsoft.AspNet.WebApi.Client.6.0.0\lib\net45\System.Net.Http.Formatting.dll</HintPath>
     </Reference>
+    <Reference Include="System.Net.Http.WebRequest" />
     <Reference Include="System.Numerics" />
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="Microsoft.CSharp" />
@@ -123,6 +136,7 @@
     <Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
       <HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.1.0\lib\net462\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
     </Reference>
+    <Reference Include="System.Security" />
     <Reference Include="System.Security.Cryptography.Algorithms, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
       <HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.1\lib\net463\System.Security.Cryptography.Algorithms.dll</HintPath>
       <Private>True</Private>
@@ -172,6 +186,8 @@
     <Compile Include="FileController.cs" />
     <Compile Include="FileDeleteDto.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="SignalR\Client.cs" />
+    <Compile Include="SignalR\Server.cs" />
     <Compile Include="Startup.cs" />
   </ItemGroup>
   <ItemGroup>

+ 71 - 0
XdCxRhDW.WebApi/SignalR/Client.cs

@@ -0,0 +1,71 @@
+using Microsoft.AspNet.SignalR.Client;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace XdCxRhDW.WebApi.SignalR
+{
+    class Program
+    {
+        //static void Main(string[] args)
+        //{
+        //    IHubProxy hub;
+        //    //服务端配置 Startup类中  app.MapSignalR();
+        //    //string url = "http://localhost:5000/signalr";
+
+        //    //服务端配置 Startup类中 app.MapSignalR("/hubs", new HubConfiguration());
+        //    string url = "http://localhost:5000/hubs";
+        //    HubConnection connection = new HubConnection(url);
+        //    connection.ConnectionSlow += Connection_ConnectionSlow;
+        //    connection.Error += Connection_Error;
+        //    connection.StateChanged += Connection_StateChanged;
+        //    ///服务端 配置 [HubName("MyTestHub")]
+        //    hub = connection.CreateHubProxy("MyTestHub");
+        //    connection.Start().Wait();
+        //    hub.On("addMessage", x =>
+        //    Console.WriteLine(x));
+        //    hub.On("SendClient", x =>
+        //    {
+        //        Console.WriteLine(x);
+        //    }
+        // );
+
+        //    string groupName = Console.ReadLine();
+        //    hub.Invoke("Sign", groupName).Wait();
+
+        //    string sendgroupName = Console.ReadLine();
+        //    hub.Invoke("SameGroupMessage", sendgroupName).Wait();
+
+        //    string line = "";
+        //    while ((line = Console.ReadLine()) != null)
+        //    {
+        //        hub.Invoke("Send", "测试", line).Wait();
+        //    }
+
+        //    Console.ReadKey();
+
+        //    //connection.Stop();
+        //    //connection.Dispose();
+        //}
+
+        private static void Connection_StateChanged(StateChange obj)
+        {
+            if (obj.NewState == ConnectionState.Disconnected)
+            {
+                Console.WriteLine("连接状态为未连接啦");
+            }
+        }
+
+        private static void Connection_Error(Exception obj)
+        {
+            Console.WriteLine("连接发生错误啦");
+        }
+
+        private static void Connection_ConnectionSlow()
+        {
+            Console.WriteLine("连接超时啦");
+        }
+    }
+}

+ 55 - 0
XdCxRhDW.WebApi/SignalR/Server.cs

@@ -0,0 +1,55 @@
+using Microsoft.AspNet.SignalR.Hubs;
+using Microsoft.AspNet.SignalR;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.Remoting.Contexts;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+
+namespace XdCxRhDW.WebApi.SignalR
+{
+    [HubName("MyTestHub")]
+    public class MyHub : Hub
+    {
+
+        public void Send(string name, string message)
+        {
+            String msg = String.Format("Message send by {0}: {1}", name, message);
+            Console.WriteLine(msg);
+            Clients.Client(GlobalModel._ids[0]).addMessage(msg);
+
+        }
+
+        public void Sign(string projectNo)
+        {
+            Groups.Add(Context.ConnectionId, projectNo);
+
+        }
+
+        public void SameGroupMessage(string name)
+        {
+            Clients.Group(name).SendClient($"咱们是同一个组{name}");
+        }
+
+        public override Task OnConnected()
+        {
+            Console.WriteLine("Client connected: " + Context.ConnectionId);
+            GlobalModel._ids.Add(Context.ConnectionId);
+
+            return base.OnConnected();
+        }
+        public override Task OnDisconnected(Boolean stopCall)
+        {
+            Console.WriteLine("Client disconnected: " + Context.ConnectionId);
+            return base.OnDisconnected(true);
+        }
+    }
+
+    public class GlobalModel
+    {
+
+        public static List<string> _ids = new List<string>();
+    }
+}

+ 10 - 2
XdCxRhDW.WebApi/Startup.cs

@@ -37,6 +37,7 @@ using System.Threading.Tasks;
 using System.Diagnostics;
 using System.Web.Http.Controllers;
 using Microsoft.Owin.Hosting;
+using Microsoft.AspNet.SignalR;
 
 [assembly: OwinStartup(typeof(XdCxRhDW.WebApi.Startup))]
 
@@ -167,10 +168,15 @@ namespace XdCxRhDW.WebApi
 
             var builder = new ContainerBuilder();
             var controllerAssemblys = AppDomain.CurrentDomain.GetAssemblies().Where(p =>
-                p.GetTypes().Any(t => t.BaseType == typeof(BaseController))).ToArray();
+            {
+                if (p.FullName.StartsWith("Microsoft")) return false;
+                return p.GetTypes().Any(t => t.BaseType == typeof(BaseController));
+            }).ToArray();
             builder.RegisterApiControllers(controllerAssemblys);
 
-            var serviceTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(p => p.GetTypes())
+            var serviceTypes = AppDomain.CurrentDomain.GetAssemblies()
+                .Where(p => !p.FullName.StartsWith("Microsoft"))
+                .SelectMany(p => p.GetTypes())
                 .Where(p => p.Namespace != null && p.Namespace.EndsWith(".Service")).ToList();
             foreach (var serviceType in serviceTypes)
             {
@@ -186,6 +192,8 @@ namespace XdCxRhDW.WebApi
             app.UseAutofacMiddleware(container);
             app.UseAutofacWebApi(config);
             app.UseCors(CorsOptions.AllowAll);
+            app.MapSignalR();
+            //app.MapSignalR("/hubs", new HubConfiguration());
             app.UseWebApi(config);
         }
 

+ 6 - 0
XdCxRhDW.WebApi/packages.config

@@ -5,6 +5,9 @@
   <package id="Autofac.WebApi2" version="6.1.1" targetFramework="net472" />
   <package id="Autofac.WebApi2.Owin" version="6.2.1" targetFramework="net472" />
   <package id="Microsoft.AspNet.Cors" version="5.3.0" targetFramework="net472" />
+  <package id="Microsoft.AspNet.SignalR.Client" version="2.4.3" targetFramework="net472" />
+  <package id="Microsoft.AspNet.SignalR.Core" version="2.4.3" targetFramework="net472" />
+  <package id="Microsoft.AspNet.SignalR.SelfHost" version="2.4.3" targetFramework="net472" />
   <package id="Microsoft.AspNet.WebApi.Client" version="6.0.0" targetFramework="net472" />
   <package id="Microsoft.AspNet.WebApi.Core" version="5.3.0" targetFramework="net472" />
   <package id="Microsoft.AspNet.WebApi.Owin" version="5.3.0" targetFramework="net472" />
@@ -13,9 +16,12 @@
   <package id="Microsoft.Bcl.AsyncInterfaces" version="9.0.0" targetFramework="net472" />
   <package id="Microsoft.Owin" version="4.2.2" targetFramework="net472" />
   <package id="Microsoft.Owin.Cors" version="4.2.2" targetFramework="net472" />
+  <package id="Microsoft.Owin.Diagnostics" version="4.2.2" targetFramework="net472" />
   <package id="Microsoft.Owin.FileSystems" version="4.2.2" targetFramework="net472" />
   <package id="Microsoft.Owin.Host.HttpListener" version="4.2.2" targetFramework="net472" />
   <package id="Microsoft.Owin.Hosting" version="4.2.2" targetFramework="net472" />
+  <package id="Microsoft.Owin.Security" version="4.2.2" targetFramework="net472" />
+  <package id="Microsoft.Owin.SelfHost" version="4.2.2" targetFramework="net472" />
   <package id="Microsoft.Owin.StaticFiles" version="4.2.2" targetFramework="net472" />
   <package id="Microsoft.Web.Infrastructure" version="2.0.0" targetFramework="net472" />
   <package id="Newtonsoft.Json" version="13.0.3" targetFramework="net472" />