C# dot net core 单实例应用程序将参数传递给第一个实例

Ale*_*icu 6 c# parameter-passing single-instance .net-core

最近,我决定将我的一个用 C# 编写并针对.NET Framework 4.5 的 WPF Windows 桌面应用程序迁移到最新的.NET Core 3.1。一切都很好,直到我必须添加对单实例应用程序的支持,同时能够将任何参数从第二个实例传递到第一个运行实例。我之前针对单实例应用程序的 WPF 实现是使用System.Runtime.Remoting,这在 .NET Core 中不可用。因此我必须做一些新的事情。下面是我想出的实现。它工作得很好,但我觉得它可以改进。请随意讨论和改进建议的解决方案。

我创建了一个 SingleInstanceService,它使用信号量来指示它是否是第一个实例。如果它是第一个实例,我将创建一个 TcpListener 并无限期地等待从第二个实例传递的任何参数。如果启动了第二个实例,则我将第二个实例的参数发送到第一个侦听实例,然后退出第二个实例。

internal class SingleInstanceService
{
    internal Action<string[]> OnArgumentsReceived;

    internal bool IsFirstInstance()
    {
        if (Semaphore.TryOpenExisting(semaphoreName, out semaphore))
        {
            Task.Run(() => { SendArguments(); Environment.Exit(0); });
            return false;
        }
        else
        {
            semaphore = new Semaphore(0, 1, semaphoreName);
            Task.Run(() => ListenForArguments());
            return true;
        }
    }

    private void ListenForArguments()
    {
        TcpListener tcpListener = new TcpListener(IPAddress.Parse(localHost), localPort);
        try
        {
            tcpListener.Start();
            while (true)
            {
                TcpClient client = tcpListener.AcceptTcpClient();
                Task.Run(() => ReceivedMessage(client));
            }
        }
        catch (SocketException ex)
        {
            Log.Error(ex);
            tcpListener.Stop();
        }
    }

    private void ReceivedMessage(TcpClient tcpClient)
    {
        try
        {
            using (NetworkStream networkStream = tcpClient?.GetStream())
            {
                string data = null;
                byte[] bytes = new byte[256];
                int bytesCount;
                while ((bytesCount = networkStream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    data += Encoding.UTF8.GetString(bytes, 0, bytesCount);
                }
                OnArgumentsReceived(data.Split(' '));
            }
        }
        catch (Exception ex)
        {
            Log.Error(ex);
        }
    }

    private void SendArguments()
    {
       try
       {
            using (TcpClient tcpClient = new TcpClient(localHost, localPort))
            {
                using (NetworkStream networkStream = tcpClient.GetStream())
                {
                    byte[] data = Encoding.UTF8.GetBytes(string.Join(" ", Environment.GetCommandLineArgs()));
                    networkStream.Write(data, 0, data.Length);                       
                }
            }
        }
        catch (Exception ex)
        {
            Log.Error(ex);
        }
    }

    private Semaphore semaphore;
    private string semaphoreName = $"Global\\{Environment.MachineName}-myAppName{Assembly.GetExecutingAssembly().GetName().Version}-sid{Process.GetCurrentProcess().SessionId}";
    private string localHost = "127.0.0.1";
    private int localPort = 19191;
}
Run Code Online (Sandbox Code Playgroud)

然后在 App.xaml.cs 中,我有以下代码:

protected override void OnStartup(StartupEventArgs e)
{            
   SingleInstanceService singleInstanceService = new SingleInstanceService();

   if (singleInstanceService.IsFirstInstance())
   {
       singleInstanceService.OnArgumentsReceived += OnArgumentsReceived;      
       // Some other calls
   }
   base.OnStartup(e);
}

private void OnArgumentsReceived(string[] args)
{
    // ProcessCommandLineArgs(args);           
}
Run Code Online (Sandbox Code Playgroud)

请随意讨论这个主题,我认为这是 Windows 桌面开发人员中非常常见的用例。谢谢。

小智 -5

使用依赖注入。您需要定义一个接口,然后实现该接口。在启动代码中,您需要添加接口及其实现作为单例服务。

在您的实现构造函数中,您可以放置​​只想运行一次的代码。这将持续对象的生命周期。

还有其他类型的注入,瞬态注入和范围注入,但对于您的用例,您可能只需要单例。

程序.cs

using System;
using Microsoft.Extensions.DependencyInjection;

namespace example
{
    class Program
    {
        static void Main(string[] args)
        {
            // create service collection
            var serviceCollection = new ServiceCollection();
            ConfigureServices(serviceCollection);

            var serviceProvider = serviceCollection.BuildServiceProvider();

            serviceProvider.GetService<Startup>().Run();
        }

        private static void ConfigureServices(IServiceCollection serviceCollection)
        {
            // add services
            serviceCollection.AddTransient<IMyService, MyService>();

            serviceCollection.AddTransient<Startup>();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

启动.cs

namespace example
{
    public class Startup
    {
        private readonly IMyService _myService;
        public Startup(IMyService myService)
        {
            _myService = myService;
        }

        public void Run()
        {
            _myService.MyMethod();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

交互

namespace example
{
    public interface IMyService
    {
        void MyMethod();
    }
}
Run Code Online (Sandbox Code Playgroud)

服务实施

 namespace example
    {
        public class MyService : IMyService
        {
            public MyService()
            {
                // on signleton service this only gets invoked once
            }

            public void MyMethod()
            {
                // your imolmentation here 
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

一些在线参考: https ://dzone.com/articles/dependency-injection-in-net-core-console-applicati

https://code-maze.com/dependency-inversion-principle/