in docker_images/csharp/wrapper/src/Glue/ModuleGlue.cs [249:305]
public async Task<object> RoundtripMethodCallAsync(string connectionId, string methodName, MethodRequestAndResponse requestAndResponse)
{
Console.WriteLine("RoundtripMethodCallAsync received for {0} and methodName {1}", connectionId, methodName);
Console.WriteLine(JsonConvert.SerializeObject(requestAndResponse));
var client = objectMap[connectionId];
var mutex = new System.Threading.SemaphoreSlim(1);
await mutex.WaitAsync().ConfigureAwait(false); // Grab the mutex. The handler will release it later
MethodCallback callback = async (methodRequest, userContext) =>
{
Console.WriteLine("Method invocation received");
object request = JsonConvert.DeserializeObject(methodRequest.DataAsJson);
string received = JsonConvert.SerializeObject(new JRaw(request));
string expected = ((Newtonsoft.Json.Linq.JToken)requestAndResponse.RequestPayload)["payload"].ToString();
Console.WriteLine("request expected: " + expected);
Console.WriteLine("request received: " + received);
if (expected != received)
{
Console.WriteLine("request did not match expectations");
Console.WriteLine("Releasing the method mutex");
mutex.Release();
return new MethodResponse(500);
}
else
{
int status = 200;
if (requestAndResponse.StatusCode != null)
{
status = (int)requestAndResponse.StatusCode;
}
byte[] responseBytes = GlueUtils.ObjectToBytes(requestAndResponse.ResponsePayload);
Console.WriteLine("Releasing the method mutex");
mutex.Release();
Console.WriteLine("Returning the result");
return new MethodResponse(responseBytes, status);
}
};
Console.WriteLine("Setting the handler");
await client.SetMethodHandlerAsync(methodName, callback, null).ConfigureAwait(false);
Console.WriteLine("Waiting on the method mutex");
await mutex.WaitAsync().ConfigureAwait(false);
Console.WriteLine("Method mutex released. Waiting for a tiny bit."); // Otherwise, the connection might close before the response is actually sent
await Task.Delay(100).ConfigureAwait(false);
Console.WriteLine("Nulling the handler");
await client.SetMethodHandlerAsync(methodName, null, null).ConfigureAwait(false);
Console.WriteLine("RoundtripMethodCallAsync is complete");
return new object();
}