User API Requirements

We are currently working on an API for an existing system.

It basically wraps some web requests in the form of an easy-to-use library that third-party companies should be able to use with our product.

As part of the API, there is an event mechanism where the server can call the client back through a permanent socket connection.

To minimize the load on the server, we want to have only one connection to the computer. A socket is currently open for each process, and this can cause loading problems if you have multiple applications using the API.

So my question is: if we want to deploy our API as a single standalone assembly, then what is the best way to fix our problem?

A couple of options we were thinking about:

  • Write out the COM object of the process (I don’t know if this works in .Net)
  • Include the second exe file, which is required for events, it would have to have one instance on its own and open a named pipe or something to communicate through several processes.
  • Extract this exe file from the embedded resource and execute it

None of this seems perfect.

Any better ideas?

+4
source share
5 answers

The best solution we came up with is to create a Windows service that opens a named pipe to control several client processes through a single socket connection to the server.

Then our API will be able to determine if the service is started / installed and whether it fell back to create its own connection for the client otherwise.

Third parties can decide whether they want to associate the service with their product or not, but the main applications from our system will be installed.

I will mark this as an answer in a few days if anyone does not have a better option. I was hoping there was a way to complete our assembly as a new process, but all the roads for this do not seem very reliable.

0
source

Do you mean something like Net.TCP Port Exchange ?

0
source

You can fix a client-side port when opening a socket, say, 45534. Since one port can only be opened by one process, only one process at a time could open a socket connection to the server.

0
source

Well, there are many ways to solve this, as expressed in all answers and comments, but perhaps an easier way to use it is to simply store the global status in a place accessible to all users of the current computer (maybe if you can have different users registered on the computer) where you store WHO has the right to open it. Something like "blocking" as used to call. This store can be a field on the local or intranet, a simple file, or something else. This way you do not need to create or distribute additional binary files.

0
source

When a client connects to your server, you create a new thread for processing (not a process). You can save its IP address in a static dictionary (shared between all threads). Sort of:

static Dictionary<string, TcpClient> clients = new Dictionary<string, TcpClient>(); //This method is executed in a thread void ProcessRequest(TcpClient client) { string ip = null; //TODO: get client IP address lock (clients) { ... if (clients.ContainsKey(ip)) { //TODO: Deny connection return; } else { clients.Add(ip, client); } } //TODO: Answer the client } //TODO: Delete client from list on disconnection 
0
source

Source: https://habr.com/ru/post/1312124/


All Articles