/// <summary>
 /// Registers a service as a subscriber for messages of type <paramref name="messageType"/>.
 /// </summary>
 /// <param name="service">The service registering itself as a subscriber for messages of type <paramref name="messageType"/></param>
 /// <param name="messageType">The type of message to register for (each message type has its own <see cref="IBrokerActor"/> instance)</param>
 /// <param name="listenerName">(optional) The name of the listener that is used to communicate with the service</param>
 /// <returns></returns>
 public static Task RegisterMessageTypeAsync(this StatelessService service, Type messageType, string listenerName = null)
 {
     if (service == null)
     {
         throw new ArgumentNullException(nameof(service));
     }
     if (messageType == null)
     {
         throw new ArgumentNullException(nameof(messageType));
     }
     return(RegisterMessageTypeAsync(service.Context, service.GetServicePartition().PartitionInfo, messageType, listenerName));
 }
 /// <summary>
 /// Unregisters a service as a subscriber for messages of type <paramref name="messageType"/>.
 /// </summary>
 /// <param name="service">The service unregistering itself as a subscriber for messages of type <paramref name="messageType"/></param>
 /// <param name="messageType">The type of message to unregister for (each message type has its own <see cref="IBrokerActor"/> instance)</param>
 /// <param name="flushQueue">Publish any remaining messages.</param>
 /// <returns></returns>
 public static Task UnregisterMessageTypeAsync(this StatelessService service, Type messageType, bool flushQueue)
 {
     if (service == null)
     {
         throw new ArgumentNullException(nameof(service));
     }
     if (messageType == null)
     {
         throw new ArgumentNullException(nameof(messageType));
     }
     return(UnregisterMessageTypeAsync(service.Context, service.GetServicePartition().PartitionInfo, messageType, flushQueue));
 }
        /// <summary>
        /// Unregisters a Service as a subscriber for messages of type <paramref name="messageType"/> using a <see cref="IRelayBrokerActor"/> approach.
        /// </summary>
        /// <param name="service">The service registering itself as a subscriber for messages of type <paramref name="messageType"/></param>
        /// <param name="messageType">The type of message to unregister for (each message type has its own <see cref="IBrokerActor"/> instance)</param>
        /// <param name="relayBrokerActorId">The ID of the relay broker to unregister with.</param>
        /// <param name="sourceBrokerActorId">(optional) The ID of the source <see cref="IBrokerActor"/> that was used as the source for the <paramref name="relayBrokerActorId"/>
        /// If not specified, the default <see cref="IBrokerActor"/> for the message type <paramref name="messageType"/> will be used.</param>
        /// <param name="flushQueue">Publish any remaining messages.</param>
        /// <returns></returns>
        public static Task UnregisterMessageTypeWithRelayBrokerAsync(this StatelessService service, Type messageType, ActorId relayBrokerActorId, ActorId sourceBrokerActorId, bool flushQueue)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (messageType == null)
            {
                throw new ArgumentNullException(nameof(messageType));
            }
            if (relayBrokerActorId == null)
            {
                throw new ArgumentNullException(nameof(relayBrokerActorId));
            }

            return(UnregisterMessageTypeWithRelayBrokerAsync(service.Context, service.GetServicePartition().PartitionInfo, messageType, relayBrokerActorId, sourceBrokerActorId, flushQueue));
        }
        public static async Task UnregisterMessageTypeWithBrokerServiceAsync(this StatelessService service, Type messageType, bool flushQueue, Uri brokerServiceName = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (messageType == null)
            {
                throw new ArgumentNullException(nameof(messageType));
            }
            if (brokerServiceName == null)
            {
                brokerServiceName = await PublisherServices.PublisherServiceExtensions.DiscoverBrokerServiceNameAsync(new Uri(service.Context.CodePackageActivationContext.ApplicationName));

                if (brokerServiceName == null)
                {
                    throw new InvalidOperationException("No brokerServiceName was provided or discovered in the current application.");
                }
            }
            var brokerService = await PublisherActors.PublisherActorExtensions.GetBrokerServiceForMessageAsync(messageType.Name, brokerServiceName);

            var serviceReference = CreateServiceReference(service.Context, service.GetServicePartition().PartitionInfo);
            await brokerService.UnregisterServiceSubscriberAsync(serviceReference, messageType.FullName, flushQueue);
        }