'Communication betwen local programms in C#
I'm surching for a way to communicate betwen two local c# Programms in an effective and very fast way. The best would be if it is possible to trigger a method of the "main" program with the second one and recive the result like a getter and setter. is there a something i that dosnt need 2 install a local server or something like that?
if more details needed. I am writing a little Programm for some Audio management and have a Device that has some dials and and an SDK for Plugins. The plan is to change some settings in the Programm if i have the Device connected to my PC and show some of the results on it. And the reason for not dooing all of it as a Plugin is that the Programm should be able to run on its own.
i would be glad for some simple exaples to start with.
Solution 1:[1]
I've successfully used Unosquare's Embed.IO (no affiliation) before now to make a program host a small web server. You can see examples of setup on their home page
You might set up some AudioController class:
class AudioController: WebApiController{
[Route(HttpVerbs.Get, "/api/samplerate/{rate}")]
public object SetSampleRate(int rate)
{
//set the rate?
Properties.Settings.Default.SamplingRate = rate;
return true;
}
}
And then in the client you might poke that endpoint:
var wc = new WebClient();
wc.DownloadString("http://localhost:1234/api/samplerate/44100");
Perhaps you want a more complex request object:
class RecorderSettings{
public int SampleRate{get;set;}
public int Bits{get;set;}
}
class AudioController: WebApiController{
[Route(HttpVerbs.Post, "/api/setMulti")]
public async Task<object> SetMultiple()
{
var x = await HttpContext.GetRequestDataAsync<RecorderSettings>();
Properties.Settings.Default.SamplingRate = x.SampleRate;
Properties.Settings.Default.Bitness = x.Bits;
return ...;
}
}
And some client poking it with json:
//client is some static HttpClient
HttpResponseMessage response = await client.PostAsync(
"http://localhost:1234/api/setMulti",
new StringContent(
JsonConvert.Serialize(new { SampleRate = 44100, Bits = 16}),
Encoding.UTF8,
MediaTypeNames.Application.Json
)
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
