zoukankan      html  css  js  c++  java
  • zyrpc service

    // Compile: csc service.cs /r:Zyan.Communication.dll /r:Topshelf.dll

    //

    // Usage:

    //    service.exe            Run the console server

    //    service.exe install    Install the service (requires admin rights)

    //    service.exe start      Start the installed service (admin)

    //    service.exe stop       Stops the service (admin)

    //    service.exe uninstall  Uninstall the service (admin)

    //    service.exe client     Run the console client

    using System;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Topshelf;

    using Zyan.Communication;

    using Zyan.Communication.Protocols.Tcp;

    struct Program

    {

    const int TcpPortNumber = 8765;

    const string ZyanHostName = "SampleService";

    static void Main(string[] args)

    {

    if (args.Any(s => s == "client"))

    {

    RunClient().Wait();

    return;

    }

    RunServer();

    }

    // ------------------------------- Shared code --------

    public interface ISampleService

    {

    string GeneratePassword(int length);

    }

    // ------------------------------- Client code --------

    static async Task RunClient()

    {

    var protocol = new TcpDuplexClientProtocolSetup();

    var url = protocol.FormatUrl("localhost", TcpPortNumber, ZyanHostName);

    using (var conn = await Task.Run(() => new ZyanConnection(url, protocol)))

    {

    Console.WriteLine("Connected to server.");

    var proxy = conn.CreateProxy<ISampleService>();

    while (true)

    {

    Console.Write("Enter password length (empty line to quit): ");

    var line = Console.ReadLine().Trim(' ', ' ', ' ', '.', '-');

    if (string.IsNullOrEmpty(line) || line.Any(c => !char.IsNumber(c)))

    {

    Console.WriteLine("Bye.");

    break;

    }

    var length = Convert.ToInt32(line);

    var password = await Task.Run(() => proxy.GeneratePassword(length));

    Console.WriteLine("Server generated password: {0}", password);

    }

    }

    }

    // ------------------------------- Server code --------

    static void RunServer()

    {

    HostFactory.Run(x =>

    {

    //x.UseNLog(); // configure logging as needed

    x.Service<Dispatcher>(s =>

    {

    s.ConstructUsing(name => new Dispatcher());

    s.WhenStarted(d => d.Start());

    s.WhenStopped(d => d.Stop());

    });

    // these options can be overridden via the command line:

    // service.exe install --localservice -servicename:Cida -description:Demo -displayname:CidaDemo

    x.RunAsNetworkService();

    x.SetDescription("Configuration Item Discovery Agent");

    x.SetDisplayName("CidaService");

    x.SetServiceName("CidaService");

    x.EnableServiceRecovery(rc => rc.RestartService(1)); // restart after one minute

    });

    Console.WriteLine("Topshelf application is finished. Exiting...");

    }

    internal class Dispatcher

    {

    private ZyanComponentHost Host { get; set; }

    public void Start()

    {

    var protocol = new TcpDuplexServerProtocolSetup(TcpPortNumber);

    Host = new ZyanComponentHost(ZyanHostName, protocol);

    Host.RegisterComponent<ISampleService, SampleService>();

    Console.WriteLine("Server is up and running. Try running clients.");

    }

    public void Stop()

    {

    if (Host != null)

    {

    Host.Dispose();

    Host = null;

    Console.WriteLine("Server is stopped.");

    }

    }

    }

    internal class SampleService : ISampleService

    {

    public string GeneratePassword(int length)

    {

    var sb = new StringBuilder();

    var rnd = new Random();

    for (var i = 0; i < length; i++)

    {

    var @char = (char)('a' + rnd.Next('z' - 'a'));

    sb.Append(@char);

    }

    Console.WriteLine("Generated password: {0}, length = {1}",

    sb, length);

    return sb.ToString();

    }

    }

    }

    c:>service.exe install

    Configuration Result:

    [Success] Name CidaService

    [Success] Description Configuration Item Discovery Agent

    [Success] ServiceName CidaService

    Topshelf v3.2.150.0, .NET Framework v4.0.30319.34209

    Выполняется групповая операция установки.

    Начинается этап установки процедуры установки.

    Installing CidaService service

    Устанавливается служба CidaService...

    Служба CidaService успешно установлена.

    Этап установки успешно выполнен, начинается этап фиксации.

    Этап фиксации выполнен успешно.

    Групповая операция установки выполнена.

    Topshelf application is finished. Exiting...

    c:>service.exe start

    Configuration Result:

    [Success] Name CidaService

    [Success] Description Configuration Item Discovery Agent

    [Success] ServiceName CidaService

    Topshelf v3.2.150.0, .NET Framework v4.0.30319.34209

    The CidaService service was started.

    Topshelf application is finished. Exiting...

    c:service.exe client

    Connected to server.

    Enter password length (empty line to quit): 10

    Server generated password: xsshpcpdga

    Enter password length (empty line to quit): 11

    Server generated password: goarqvoqich

    Enter password length (empty line to quit): 123

    Server generated password: lmiwctrtsoasxgcmntvdfebclbfoceaiwuaqeosnnrlytplbotlt

    gujgkgkaocqonqudfkixaipnschvpdaisgcbltgxerykbwkcthvdlmsxfvnixokkcivhotr

    Enter password length (empty line to quit):

    Bye.

    c:>service.exe stop

    Configuration Result:

    [Success] Name CidaService

    [Success] Description Configuration Item Discovery Agent

    [Success] ServiceName CidaService

    Topshelf v3.2.150.0, .NET Framework v4.0.30319.34209

    The CidaService service was stopped.

    Topshelf application is finished. Exiting...

    c:>service.exe uninstall

    Configuration Result:

    [Success] Name CidaService

    [Success] Description Configuration Item Discovery Agent

    [Success] ServiceName CidaService

    Topshelf v3.2.150.0, .NET Framework v4.0.30319.34209

  • 相关阅读:
    [python]如何理解uiautomator里面的 instance 及使用场景
    [python]如何理解uiautomator里面的 right,left,up,down 及使用场景
    [python] try......except.....finally
    [python]python官方原版编码规范路径
    [python]pip 版本9.0.1升级到10.0.1故障解决办法
    [python]如何理解uiautomator里面的 child, child_by_text, sibling,及使用场景
    [python]关于在python中模块导入问题追加总结
    [Eclipse]如何往eclipse中导入单个python文件,其它类型代码文件也可以参照该方法
    [Eclipse]在重命令文件名时,提示编码格式有问题导致修改失败,需要设置如下几个默认编码为UTF-8
    [MySQL]在安装windows版MySQL时遇到过如下问题Error Nr.1045和Error.Nr.2003,相应解决办法如下
  • 原文地址:https://www.cnblogs.com/xiajing12345/p/6767415.html
Copyright © 2011-2022 走看看