Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appears when subscribing after publishing an event:System.FormatException: Could not deserialize JSON text #3

Closed
litiansky opened this issue Mar 2, 2021 · 12 comments

Comments

@litiansky
Copy link

litiansky commented Mar 2, 2021

I installed in the Domain project EasyAbp.NotificationService.Domain and EasyAbp.NotificationService.Provider.Mailing packages, and introduced in DomainModule module:
image
The code follows the source code example to write the test:
image
However, the console did report the following error and did not execute the subscription event:
image
Detailed error message:

---> (Inner Exception #4) System.FormatException: Could not deserialize JSON text: '{"$type":"EasyAbp.NotificationService.Provider.Mailing.CreateEmailNotificationEto, EasyAbp.NotificationService.Provider.Mailing.Abstractions","Subject":"test","Body":"","TenantId":null,"UserIds":{"$type":"System.Collections.Generic.List1[[System.Guid, System.Private.CoreLib]], System.Private.CoreLib","$values":["1a5d39c8-2822-42e0-a92e-39fae1a6a4ab"]}}'
---> Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type EasyAbp.NotificationService.Provider.Mailing.CreateEmailNotificationEto. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Subject', line 1, position 152.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Rebus.Serialization.Json.JsonSerializer.Deserialize(String bodyString, Type type)
--- End of inner exception stack trace ---
at Rebus.Serialization.Json.JsonSerializer.Deserialize(String bodyString, Type type)
at Rebus.Serialization.Json.JsonSerializer.GetMessage(TransportMessage transportMessage, Encoding bodyEncoding)
at Rebus.Serialization.Json.JsonSerializer.Deserialize(TransportMessage transportMessage)
at Rebus.Compression.UnzippingSerializerDecorator.Deserialize(TransportMessage transportMessage)
at Rebus.Pipeline.Receive.DeserializeIncomingMessageStep.Process(IncomingStepContext context, Func1 next) at Rebus.DataBus.ClaimCheck.HydrateIncomingMessageStep.Process(IncomingStepContext context, Func1 next)
at Rebus.Retry.FailFast.FailFastStep.Process(IncomingStepContext context, Func1 next) at Rebus.Retry.Simple.SimpleRetryStrategyStep.DispatchWithTrackerIdentifier(Func1 next, String identifierToTrackMessageBy, ITransactionContext transactionContext, String messageId, String secondLevelMessageId)<---
`

Add error messages when using SMS for testing:

---> (Inner Exception #4) System.FormatException: Could not deserialize JSON text: '{"$type":"EasyAbp.NotificationService.Provider.Sms.CreateSmsNotificationEto, EasyAbp.NotificationService.Provider.Sms.Abstractions","Text":"test","Properties":{"$type":"System.Collections.Generic.Dictionary2[[System.String, System.Private.CoreLib],[System.Object, System.Private.CoreLib]], System.Private.CoreLib","MyProperty":"123456"},"TenantId":null,"UserIds":{"$type":"System.Collections.Generic.List1[[System.Guid, System.Private.CoreLib]], System.Private.CoreLib","$values":["1a5d39c8-2822-42e0-a92e-39fae1a6a4ab"]}}' ---> Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type EasyAbp.NotificationService.Provider.Sms.CreateSmsNotificationEto. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Text', line 1, position 139. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Rebus.Serialization.Json.JsonSerializer.Deserialize(String bodyString, Type type) --- End of inner exception stack trace --- at Rebus.Serialization.Json.JsonSerializer.Deserialize(String bodyString, Type type) at Rebus.Serialization.Json.JsonSerializer.GetMessage(TransportMessage transportMessage, Encoding bodyEncoding) at Rebus.Serialization.Json.JsonSerializer.Deserialize(TransportMessage transportMessage) at Rebus.Compression.UnzippingSerializerDecorator.Deserialize(TransportMessage transportMessage) at Rebus.Pipeline.Receive.DeserializeIncomingMessageStep.Process(IncomingStepContext context, Func1 next)
at Rebus.DataBus.ClaimCheck.HydrateIncomingMessageStep.Process(IncomingStepContext context, Func1 next) at Rebus.Retry.FailFast.FailFastStep.Process(IncomingStepContext context, Func1 next)
at Rebus.Retry.Simple.SimpleRetryStrategyStep.DispatchWithTrackerIdentifier(Func1 next, String identifierToTrackMessageBy, ITransactionContext transactionContext, String messageId, String secondLevelMessageId)<---

@gdlcf88 gdlcf88 closed this as completed in e76ecf2 Mar 2, 2021
@gdlcf88
Copy link
Member

gdlcf88 commented Mar 2, 2021

I added a parameterless constructor, please try again with v0.3.1, thanks.

@litiansky
Copy link
Author

This morning I downloaded the source code locally and added constructors to solve the JSON problem, but I found the following two problems:
image

  1. Subscription messages will fail if you do not inherit ITransientDependency
  2. The constructor I use to pass the message is
new CreateEmailNotificationEto(
                    CurrentTenant.Id,
                    userId,
                    "标题",
                    body)

Without the userId, I wonder if the two constructors have the same number of arguments?

@gdlcf88
Copy link
Member

gdlcf88 commented Mar 2, 2021

Subscription messages will fail if you do not inherit ITransientDependency

Please check your module dependency.

public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services
.TryAddTransient<IDistributedEventHandler<CreateEmailNotificationEto>,
CreateEmailNotificationEventHandler>();
}

@litiansky
Copy link
Author

I am using the 0.3.1 package directly now, and still can't receive messages. The error message is as follows

---> (Inner Exception #4) Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'EasyAbp.NotificationService.Provider.Mailing.CreateEmailNotificationEventHandler' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Volo.Abp.EventBus.IocEventHandlerFactory.GetHandler()
   at Volo.Abp.EventBus.EventBusBase.TriggerHandlerAsync(IEventHandlerFactory asyncHandlerFactory, Type eventType, Object eventData, List`1 exceptions)
   at Volo.Abp.EventBus.EventBusBase.TriggerHandlersAsync(Type eventType, Object eventData, List`1 exceptions)
   at Volo.Abp.EventBus.EventBusBase.TriggerHandlersAsync(Type eventType, Object eventData)
   at Volo.Abp.EventBus.Rebus.RebusDistributedEventHandlerAdapter`1.Handle(TEventData message)
   at Rebus.Pipeline.Receive.HandlerInvoker`1.Invoke()
   at Rebus.Pipeline.Receive.DispatchIncomingMessageStep.Process(IncomingStepContext context, Func`1 next)
   at Rebus.Sagas.LoadSagaDataStep.Process(IncomingStepContext context, Func`1 next)
   at Rebus.Pipeline.Receive.ActivateHandlersStep.Process(IncomingStepContext context, Func`1 next)
   at Rebus.Pipeline.Receive.HandleRoutingSlipsStep.Process(IncomingStepContext context, Func`1 next)
   at Rebus.Pipeline.Receive.DeserializeIncomingMessageStep.Process(IncomingStepContext context, Func`1 next)
   at Rebus.DataBus.ClaimCheck.HydrateIncomingMessageStep.Process(IncomingStepContext context, Func`1 next)
   at Rebus.Retry.FailFast.FailFastStep.Process(IncomingStepContext context, Func`1 next)
   at Rebus.Retry.Simple.SimpleRetryStrategyStep.DispatchWithTrackerIdentifier(Func`1 next, String identifierToTrackMessageBy, ITransactionContext transactionContext, String messageId, String secondLevelMessageId)<---

@gdlcf88
Copy link
Member

gdlcf88 commented Mar 2, 2021

@litiansky
Copy link
Author

Sorry, I read the documentation, but I still don't understand why. I knew I needed to introduce modules, so I did:
image

I don't think the TryAddTransient below needs to be added to my module either, but I tried it and it doesn't work either. Thank you for your help

@gdlcf88
Copy link
Member

gdlcf88 commented Mar 2, 2021

Could you please try this code:

            context.Services.AddTransient<CreateEmailNotificationEventHandler>();

@litiansky
Copy link
Author

Very good. After adding this sentence, emails can be sent normally, and parameters can also be passed normally. I hope it can be added to the document, so that other people will not encounter this problem

@gdlcf88
Copy link
Member

gdlcf88 commented Mar 2, 2021

Very good. After adding this sentence, emails can be sent normally, and parameters can also be passed normally. I hope it can be added to the document, so that other people will not encounter this problem

4608b6d

@litiansky
Copy link
Author

There is another problem. I didn't mention a new issue. I just wrote it here.
I used SMS to send the notification, but after execution, the database notification table saved the data, but did not save the passed parameters:
image

Send method:

image

@gdlcf88
Copy link
Member

gdlcf88 commented Mar 2, 2021

There is another problem

Please create a new issue, thank you.

@litiansky
Copy link
Author

There is another problem

Please create a new issue, thank you.

ok It's already created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant