So I think I’ve got it working.
I think the basic issue is that the Newtonsoft JSON framework just takes up too much memory. When I moved to System.Text.Json it was interesting to watch how many dll’s were uninstalled from the Meadow
2>[26/01/2021 20:39:02] Meadow successfully deleted 'Newtonsoft.Json.dll' (00:00:00.1269271 since last.)
2>[26/01/2021 20:39:12] Meadow successfully deleted 'System.Xml.Linq.dll' (00:00:09.9979770 since last.)
2>[26/01/2021 20:39:22] Meadow successfully deleted 'System.Runtime.Serialization.dll' (00:00:10.0623086 since last.)
2>[26/01/2021 20:39:32] Meadow successfully deleted 'System.ServiceModel.Internals.dll' (00:00:10.0000605 since last.)
2>[26/01/2021 20:39:42] Meadow successfully deleted 'System.Data.dll' (00:00:09.9684865 since last.)
2>[26/01/2021 20:39:52] Meadow successfully deleted 'System.Transactions.dll' (00:00:09.9995034 since last.)
2>[26/01/2021 20:40:02] Meadow successfully deleted 'System.EnterpriseServices.dll' (00:00:10.0004231 since last.)
Try installing the System.Text.Json package from nuget.
In the header of the class you want to replace
using Newtonsoft.Json;
with
using System.Text.Json;
then I have this code working
public async Task<bool> SendNotification()
{
Console.WriteLine("Start Notification: " );
try
{
string uri = "http://192.168.1.11:8002/api/MeadowLogs";
Console.WriteLine("Build object: ");
var data = new
{
LogData = "Meadow"
};
Console.WriteLine("Serialize Data: ");
string httpContent = JsonSerializer.Serialize(data);
Console.WriteLine(httpContent);
Console.WriteLine("Build httpcontent: ");
var stringContent = new StringContent(httpContent);
Console.WriteLine("Create http client: ");
var client = new HttpClient();
Console.WriteLine("Adding Headers: ");
stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
Console.WriteLine("Sending Message: ");
var response = await client.PostAsync(uri, stringContent).ConfigureAwait(false);
var result = response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
return true;
}
else
{
Console.WriteLine(response.ReasonPhrase);
return false;
}
}
catch (TaskCanceledException ex)
{
Console.WriteLine(ex.ToString());
return false;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
Lots of unnecessary Console.Writelogs in there which you can remove. The other issue I had is that most internet enabled REST API’s are https URLs. As the Meadow doesn’t support TLS yet, I had to write a local API server to test it with. But this runs now, can connect to the http server and gets a response back. Hopefully this helps