Processing Service Bus Subscriptions Through WCF - Part 2

In my last post I showed how a WCF service is used to process messages that are sent to Windows Service Bus topic. This post completes the concept by showing how a client application can send event engagement requests to a Windows Service Bus topic, so the messages can later be processed by the WCF service built in my previous post. This post assumes that Windows Service Bus has already been installed. For more information on installing service bus, visit here.

1. Create a new Console Application, which will serve as the client application that will submit messages to the service.

2. Add the service contract which has the interface the WCF service implements, and the data contract for the engagement request. Since the console application does not need a reference to System.ServiceModel.Web, this directive can be commented out.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

//using System.ServiceModel.Web;

using System.Text;

namespace SB.WCF.Subscription

{

[
ServiceContract]

public interface IEngagementService

{

[
OperationContract(IsOneWay = true, Action = "RequestEngagement")]

[ReceiveContextEnabled(ManualControl = true)]

void RequestEngagement(EngagementRequest request);

}

[
DataContract]

public class EngagementRequest

{

[
DataMember]

public Guid EngagementId { get; set; }[

DataMember]

public EngagementInfo Venue { get; set; }[

DataMember]

public string Description { get; set; }[

DataMember]public List<EngagementEvent> EventSchedule { get; set; }

}

public class EngagementEvent

{

[
DataMember]

public string EventName { get; set; }[

DataMember]public DateTime EventDate { get; set; }

}

public class EngagementInfo

{

[
DataMember]

public int Capacity { get; set; }[

DataMember]public string Location { get; set; }

}

}

3. Add the nuget package for Service Bus 1.1. This will add some default configuration to the app.config.

4. Change the app.config with the configuration indicated below. Notice that the configuration is not too different then the configuration used for setting up the WCF service.

<?xml version="1.0" encoding="utf-8"?>

<configuration><

system.serviceModel>

<extensions><

bindingElementExtensions>

<add name="netMessagingTransport" type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingTransportExtensionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /></

bindingElementExtensions>

<bindingExtensions><

add name="netMessagingBinding" type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

</bindingExtensions><!--

In this extension section we are introducing all known service bus extensions. User can remove the ones they don't need. -->

<behaviorExtensions><

add name="connectionStatusBehavior" type="Microsoft.ServiceBus.Configuration.ConnectionStatusElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

<add name="transportClientEndpointBehavior" type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><

add name="serviceRegistrySettings" type="Microsoft.ServiceBus.Configuration.ServiceRegistrySettingsElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

</behaviorExtensions></

extensions>

<behaviors><

endpointBehaviors>

<behavior name="endpointBehavior"><

transportClientEndpointBehavior>

<tokenProvider><

windowsAuthentication>

<stsUris><

stsUri value="https://attpa-10130435.at.local:9355/ServiceBusDefaultNamespace" />

</stsUris></

windowsAuthentication>

</tokenProvider></

transportClientEndpointBehavior>

</behavior></

endpointBehaviors>

</behaviors><

bindings>

<netMessagingBinding><

binding name="messageBinding" closeTimeout="00:03:00" openTimeout="00:03:00" receiveTimeout="00:03:00" sendTimeout="00:03:00" sessionIdleTimeout="00:01:00" prefetchCount="-1">

<transportSettings batchFlushInterval="00:00:01"></transportSettings></

binding>

</netMessagingBinding></

bindings> <client>

<endpoint name="EngagementRequestor"

address="sb://attpa-10130435.at.local/ServiceBusDefaultNamespace/EngagementTopic"

binding="netMessagingBinding"

bindingConfiguration="messageBinding"

contract="SB.WCF.Subscription.IEngagementService"

behaviorConfiguration="endpointBehavior"></endpoint>

</client></

system.serviceModel>

</configuration>

 You will notice that the endpoint only has an address, which was different than the configuration used for the WCF service. This is because the client only needs to know the address (topic) of where the message will be sent.

 4. Now add the following code, which references the WCF service and builds the data contract that will be sent as a message to the service bus.

using SB.WCF.Subscription;

using System;

using System.Collections.Generic;

using System.Linq;

using System.ServiceModel;

using System.Text;

using System.Threading.Tasks;

namespace SB.Subscription.Client

{

class Program

{

static void Main(string[] args)

{

CreateServiceClient();

}

private static void CreateServiceClient()

{

ChannelFactory<IEngagementService> channel = new ChannelFactory<IEngagementService>("EngagementRequestor");

var engagementRequest = new EngagementRequest

{

EngagementId =
Guid.NewGuid(),

Venue = new EngagementInfo { Capacity=50, Location="Hotel California" },Description =

"Geographical Beetle Fighting Tournament",

EventSchedule = new List<EngagementEvent> //[] //indicates a dynamic array

{

new EngagementEvent { EventDate = Convert.ToDateTime("5/6/2014"), EventName="US Beetle Competition"}, new EngagementEvent { EventDate = DateTime.Now, EventName="Asia Beetle Competition"}

}

};

var engagementService = channel.CreateChannel();

engagementService.RequestEngagement(engagementRequest);

channel.Close();

}

}

}

Your client is now ready to send messages to the service bus. Make sure the WCF service is running, that will consume service bus messages and then fire up the client to send a message.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: BayerWhite
Posted on: 4/22/2014 at 1:43 AM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Comments

Comments are closed