I am trying to write a service to forward data to cumulocity.
When a sensor reads data, it sends it to our server. Then the server forwards that data to my service. I parse it and use Smart REST templates to send it to cumulocity.
I looked at the guides and copied the code there for some test. For example a device creation message looks like this.
public async Task ClientTest([FromBody] JObject sensorjson)
{
var cDetails = new ConnectionDetailsBuilder()
.WithClientId("clientId")
.WithHost("mqtt.cumulocity.com")
.WithCredentials("Credentials")
.WithCleanSession(true)
.(WithProtocol(TransportType.Tcp).Build();
MqttClient client = new MqttClient(cDetails);
await client.EstablishConnectionAsync();
string topic = "s/us";
string payload = $"100,TestDevice,Test";
var message = new MqttMessageRequestBuilder()
.WithTopicName(topic)
.WithQoS(QoS.AT_LEAST_ONCE)
.WithMessageContent(payload)
.Build();
await client.PublishAsync(message);
await client.Disconnect();
}
When the code is like this, no device is created in my cumulocity tenant.
When I comment out “await client.Disconnect();” it creates the device.
What is the reason behind this?
In my real code if I don’t disconnect at some point memory usage cannot be maintained. If I disconnect my messages doesn’t go to the broker.
What should I do?