Skip to content

Instantly share code, notes, and snippets.

@rido-min
Created March 11, 2021 06:03
Show Gist options
  • Save rido-min/09e64245174aceb9296431cce58b1cf4 to your computer and use it in GitHub Desktop.
Save rido-min/09e64245174aceb9296431cce58b1cf4 to your computer and use it in GitHub Desktop.
C2DModules
using Microsoft.Azure.Devices.Client;
using System;
using System.IO;
using System.Threading.Tasks;
namespace c2d_device
{
class Program
{
static async Task Main(string[] args)
{
const string cs = "HostName=.azure-devices.net;DeviceId=;ModuleId=;SharedAccessKey=";
ModuleClient client = ModuleClient.CreateFromConnectionString(cs, TransportType.Mqtt);
await client.SetMessageHandlerAsync((Message message, object ctx) =>
{
Console.WriteLine(new StreamReader(message.BodyStream).ReadToEnd());
return Task.FromResult(MessageResponse.Completed);
}
, null);
Console.WriteLine("Listening");
Console.ReadLine();
}
}
}
using Microsoft.Azure.Devices;
using System;
using System.Text;
using System.Threading.Tasks;
namespace c2d_service
{
class Program
{
static async Task Main(string[] args)
{
var cs = "HostName=.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey="
var service = ServiceClient.CreateFromConnectionString(cs);
var msgText = "Msg from: " + DateTime.Now.ToLongTimeString();
var msg = new Message(Encoding.UTF8.GetBytes(msgText));
await service.SendAsync("", "", msg);
Console.WriteLine("Msg Sent: " + msgText);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment