Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Azure通知中心的InvalidDataContractException_C#_Asp.net_Azure_Azure Notificationhub - Fatal编程技术网

C# 使用Azure通知中心的InvalidDataContractException

C# 使用Azure通知中心的InvalidDataContractException,c#,asp.net,azure,azure-notificationhub,C#,Asp.net,Azure,Azure Notificationhub,我正在复制这篇关于使用Windows Azure通知中心发送推送通知的文章: 不幸的是,当我尝试使用CreateWindowsNativeRegistrationAsync方法时,我在Microsoft.ServiceBus中遇到了InvalidDataContractException异常。我看不出我的代码与示例中的代码有什么不同,我没有进行序列化。(大约在页面的一半处): 更新:在控制台应用程序中放入与channelURI相同的代码会导致异常和以下错误:“不支持的频道uri:” 公共异步任务

我正在复制这篇关于使用Windows Azure通知中心发送推送通知的文章:

不幸的是,当我尝试使用CreateWindowsNativeRegistrationAsync方法时,我在Microsoft.ServiceBus中遇到了InvalidDataContractException异常。我看不出我的代码与示例中的代码有什么不同,我没有进行序列化。(大约在页面的一半处):

更新:在控制台应用程序中放入与channelURI相同的代码会导致异常和以下错误:“不支持的频道uri:”

公共异步任务NotificationRegistration(用户用户,NotificationRegistration注册表)
{
var regs=wait hubClient.GetRegistrationsByTagAsync(reg.InstallationID,100);
bool updated=false;
bool firstRegistration=true;
RegistrationDescription注册=空;
foreach(regs中的var regDescription)
{
if(首次注册)
{
regDescription.Tags=newhashset(){reg.InstallationID,user.\u id};
//我们需要分别处理每个平台。
交换机(注册平台)
{
案例“WP”:
var winReg=regDescription作为WindowsRegistrationDescription;
winReg.ChannelUri=新Uri(reg.ChannelUri);
注册=等待hubClient.UpdateRegistrationAsync(winReg);
打破
}
更新=真;
首次注册=错误;
}
其他的
{
//我们不应该有任何额外的注册;如果有,请删除。
等待hubClient.DeleteRegistrationAsync(regDescription);
}
}
//创建一个新的注册。
如果(!已更新)
{
交换机(注册平台)
{
案例“WP”:
//这里总是失败
registration=wait hubClient.createWindowsNativerRegistrationAsync(reg.ChannelUri,新字符串[]{reg.InstallationID,user.\u id});
打破
}
}
}

如果您能提供任何关于我哪里出了问题的指导,我们将不胜感激。…

从平台上看,您似乎正在使用WindowsPhone。如果是这种情况,则必须创建MPnsrRegistrationDescription。因此,您可能会遇到异常,因为CHannelURI不是WIndows应用商店CHannelURI

让我知道


埃利奥

太棒了!非常感谢,这就是问题所在。我认为该文档应该更清楚地表明,Windows Phone需要单独的类型,因为在示例中只说明了Windows和iOS。
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;
    }
  }
}