/// <summary> /// Stop listening, free resources. /// </summary> public void Stop() { if (_server != null) { var listener = _server; _server = null; GC.Collect(0, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); listener.StopListening(); } }
/// <summary> /// Start listening at 127.0.0.1:port. /// </summary> public void Start(string codeAPI) { if (_server != null) { throw new Exception("Server already initialized."); } // The client and server must agree on the interface id to use: var iid = new Guid("{1B617C4B-BF68-4B8C-AE2B-A77E6A3ECEC5}"); // Create the server instance, adjust the defaults to your needs. _server = new RpcServerApi(iid, 100, ushort.MaxValue, allowAnonTcp: false); try { // Add an endpoint so the client can connect, this is local-host only: _server.AddProtocol(RpcProtseq.ncalrpc, codeAPI, 100); // If you want to use TCP/IP uncomment the following, make sure your client authenticates or allowAnonTcp is true _server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\" + codeAPI, 25); // If you want to use TCP/IP uncomment the following, make sure your client authenticates or allowAnonTcp is true _server.AddProtocol(RpcProtseq.ncacn_ip_tcp, @"18081", 25); // Add the types of authentication we will accept _server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_GSS_NEGOTIATE); _server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); _server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE); // Subscribe the code to handle requests on this event: _server.OnExecute += _server_OnExecute; //_server.OnExecute += delegate (IRpcClientInfo client, byte[] bytes) { return new byte[0]; }; // Start Listening _server.StartListening(); } catch (Exception ex) { Console.Error.WriteLine(ex); } }