【问题标题】:InvalidDataContractException using Azure Notification Hub使用 Azure 通知中心的 InvalidDataContractException
【发布时间】:2013-12-29 20:56:43
【问题描述】:

我几乎复制了这篇关于使用 Windows Azure 通知中心发送推送通知的文章:http://www.windowsazure.com/en-us/manage/services/notification-hubs/notify-users-aspnet/

不幸的是,当我尝试使用 CreateWindowsNativeRegistrationAsync 方法时,我遇到了 InvalidDataContractException 发生在 Microsoft.ServiceBus 中。我看不出我的代码与示例有任何区别,而且我没有进行序列化。 (大约在页面的一半处):

更新:在控制台应用程序中使用“http://test.com”作为 channelURI 会导致异常和以下错误:“不支持的通道 uri:http://test.com”

public async Task<bool> NotificationRegistration(User user, NotificationRegistration reg)
{
  var regs = await hubClient.GetRegistrationsByTagAsync(reg.InstallationID, 100);

  bool updated = false;
  bool firstRegistration = true;
  RegistrationDescription registration = null;

  foreach (var regDescription in regs)
  {
    if (firstRegistration)
    {
      regDescription.Tags = new HashSet<string>() { reg.InstallationID, user._id };

      // We need to handle each platform separately.
      switch (reg.Platform)
      {
        case "WP":
          var winReg = regDescription as WindowsRegistrationDescription;
          winReg.ChannelUri = new Uri(reg.ChannelUri);
          registration = await hubClient.UpdateRegistrationAsync(winReg);
          break;
      }
      updated = true;
      firstRegistration = false;
    }
    else
    {
      // We shouldn't have any extra registrations; delete if we do.
      await hubClient.DeleteRegistrationAsync(regDescription);
    }
  }

  // Create a new registration.
  if (!updated)
  {
    switch (reg.Platform)
    {
        case "WP":
        //always fails here
        registration = await hubClient.CreateWindowsNativeRegistrationAsync(reg.ChannelUri, new string [] { reg.InstallationID, user._id });
            break;
    }
  }
}

任何关于我哪里出错的指导将不胜感激......

【问题讨论】:

    标签: c# asp.net azure azure-notificationhub


    【解决方案1】:

    从平台看来,您正在使用 WindowsPhone。如果是这种情况,您必须创建一个 MpnsRegistrationDescription。因此,您可能会遇到异常,因为 CHannelURI 不是 WIndows Store CHannelURI。

    告诉我,

    埃利奥

    【讨论】:

    • 太棒了!非常感谢,这就是问题所在。我认为该文档应该更清楚地说明 Windows Phone 需要一个单独的类型,因为它在示例中只说 Windows 和 iOS。
    猜你喜欢
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2019-08-25
    • 2019-10-11
    相关资源
    最近更新 更多