首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Azure Event hub / IoT Hub sdk修复Unity中的“无效URI”?

使用Azure Event hub / IoT Hub sdk修复Unity中的“无效URI”?
EN

Stack Overflow用户
提问于 2019-05-07 18:27:29
回答 1查看 292关注 0票数 0

我正在尝试从Azure Unity集线器接收数据到我的IoT应用程序。我正面临着关于URI的问题。

首先,全局图片是一个树莓派向Azure Python Hub发送温度数据(感谢Azure Devices IoT Hub Python SDK)。我的单位,感谢Azure Event Hub.NET SDK连接到我的Azure IoT集线器的事件集线器终结点以接收温度数据。

我修改了Azure提供的c#示例代码,使其可以与Unity一起工作。但是当我播放这个应用程序时,我得到了一个错误消息“无效Uri”。

错误消息: UriFormatException:无效URI: URI方案无效。System.Uri.CreateThis (System.String uri,System.Boolean dontEscape,System.UriKind uriKind) (at :0) System.Uri..ctor (System.String uriString) (at :0) deviceToCloud+d__6.MoveNext () (at Assets/Script/DeviceToCloud.cs:89)重新抛出为AggregateException:出现一个或多个错误。System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) (在<1f0c1ef1ad524c38bbc5536809c46b48>:0) System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout,System.Threading.CancellationToken cancellationToken) (在<1f0c1ef1ad524c38bbc5536809c46b48>:0) System.Threading.Tasks.Task.Wait () (在<1f0c1ef1ad524c38bbc5536809c46b48>:0) deviceToCloud.Start () (在Assets/Script/DeviceToCloud.cs:121)

有没有人知道这个错误以及如何修复它?是关于端点的吗?

感谢您的帮助!

下面是代码。

代码语言:javascript
复制
public class deviceToCloud : MonoBehaviour

{
    // Event Hub-compatible endpoint
    private readonly static string s_eventHubsCompatibleEndpoint = "Endpoint=sb://xxx.servicebus.windows.net/;SharedAccessKeyName=xxx;SharedAccessKey=xxx;EntityPath=xxx";

    // Event Hub-compatible name
    private readonly static string s_eventHubsCompatiblePath = "xxx";

    // Keys
    private readonly static string s_iotHubSasKey = "xxx";
    private readonly static string s_iotHubSasKeyName = "xxx";
    private static EventHubClient s_eventHubClient;

    // Asynchronously create a PartitionReceiver for a partition and then start 
    // reading any messages sent from the simulated client.
    private static async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct)
    {

        // Create the receiver using the default consumer group.
        // For the purposes of this sample, read only messages sent since 
        // the time the receiver is created. Typically, you don't want to skip any messages.
        var eventHubReceiver = s_eventHubClient.CreateReceiver("$Default", partition, EventPosition.FromEnqueuedTime(DateTime.Now));

        while (true)
        {
            if (ct.IsCancellationRequested) break;
            // Check for EventData - this methods times out if there is nothing to retrieve.
            var events = await eventHubReceiver.ReceiveAsync(100);

            // If there is data in the batch, process it.
            if (events == null) continue;

            foreach (EventData eventData in events)
            {
                string data = Encoding.UTF8.GetString(eventData.Body.Array);
                Debug.Log("Message received: " + data);
            }

        }

    }

    //private static async Task Main(string[] args)
    public  async Task ReadD2C()

    {
        Debug.Log("Fonction principale ReadD2C");

        // Create an EventHubClient instance to connect to the
        // IoT Hub Event Hubs-compatible endpoint.
        var connectionString = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompatibleEndpoint), s_eventHubsCompatiblePath, s_iotHubSasKeyName, s_iotHubSasKey);
        s_eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

        // Create a PartitionReciever for each partition on the hub.
        var runtimeInfo = await s_eventHubClient.GetRuntimeInformationAsync();
        var d2cPartitions = runtimeInfo.PartitionIds;

        CancellationTokenSource cts = new CancellationTokenSource();

        Console.CancelKeyPress += (s, e) =>
        {
            e.Cancel = true;
            cts.Cancel();
            Debug.Log("Exit");
        };

        var tasks = new List<Task>();
        foreach (string partition in d2cPartitions)
        {
            tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
        }

        // Wait for all the PartitionReceivers to finsih.
        Task.WaitAll(tasks.ToArray());
    }

    private void Start()
    {
        Debug.Log("Start");

        ReadD2C().Wait();

    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-05-07 19:37:47

看起来s_eventHubsCompatibleEndpoint不是一个正确的地址。也许可以试着这样做?

sb://xxx.servicebus.windows.net/;SharedAccessKeyName=xxx;SharedAccessKey=xxx;EntityPath=xxx

您确定这是正确的URL吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56020557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档