Skip to content

Communicate to Azure Service Fabric Services using Service Bus Queues and Topics

License

Notifications You must be signed in to change notification settings

yonglehou/ServiceFabric.ServiceBus

 
 

Repository files navigation

Demo Project

Need some help to get started? Have a look at: 'https://github.com/loekd/ServiceFabric.ServiceBus/tree/master/ServiceFabric.ServiceBus.Demo'

Nuget Packages:

ServiceFabric.ServiceBus.Clients https://www.nuget.org/packages/ServiceFabric.ServiceBus.Clients/2.0.0 ServiceFabric.ServiceBus.Services https://www.nuget.org/packages/ServiceFabric.ServiceBus.Services/2.0.0 (supports the GA version of Azure Service Fabric)

If you want to integrate this into an existing project, read on...


Server Package

Make sure your projects are configured to build as 64 bit programs!

To create a message handler:

internal sealed class Handler : IServiceBusMessageReceiver
{
	private readonly StatefulService _service;

	public Handler(StatefulService service)
	{
		_service = service;
	}

	public void ReceiveMessage(BrokeredMessage message)
	{
		ServiceEventSource.Current.ServiceMessage(_service, $"Handling queue message {message.MessageId}");
	}
}

To create a Stateful Service that can be accessed through a Service Bus Queue:

internal sealed class SampleQueueListeningStatefulService : StatefulService
{
	protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
	{
		// In the configuration file, define connection strings: 
		// "Microsoft.ServiceBus.ConnectionString.Receive"
		// and "Microsoft.ServiceBus.ConnectionString.Send"
			
		// Also, define a QueueName:
		string serviceBusQueueName = CloudConfigurationManager.GetSetting("QueueName");

		yield return new ServiceReplicaListener(context => new ServiceBusQueueCommunicationListener(
			new Handler(this)
			, context				
			, serviceBusQueueName), "ServiceBusEndPoint");
	}
}

To create a Stateful Service that can be accessed through a Service Bus Subscription:

internal sealed class SampleSubscriptionListeningStatefulService : StatefulService
{
	protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
	{
		// In the configuration file, define connection strings: 
		// "Microsoft.ServiceBus.ConnectionString.Receive"
		// and "Microsoft.ServiceBus.ConnectionString.Send"
			
		// Also, define Topic & Subscription Names:
		string serviceBusTopicName = CloudConfigurationManager.GetSetting("TopicName");
		string serviceBusSubscriptionName = CloudConfigurationManager.GetSetting("SubscriptionName");

		yield return new ServiceReplicaListener(context => new ServiceBusSubscriptionCommunicationListener(
			new Handler(this)
			, context			
			, serviceBusTopicName
			, serviceBusSubscriptionName));
	}
}

To create a Stateless Service that can be accessed through a Service Bus Queue:

internal sealed class SampleQueueListeningStatelessService : StatelessService
{
	protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
	{
		// In the configuration file, define connection strings: 
		// "Microsoft.ServiceBus.ConnectionString.Receive"
		// and "Microsoft.ServiceBus.ConnectionString.Send"
			
		// Also, define a QueueName:
		string serviceBusQueueName = CloudConfigurationManager.GetSetting("QueueName");
		yield return new ServiceInstanceListener(context => new ServiceBusQueueCommunicationListener(
			new Handler(this)
			, context			
			, serviceBusQueueName));
	}
}

To create a Stateless Service that can be accessed through a Service Bus Subscription:

internal sealed class SampleSubscriptionListeningStatelessService : StatelessService
{
	protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
	{
		// In the configuration file, define connection strings: 
		// "Microsoft.ServiceBus.ConnectionString.Receive"
		// and "Microsoft.ServiceBus.ConnectionString.Send"
			
		// Also, define Topic & Subscription Names:
		string serviceBusTopicName = CloudConfigurationManager.GetSetting("TopicName");
		string serviceBusSubscriptionName = CloudConfigurationManager.GetSetting("SubscriptionName");

		yield return new ServiceInstanceListener(context => new ServiceBusSubscriptionCommunicationListener(
			new Handler(this)
			, context
			, serviceBusTopicName
			, serviceBusSubscriptionName));
	}
}

Client Package

Make sure your projects are configured to build as 64 bit programs!

To communicate to a ServiceFabric Service through a Topic (Queues are also supported): (add the Microsoft.ServiceFabric.Services nuget package)

//the name of your application and the name of the Service, the default partition resolver and the topic name
//to create a communication client factory:
var uri = new Uri("fabric:/[ServiceFabric App]/[ServiceFabric Service]");
var resolver = ServicePartitionResolver.GetDefault();
string serviceBusTopicName = CloudConfigurationManager.GetSetting("TopicName");
var factory = new ServiceBusTopicCommunicationClientFactory(resolver, serviceBusTopicName);

//determine the partition and create a communication proxy
var partitionKey = new ServicePartitionKey(0L);
var servicePartitionClient = new ServicePartitionClient<ServiceBusTopicCommunicationClient>(factory, uri, partitionKey);

//use the proxy to send a message to the Service
servicePartitionClient.InvokeWithRetry(c => c.SendMessage(new BrokeredMessage()
{
	Properties =
	{
		{ "TestKey", "TestValue" }
	}
}));

About

Communicate to Azure Service Fabric Services using Service Bus Queues and Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 90.5%
  • PowerShell 9.5%