You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
source.AppendLine(" public async ValueTask InvokeAsync(object message, CancellationToken cancellationToken = default)");
85
93
source.AppendLine(" {");
86
-
source.AppendLine(" var messageTypeName = message.GetType().FullName;");
87
-
source.AppendLine(" var handlers = _serviceProvider.GetKeyedServices<HandlerRegistration>(messageTypeName);");
94
+
source.AppendLine(" var messageType = message.GetType();");
95
+
source.AppendLine(" var handlers = GetHandlersForType(messageType);");
88
96
source.AppendLine(" var handlersList = handlers.ToList();");
89
97
source.AppendLine();
90
98
source.AppendLine(" if (handlersList.Count == 0)");
91
-
source.AppendLine(" throw new InvalidOperationException($\"No handler found for message type {messageTypeName}\");");
99
+
source.AppendLine(" throw new InvalidOperationException($\"No handler found for message type {messageType.FullName}\");");
92
100
source.AppendLine();
93
101
source.AppendLine(" if (handlersList.Count > 1)");
94
-
source.AppendLine(" throw new InvalidOperationException($\"Multiple handlers found for message type {messageTypeName}. Use PublishAsync for multiple handlers.\");");
102
+
source.AppendLine(" throw new InvalidOperationException($\"Multiple handlers found for message type {messageType.FullName}. Use PublishAsync for multiple handlers.\");");
95
103
source.AppendLine();
96
104
source.AppendLine(" var handler = handlersList.First();");
@@ -101,19 +109,19 @@ public static string GenerateMediatorImplementation(List<HandlerInfo> handlers)
101
109
// Generate Invoke method (sync)
102
110
source.AppendLine(" public void Invoke(object message, CancellationToken cancellationToken = default)");
103
111
source.AppendLine(" {");
104
-
source.AppendLine(" var messageTypeName = message.GetType().FullName;");
105
-
source.AppendLine(" var handlers = _serviceProvider.GetKeyedServices<HandlerRegistration>(messageTypeName);");
112
+
source.AppendLine(" var messageType = message.GetType();");
113
+
source.AppendLine(" var handlers = GetHandlersForType(messageType);");
106
114
source.AppendLine(" var handlersList = handlers.ToList();");
107
115
source.AppendLine();
108
116
source.AppendLine(" if (handlersList.Count == 0)");
109
-
source.AppendLine(" throw new InvalidOperationException($\"No handler found for message type {messageTypeName}\");");
117
+
source.AppendLine(" throw new InvalidOperationException($\"No handler found for message type {messageType.FullName}\");");
110
118
source.AppendLine();
111
119
source.AppendLine(" if (handlersList.Count > 1)");
112
-
source.AppendLine(" throw new InvalidOperationException($\"Multiple handlers found for message type {messageTypeName}. Use Publish for multiple handlers.\");");
120
+
source.AppendLine(" throw new InvalidOperationException($\"Multiple handlers found for message type {messageType.FullName}. Use Publish for multiple handlers.\");");
113
121
source.AppendLine();
114
122
source.AppendLine(" var handler = handlersList.First();");
115
123
source.AppendLine(" if (handler.IsAsync)");
116
-
source.AppendLine(" throw new InvalidOperationException($\"Cannot use synchronous Invoke with async-only handler for message type {messageTypeName}. Use InvokeAsync instead.\");");
124
+
source.AppendLine(" throw new InvalidOperationException($\"Cannot use synchronous Invoke with async-only handler for message type {messageType.FullName}. Use InvokeAsync instead.\");");
@@ -122,15 +130,15 @@ public static string GenerateMediatorImplementation(List<HandlerInfo> handlers)
122
130
// Generate InvokeAsync<TResponse> method
123
131
source.AppendLine(" public async ValueTask<TResponse> InvokeAsync<TResponse>(object message, CancellationToken cancellationToken = default)");
124
132
source.AppendLine(" {");
125
-
source.AppendLine(" var messageTypeName = message.GetType().FullName;");
126
-
source.AppendLine(" var handlers = _serviceProvider.GetKeyedServices<HandlerRegistration>(messageTypeName);");
133
+
source.AppendLine(" var messageType = message.GetType();");
134
+
source.AppendLine(" var handlers = GetHandlersForType(messageType);");
127
135
source.AppendLine(" var handlersList = handlers.ToList();");
128
136
source.AppendLine();
129
137
source.AppendLine(" if (handlersList.Count == 0)");
130
-
source.AppendLine(" throw new InvalidOperationException($\"No handler found for message type {messageTypeName}\");");
138
+
source.AppendLine(" throw new InvalidOperationException($\"No handler found for message type {messageType.FullName}\");");
131
139
source.AppendLine();
132
140
source.AppendLine(" if (handlersList.Count > 1)");
133
-
source.AppendLine(" throw new InvalidOperationException($\"Multiple handlers found for message type {messageTypeName}. Use PublishAsync for multiple handlers.\");");
141
+
source.AppendLine(" throw new InvalidOperationException($\"Multiple handlers found for message type {messageType.FullName}. Use PublishAsync for multiple handlers.\");");
134
142
source.AppendLine();
135
143
source.AppendLine(" var handler = handlersList.First();");
136
144
source.AppendLine(" var result = await handler.HandleAsync(this, message, cancellationToken, typeof(TResponse));");
@@ -142,19 +150,19 @@ public static string GenerateMediatorImplementation(List<HandlerInfo> handlers)
142
150
// Generate Invoke<TResponse> method (sync)
143
151
source.AppendLine(" public TResponse Invoke<TResponse>(object message, CancellationToken cancellationToken = default)");
144
152
source.AppendLine(" {");
145
-
source.AppendLine(" var messageTypeName = message.GetType().FullName;");
146
-
source.AppendLine(" var handlers = _serviceProvider.GetKeyedServices<HandlerRegistration>(messageTypeName);");
153
+
source.AppendLine(" var messageType = message.GetType();");
154
+
source.AppendLine(" var handlers = GetHandlersForType(messageType);");
147
155
source.AppendLine(" var handlersList = handlers.ToList();");
148
156
source.AppendLine();
149
157
source.AppendLine(" if (handlersList.Count == 0)");
150
-
source.AppendLine(" throw new InvalidOperationException($\"No handler found for message type {messageTypeName}\");");
158
+
source.AppendLine(" throw new InvalidOperationException($\"No handler found for message type {messageType.FullName}\");");
151
159
source.AppendLine();
152
160
source.AppendLine(" if (handlersList.Count > 1)");
153
-
source.AppendLine(" throw new InvalidOperationException($\"Multiple handlers found for message type {messageTypeName}. Use Publish for multiple handlers.\");");
161
+
source.AppendLine(" throw new InvalidOperationException($\"Multiple handlers found for message type {messageType.FullName}. Use Publish for multiple handlers.\");");
154
162
source.AppendLine();
155
163
source.AppendLine(" var handler = handlersList.First();");
156
164
source.AppendLine(" if (handler.IsAsync)");
157
-
source.AppendLine(" throw new InvalidOperationException($\"Cannot use synchronous Invoke with async-only handler for message type {messageTypeName}. Use InvokeAsync instead.\");");
165
+
source.AppendLine(" throw new InvalidOperationException($\"Cannot use synchronous Invoke with async-only handler for message type {messageType.FullName}. Use InvokeAsync instead.\");");
158
166
source.AppendLine();
159
167
source.AppendLine(" object result = handler.Handle!(this, message, cancellationToken, typeof(TResponse));");
0 commit comments