Azure / azure-notificationhubs-dotnet

.NET SDK for Azure Notification Hubs
MIT License
70 stars 122 forks source link

Compiling issue with 'Microsoft.Azure.NotificationHubs.CDataMember' #111

Open Martin-Stein opened 4 years ago

Martin-Stein commented 4 years ago

Hi there,

I have an UWP Project, that is using Notificationhub. When I compile my code in debug mode (x86) everything works fine. When I try to create a store package the compilation fails for configuration release x86 with error:

"System.Runtime.Serialization.InvalidDataContractException: Type 'Microsoft.Azure.NotificationHubs.CDataMember' does not have a static method 'GenerateSchema' that takes a parameter of type 'System.Xml.Schema.XmlSchemaSet' as specified by the XmlSchemaProviderAttribute attribute."

I am using VS2019 Version 16.4.2 Nuget Package is: Microsoft.Azure.NotificatioHubs Version 3.3.0

What causes this isssue??

Kind regards,

Martin

Martin-Stein commented 4 years ago

Hi, I have played a litte bit around and it turns out, when I have WindowsAzure.Messaging.Managed and Microsoft.Azure.NotificationHubs Nuget-packages in my project, I am not able to compile in release configuration. Even if no line of code is involved. It is enough to have the Nuget-packages in my references to produce the error above!

Best regards,

Martin

Grzesik commented 4 years ago

Hi, I have the same problem. Do you have any solution? I can't go to the store because of this error. The Microsoft.Azure.NotificationHubs Nuget package has the version 3.3.0 (the latest).

3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : System.Runtime.Serialization.InvalidDataContractException: Type 'Microsoft.Azure.NotificationHubs.CDataMember' does not have a static method 'GenerateSchema' that takes a parameter of type 'System.Xml.Schema.XmlSchemaSet' as specified by the XmlSchemaProviderAttribute attribute. 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.SchemaExporter.InvokeSchemaProviderMethod(Type clrType, XmlSchemaSet schemas, XmlQualifiedName& stableName, XmlSchemaType& xsdType, Boolean& hasRoot) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.SchemaExporter.GetXmlTypeInfo(Type type, XmlQualifiedName& stableName, XmlSchemaType& xsdType, Boolean& hasRoot) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.XmlDataContract.XmlDataContractCriticalHelper..ctor(Type type) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.XmlDataContract..ctor(Type type) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Type type) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Type type) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.DataMember.CriticalHelper.get_MemberTypeContract() 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.DataMember.Equals(Object other, Dictionary2 checkedContracts) 3>C:\Users\Marian.GRZESIK\.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.ClassDataContract.Equals(Object other, Dictionary2 checkedContracts) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.DataContract.Equals(Object other) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Collections.Generic.ObjectEqualityComparer1.Equals(T x, T y) 3>C:\Users\Marian.GRZESIK\.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Collections.Generic.Dictionary2.ContainsValue(TValue value) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Collections.Generic.Dictionary2.ValueCollection.System.Collections.Generic.ICollection<TValue>.Contains(TValue item) 3>C:\Users\Marian.GRZESIK\.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Linq.Enumerable.Contains[TSource](IEnumerable1 source, TSource value) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at SerializationAssemblyGenerator.Program.GenerateDataContractSerializerHelperCode(IEnumerable1 contracts, IEnumerable1 jsonContracts, IEnumerable1 wcfSerializers) 3>C:\Users\Marian.GRZESIK\.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at SerializationAssemblyGenerator.Program.GenerateDataContractSerializerHelper(IEnumerable1 contracts, IEnumerable1 jsonContracts, GeneratorSettings settings, String intermediateAssembly, IEnumerable1 wcfSerializers) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at SerializationAssemblyGenerator.Program.Main(String[] args)

ncunning commented 4 years ago

Same issue. Can't build for release. Anyone found a workaround for store submission?

stankovski commented 4 years ago

Why do you need WindowsAzure.Messaging.Managed nuget package? If you need it for ServiceBus, can you use https://www.nuget.org/packages/Microsoft.Azure.ServiceBus package instead?

ncunning commented 4 years ago

@stankovski The documentation says to use it. https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-windows-store-dotnet-get-started-wns-push-notification#create-a-sample-windows-app

Step 8

stankovski commented 4 years ago

This looks like a doc issue. You should not need the WindowsAzure.Messaging.Managed nuget package.

ncunning commented 4 years ago

@stankovski I have raised a query https://github.com/MicrosoftDocs/azure-docs/issues/51148

But this Line here: var hub = new NotificationHub("", "");

On peeking into the code uses:

region Assembly Microsoft.WindowsAzure.Messaging.Managed, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

//

endregion

using Microsoft.WindowsAzure.Messaging; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Windows.Data.Xml.Dom;

namespace Microsoft.WindowsAzure.Messaging { public sealed class NotificationHub : Entity { public NotificationHub(string notificationHubPath, string connectionString);

Grzesik commented 4 years ago

I use it also, because I have an Xamarin.Forms App. The same Nuget Package is used for iOS, Android and UWP.

ncunning commented 4 years ago

@Grzesik One thing i did find was that i can get it to build/compile in release mode. Notification Hub still doesn't work so kinda pointless:

WITH THE NATIVE TOOLCHAIN ENABLED FOR x86, x64 and ARM:

So i added: typeof(Windows.Networking.PushNotifications.PushNotificationChannel).GetTypeInfo().Assembly, typeof(Microsoft.WindowsAzure.Messaging.DataContractBinarySerializer).GetTypeInfo().Assembly,

The the assembliesToInclude Xamarin.Forms.Forms.Init(e, assembliesToInclude);

And now it builds/compiles. However i see alot of WindowsAzure.Messaging.Managed Exceptions of being thrown and obviously notifications don't work or register with the PushNotificationChannelManager.

Grzesik commented 4 years ago

@ncunning I've tried it, but I get still an error in Release mode::

Processing application code 3> Computing application closure and generating interop code 3> Loading 199 modules... 3> Generating code... 3> Interop code generated. 3> Generating serialization code 3> Compiling interop code 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : System.Runtime.Serialization.InvalidDataContractException: Type 'Microsoft.Azure.NotificationHubs.CDataMember' does not have a static method 'GenerateSchema' that takes a parameter of type 'System.Xml.Schema.XmlSchemaSet' as specified by the XmlSchemaProviderAttribute attribute. 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.SchemaExporter.InvokeSchemaProviderMethod(Type clrType, XmlSchemaSet schemas, XmlQualifiedName& stableName, XmlSchemaType& xsdType, Boolean& hasRoot) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.SchemaExporter.GetXmlTypeInfo(Type type, XmlQualifiedName& stableName, XmlSchemaType& xsdType, Boolean& hasRoot) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.XmlDataContract.XmlDataContractCriticalHelper..ctor(Type type) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.XmlDataContract..ctor(Type type) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Type type) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Type type) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.DataMember.CriticalHelper.get_MemberTypeContract() 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.DataMember.Equals(Object other, Dictionary2 checkedContracts) 3>C:\Users\Marian.GRZESIK\.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.ClassDataContract.Equals(Object other, Dictionary2 checkedContracts) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Runtime.Serialization.DataContract.Equals(Object other) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Collections.Generic.ObjectEqualityComparer1.Equals(T x, T y) 3>C:\Users\Marian.GRZESIK\.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Collections.Generic.Dictionary2.ContainsValue(TValue value) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Collections.Generic.Dictionary2.ValueCollection.System.Collections.Generic.ICollection<TValue>.Contains(TValue item) 3>C:\Users\Marian.GRZESIK\.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at System.Linq.Enumerable.Contains[TSource](IEnumerable1 source, TSource value) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at SerializationAssemblyGenerator.Program.GenerateDataContractSerializerHelperCode(IEnumerable1 contracts, IEnumerable1 jsonContracts, IEnumerable1 wcfSerializers) 3>C:\Users\Marian.GRZESIK\.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at SerializationAssemblyGenerator.Program.GenerateDataContractSerializerHelper(IEnumerable1 contracts, IEnumerable1 jsonContracts, GeneratorSettings settings, String intermediateAssembly, IEnumerable1 wcfSerializers) 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : at SerializationAssemblyGenerator.Program.Main(String[] args) 3> Generating System.Reflection.DispatchProxy proxy code. 3>C:\Users\Marian.GRZESIK.nuget\packages\microsoft.net.native.compiler\2.2.8-rel-28605-00\tools\Microsoft.NetNative.targets(801,5): error : ILT0032: Failed to compile serialization code. See the build log for error details.

