Created
March 11, 2021 06:03
-
-
Save rido-min/09e64245174aceb9296431cce58b1cf4 to your computer and use it in GitHub Desktop.
C2DModules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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