BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News New Amazon SQS Bindings Added to CoreWCF

New Amazon SQS Bindings Added to CoreWCF

This item in japanese

Amazon delivered a new binding for CoreWCF, the open-source replacement for Windows Communication Foundation, that supports the Amazon Simple Queue Service (SQS) in server and client code. The new binding allows simple migration of legacy Microsoft MSMQ binding to an AWS cloud-based implementation.

The Amazon SQS is a cloud-based queue service that allows components to send, store and receive messages reliably and asynchronously. SQS queues are identified by name and can be read and written to.

It is worth clarifying that the SQS binding in CoreWCF, like the legacy MSMQ (Microsoft Message Queuing) binding, is a one-way binding, meaning the client gets no response for the service invoked via the binding. Consequently, only void methods are allowed in the CoreWCF service interface. The MSMQ support in CoreWCF (together with NetNamedPipe) remains in the preview stage, indicating ongoing development efforts for these additions in the future. Microsoft is expected to release a similar binding for their Azure Storage Queues.

There are two packages for SQS support: one for the client scenario, sending messages to the queue; and one for the server scenario, reading the messages from the queue and processing them. The AWS SDK for .NET is pulled together with the packages as a dependency. A full sample of the client-server code with SQS is available on the extensions’ GitHub repository.

To call an SQS queue in Amazon’s AWS using CoreWCF, developers must add the AWS.WCF.Extensions NuGet package to their .NET project. The first step is to create the authentication provider, an instance of AmazonSQSClient, supplying the AWS credentials to access the queue. The provider instance is passed to the AwsSqsBinding constructor together with the queue name. In the end, CoreWCF’s regular ChannelFactory class creates a communication channel for the service that will be invoked.

// this example assumes an existing wCF Service called ILoggingService
var sqsClient = new AmazonSQSClient();
var sqsBinding = new AWS.WCF.Extensions.SQS.AwsSqsBinding(sqsClient, queueName);
var endpointAddress = new EndpointAddress(new Uri(sqsBinding.QueueUrl));
var factory = new ChannelFactory<ILoggingService>(sqsBinding, endpointAddress);
var channel = factory.CreateChannel();
((System.ServiceModel.Channels.IChannel)channel).Open();

// send a message via the wcf client
channel.LogMessage("Hello World");

To create a CoreWCF service that will be exposed as a consumer of the SQS queue, developers must use AWS.CoreWCF.Extensions NuGet package. Creating the service binding starts with adding the queue transport to the CoreWCF services collection in the service configuration step. In addition, they will have to add the SQS client extension method with the queue name and the AWS credentials using the AddDefaultAWSOptions extension method.

In the app builder configuration step, the EnsureSqsQueue method creates the queue if it’s not present. Then, the service is connected to the queue using the AddServiceEndpoint method, instantiating the AwsSqsBinding class and specifying the queue name.

public class Startup {
  private static readonly string _queueName = "your-aws-sqs-queue";

  public void ConfigureServices(IServiceCollection services) {
    services.AddSingleton<LoggingService>();
    services.AddServiceModelServices();
    services.AddQueueTransport();

    // AWS Configuration
    AWSOptions option = new AWSOptions();
    option.Credentials =
        new BasicAWSCredentials("your access key", "your secret key");
    services.AddDefaultAWSOptions(option);
    services.AddSQSClient(_queueName);
    // end of AWS Configuration
  }

  public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
    var queueUrl = app.EnsureSqsQueue(_queueName);

    app.UseServiceModel(services => {
      services.AddService<LoggingService>();
      services.AddServiceEndpoint<LoggingService, ILoggingService>(
          new AwsSqsBinding(), queueUrl);
    });
  }
}

The SQS binding package allows for two customisations on the server side. The first one is the possibility of specifying the concurrency level for the service binding. It regulates how many messages will be pulled in a batch from the server (the default is one), and the messages will be processed on parallel threads. The other customisation is that the binding exposed two callback collections in the IDispatchCallbackCollection class. They contain two delegates to be invoked when the call to SQS succeeds or fails, allowing for graceful degradation or meaningful logging of the failed requests.

The CoreWCF project was officially released in April 2022, although it began in 2019. It aims to provide a subset of the most frequently used functionality from WCF service on the .NET platform. It is .NET Standard 2.0 compatible, allowing it to be migrated in place on .NET Framework 4.6.2 or above. It covers HTTP and TCP transport protocols with mainstream WCF bindings and other protocols such as Kafka or RabbitMQ. The current version is 1.5.1.

AWS has been one of the key contributors to the CoreWCF community project, which was announced in June 2019. The CoreWCF SQS extensions are released on GitHub under Apache 2.0 license. The current version of the extensions is 1.0.3.

About the Author

Rate this Article

Adoption
Style

BT