stankovski commented 4 years ago

I have updated the UWP sample and have verified that the latest version of the https://www.nuget.org/packages/Microsoft.Azure.NotificationHubs package works and compiles under Release configuration.

As far as usage you would need to update the code to something like:

            var hub = new NotificationHubClient(Secrets.HubName, Secrets.HubConnectionString);
            await hub.CreateOrUpdateInstallationAsync(new Installation
            {
                Platform = NotificationPlatform.Wns,
                InstallationId = "myid",
                PushChannel = channel.Uri
            });

https://github.com/Azure/azure-notificationhubs-dotnet/pull/119

Grzesik commented 4 years ago

Hi @stankovski, I've tested your sample and it works. Unfortunatelly it doesn't change my problem. But I've underestood, what the reason for the problem is. I created the NotificationHub for iOS, Android and Uwp, like described here:

https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-windows-store-dotnet-get-started-wns-push-notification

The problem ist the WindowsAzure.Messaging.Managed Nuget Package, which cause the problem, when linking in realse mode.

It is possible to update that package?

stankovski commented 4 years ago

We are working on updating our SDK to include functionality available in WindowsAzure.Messaging.Managed however you should be able to get your app working with just NotificationHubClient. What functionality are you currently missing?

Grzesik commented 4 years ago

Hi @stankovski, I've tried to substitute teh old API with the new one from your example, but it seems to be not compatible with the Azure settings. It cant find my NotificationHubName. Its strange.

private PushNotificationChannel channel;

    public async Task RegisterRemoteNotifications()
    {
        try
        {
            var tag = GetLastTag();
            if (tag == null)
            {
                return;
            }

            if (channel != null)
            {
                channel.PushNotificationReceived -= Channel_PushNotificationReceived;
            }

            channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
            var token = channel.Uri;
            await SaveToken(token);

            var hub = new NotificationHubClient(AppConstants.NotificationHubName, AppConstants.NotificationHubDefaultListenSharedAccessSignature);

            // delete all registration with Azure Notification Hub for this token
            //await hub.UnregisterAllAsync(token);
            await hub.DeleteRegistrationAsync(token);

            // register device with Azure Notification Hub using the token 
            //Registration registration = await hub.RegisterNativeAsync(token, new string[] { tag });
            var registration = await hub.CreateWindowsNativeRegistrationAsync(token, new string[] { tag });

            await AdvowareMobile.Logging.Log.Info($"RemoteNotifications registered, tag={tag}");

            // subscribe to the SubscriptionTags list with a simple template.
            //string pnsHandle = registration.PNSHandle;
            //TemplateRegistration templateReg = hub.RegisterTemplate(pnsHandle, "defaultTemplate", AppConstants.FCMTemplateBody, AppConstants.SubscriptionTags);

            channel.PushNotificationReceived += Channel_PushNotificationReceived;

            return;
        }
        catch (Exception e)
        {
            await AdvowareMobile.Logging.Log.Error($"RegisteredForRemoteNotifications error: {e.Message}");
        }

        return;
    }
stankovski commented 4 years ago

Not sure what you mean by

cant find my NotificationHubName

Also your line var hub = new NotificationHubClient(AppConstants.NotificationHubName, AppConstants.NotificationHubDefaultListenSharedAccessSignature); has parameters flipped. The first parameter is the connection string and the second one is the hub name.

stankovski commented 4 years ago

As mentioned above (https://github.com/Azure/azure-notificationhubs-dotnet/issues/111#issuecomment-606148344), you should just use installation API (https://docs.microsoft.com/en-us/archive/msdn-magazine/2015/april/microsoft-azure-azure-notification-hubs-best-practices-for-managing-devices#registering-devices). It will allow you to create or update a registration with the channel. There is no need to delete registrations all the time.

Grzesik commented 4 years ago

Hi @stankovski , if I change the parameters in the NotificationHubClient, I get an Exception too:

Message = "The remote server returned an error: (401) Unauthorized. Reason: ..TrackingId:8a7dd317-575c-4429-8b73-675a9d73c156,TimeStamp:2020-04-06T05:56:47.5365128Z"

  1. The Managed version of the library (from 2014) as in my and your link shows, works wery well. It cause only the problem in the release mode and can't be copied to the store
  2. I have to unregister and new register the token and tag, because in login it can be an another credential, which use an another tag.