NServicebus WCF Integration 101

90
NServiceBus and WCF Integration 101 October 10, 2014 By Rich Helton

description

NServicebus WCF Integration 101

Transcript of NServicebus WCF Integration 101

Page 1: NServicebus WCF Integration 101

NServiceBus and WCF Integration101

October 10, 2014By Rich Helton

Page 2: NServicebus WCF Integration 101

Why NServiceBus?

NServiceBus (NSB) is the most popular ESB for the C# platform in existence. It is decentralized and has many tools to assist in deployment. Many people may wonder the need to use an ESB? It is to manage services for any applications not performing in the website.

Page 3: NServicebus WCF Integration 101

Source code

My test source code can be found athttps://github.com/richhelton

Particular samples and souce code can be found at https://github.com/Particular

Page 4: NServicebus WCF Integration 101

For those that don't use ESBs....

You may see many websites that say “Don't refresh this page” for instance as they are website only centric that may not consider an ESB. You may also talk to some of their staff to know that they do not acknowledge that server and network errors can occur. For those who want to monitor their services, transactions, messages, and give your website the ability to refresh a page after you enter a credit card, we have NSB.

Page 5: NServicebus WCF Integration 101

What is a Service-Oriented Architecture

SOA is a combination of design patterns, usually implemented through frameworks.SOA combines distinct services that are communicated through messages to cooperate as a single end-to-end business system. A service could be considered a Windows service, or a distinct unit of work, such as a mainframe job.

Page 6: NServicebus WCF Integration 101

SOA Visual

Page 7: NServicebus WCF Integration 101

More SOA Advantages

By using services, we may start/stop services based on performance. We may get running totals on the number of successful and error messages.

Page 8: NServicebus WCF Integration 101

What is a Enterprise Service Bus (ESB)?

ESB is a software architecture design model in which a common bus is shared across a framework to allow common agreed upon communication between different endpoints or services.

Page 9: NServicebus WCF Integration 101

An ESB Visual

Page 10: NServicebus WCF Integration 101

Software Quality

Software Quality are cross-cutting features of the software, that normally defines the technical requirements of the software itself.These qualities are usually security, re-use, maintainability, reliability, efficiency, and other characteristics that make one want to use the framework. Notice that NSB match these qualities one-to-one.

Page 11: NServicebus WCF Integration 101

SOA and ESBs to the rescue

In the Microsoft world, when discussing a framework that implements Service Oriented Architecture (SOA), one may think of the Windows Communication Foundation (WCF) web services.

Page 12: NServicebus WCF Integration 101

SOA and ESBs to the rescue

In the Microsoft world, when discussing a framework that implements Service Oriented Architecture (SOA), one may think of the Windows Communication Foundation (WCF) web services.

Page 13: NServicebus WCF Integration 101

Benefits for NSB

Separate business logic between services. High availability of services. Highly configurable. Message durability in guaranteed delivery. Viewing and monitoring messages and services give an end-to-end viewpoint. Retries for sending of messages. Message workflows in the form of Sagas. Heartbeats to give real-time status. Easy encryption of messages and data. NSB Hosting and installation tools.

Page 14: NServicebus WCF Integration 101

Some Basics

October 10, 2014By Rich Helton

Page 15: NServicebus WCF Integration 101

The Message

NSB is built to send messages on a queue using a variety message queues and techniques, along to various configurations for almost any type of Service. First we start with messages. IMessage – is a basic message interface. IEvent – used mostly for publish-subscribe, and is a type of IMessage. ICommand – used mostly for request-response, and is a type of IMessage.

Page 16: NServicebus WCF Integration 101

Some simple Message code

Page 17: NServicebus WCF Integration 101

The Bus

NSB is built on configuring its ESB bus called IBus, and I can be configured for multiple queue and persistence, endpoint, services and message scenarios. A very basic configure.

Page 18: NServicebus WCF Integration 101

The Message handler

NSB sends messages through the Send() or Publish() methods, but it needs a message handler to be listening on on the message queue to process the message. A message handler will listen on an endpoint looking for a particular message. The endpoints and message that it is listening for is defined in the App.config, and in the message handler function.

Page 19: NServicebus WCF Integration 101

A simple Message handler

Page 20: NServicebus WCF Integration 101

Additional Tools

October 10, 2014By Rich Helton

Page 21: NServicebus WCF Integration 101

Additional Tools for NSB

Service Matrix is a graphical interface extension in Visual Studio for rapid development. Service Pulse is a production monitoring tool for heartbeats and messages. Service Insight provides deep insight into endpoints, messages and services.

Page 22: NServicebus WCF Integration 101

ServiceMatrix

Service Matrix is a graphical interface extension in Visual Studio for rapid development. It will generate code from models.

Page 23: NServicebus WCF Integration 101

ServiceMatrixSoultion Builder

Page 24: NServicebus WCF Integration 101

ServicePulse

Getting heartbeats and custom messages for Production. It is installed as a service that is connected through a URL http://localhost:9090/#/dashboard

Page 25: NServicebus WCF Integration 101

ServiceInsight

ServiceInsight provides a deep insight into messages, endpoints and services as they run, to include a graphical representation.

Page 26: NServicebus WCF Integration 101

ServiceInsight Messages

ServiceInsight provides detailed message views.

Page 27: NServicebus WCF Integration 101

Message Models

Sept 25, 2014By Rich Helton

Page 28: NServicebus WCF Integration 101

Message Features

NSB supports many message models such as request-response and publish-subscribe. Messages do not have to be only messages for the NSB Bus, but NSB integrates into databases and web services as well for third party integration. NSB can also be used to integrate into File I/O , as well as the Secure File tranfer Protocol.

Page 29: NServicebus WCF Integration 101

Publish-subscribe

NSB supports publish-subscribe, where a publisher puts the message on a queue where multiple subscribers may read it off the queue.

Page 30: NServicebus WCF Integration 101

Request-response

NSB supports request-response, where a requester sends a message that the replier receives and responds with a different message.

Page 31: NServicebus WCF Integration 101

Saga services

NSB supports sagas, the ability to save message state and respond back to the originator with changes. This is saving message state.

Page 32: NServicebus WCF Integration 101

Timeout Message

NSB supports timeout messages, the ability to set a timer in seconds, minutes, etc., or time of day to send a special message that a timeout has occurred, and to process the action, such as delete a message, start a job, and more.Here, we schedule a task every minute.

public void Handle(ScheduleATask message){

Console.WriteLine("Scheduling a task to be executed every1 minute");

Schedule.Every(TimeSpan.FromMinutes(1)).Action(() => bus.SendLocal(new ScheduledTaskExecuted()));

}

Page 33: NServicebus WCF Integration 101

Message Mutators

NSB supports message mutators, the ability to mutate, or change, messages during runtime as the message is transmitted through different endpoints. This is valuable tool for adding information, such as debugging, or informational, information as it is running.

Page 34: NServicebus WCF Integration 101

Message Encryption

NSB supports message encryption, the ability to encrypt pieces or the whole of messages and data. The default encryption used by NSB is AES.

Page 35: NServicebus WCF Integration 101

Scaleout

NSB supports scaleout messaging for clustering, this is the ability to have multiple worker services, that are clones of the same codebase, with a single sender, to offload the performance to multiple machines.

Page 36: NServicebus WCF Integration 101

Performance Monitoring

NSB supports performance counters integration into the Windows Performance Monitor to get statistics on endpoints, messages and services.

Page 37: NServicebus WCF Integration 101

Gateways

NSB supports gateways, which is the ability to send messages through an HTTP/HTTPS tunneling, in scenarios where HTTPS is needed for security and messages need to pass through these protocols to meet firewall requirements.

Page 38: NServicebus WCF Integration 101

Databus

NSB supports databus, which is the ability to attach a file to a link, or attachment, with the message, because the file is too large to be enclosed in the message itself.

Page 39: NServicebus WCF Integration 101

Sagas, What are they good for?

October 10, 2014By Rich Helton

Page 40: NServicebus WCF Integration 101

Saga Features

Sagas can save state of messages into a data store. Sagas can route messages based on message state. Sagas are event driven to be started by messages. Sagas contain timers to execute events on messages, the timer can be a periodic time such as every hour, or to execute at a certain date and time.

Page 41: NServicebus WCF Integration 101

Sagas intercept and send messages

Sagas are started by messages, they save data, and send messages. As they intercept messages, they can read and save state based on the message ID to add changes to the message, creating workflow.

Page 42: NServicebus WCF Integration 101

Saga visual

Page 43: NServicebus WCF Integration 101

Saga saves data

Sagas will save data from a message in the form of the IContainSaga interface.

Page 44: NServicebus WCF Integration 101

Saga maps data

Sagas will map the data, depending on the IBus configuration, from/to the message to/from the Saga Data object.

Page 45: NServicebus WCF Integration 101

Saga handle messages

Sagas are started by at least one message and handler messages.

Page 46: NServicebus WCF Integration 101

Saga data from messages

Sagas can save data, the Data object, associated with a message key to get details on the end-to-end workflow of the message

Page 47: NServicebus WCF Integration 101

Saga data from messages

Sagas can retrieve data associated with a message key to get details on the end-to-end workflow of the message

Page 48: NServicebus WCF Integration 101

ConfigureHowToFindSags()

Sagas need to know how to lookup messages, the key association, usually from an ID in the message, the ConfigureHowToFindSaga() performs this function.

Page 49: NServicebus WCF Integration 101

Timeouts

Sagas can handle timeouts

Set a timer

Catch a timer message

Page 50: NServicebus WCF Integration 101

Semi-conclusions

Sagas are very robust and require a class of slides unto themselves.

More on them later.

Page 51: NServicebus WCF Integration 101

Persistence Patterns

October 10, 2014By Rich Helton

Page 52: NServicebus WCF Integration 101

Persistence Features

NSB supports many methods to queue and persists messages. NSB can save subscription information for publish-subscribe in various models. NSB can save Saga data information into various models. NSB takes care of the mapping of the data objects.

Page 53: NServicebus WCF Integration 101

Available Persistent models

Page 54: NServicebus WCF Integration 101

Persistence interfaces

In-Memory – items that are persisted in the the memory of the service itself. RavenDB – the default for many items like subscriptions, items are persisted in a local Raven database. MSMQ – the default for messages, items are persisted in the Microsoft Message Queuing. NHibernate – a .NET Hibernate framework to map objects to relational databases, to include SQL Server, MySQL, and many others.

Page 55: NServicebus WCF Integration 101

What is persisted?

Timeouts – items that keep track of timeouts. Default is RavenDB. Subscriptions – the information to keep track of the subscriber information in publish-subscribe. Default is RavenDB. Sagas – the information stored for Saga data from messages. Default is RavenDB. Gateway – the information to keep track of gateway messaging. Default is RavenDB. Distributor – the messages that is sent to different workers. Default is MSMQ.

Page 56: NServicebus WCF Integration 101

Messages

Messages can also be persisted in different types of queues. By default, NSB sends messages in MSMQ. However, by using NHibernate, SQL queuing could just as easily be used to send messages.

Page 57: NServicebus WCF Integration 101

Semi-conclusions

NSB persistent patterns are very robust and require a class of slides unto themselves.

More on them later.

Page 58: NServicebus WCF Integration 101

WCF Basics

October 10, 2014By Rich Helton

Page 59: NServicebus WCF Integration 101

Feature of WCF

The Windows Communication Foundation (WCF) is a Microsoft framework for establishing communications between endpoints, usually between web services. Communication can happen over HTTP, TCP, IPC, or even MSMQ.

Page 60: NServicebus WCF Integration 101

The ABCs of WCF

WCF needs specific information to establish a connection to exchange data:Address – the URL address to establish a connection. Binding – which defines the types of services that provide connections, include security and encoding settings. Contracts – defines the operational, the API functions, and data that will be available from the server to the clients.

Page 61: NServicebus WCF Integration 101

Contracts

There are three main types of contracts:Service – defines the overall web service starting point. Operational – defines the available methods from the service. Data – defines the available data, through serialization, that is available from the endpoints, usually in the method types.

Page 62: NServicebus WCF Integration 101

Operational Contracts

The [OperationContract] defines the available methods from the service.

Page 63: NServicebus WCF Integration 101

Data Contracts

The [DataContract] defines the available data from the service.

Page 64: NServicebus WCF Integration 101

The address

The address, or available URL, is defined for the server in its App.config or Web.config.

Page 65: NServicebus WCF Integration 101

The binding type

The binding type is defined for the server in its App.config or Web.config.We are using the basic http binding and mex http binding as well to expose the endpoint to the client.

Page 66: NServicebus WCF Integration 101

Many binding types

There many binding types in WCF as there are many different transport and message types.

A list can be found at http://msdn.microsoft.com/en-us/library/ms730879(v=vs.110).aspx

Page 67: NServicebus WCF Integration 101

SVC Config Editor

We can graphically configure the App.config, or Web.config if IIS app, using the service configuration editor. Opening it in Visual Studio,

Page 68: NServicebus WCF Integration 101

SVC Config overview

We can see all the items to graphically configure

Page 69: NServicebus WCF Integration 101

Seeing the contract

The client will need import the contracts in the form of a proxy to communicate to the server. One of the forms of importing web service interfaces is the Web Service Definition Language (WSDL).

Page 70: NServicebus WCF Integration 101

The WSDL is online

In most cases the WSDL is online, as it defines the interfaces needed for the clients.

Page 71: NServicebus WCF Integration 101

Viewing the WSDL

We can see that the WSDL has many of the ABC web service components to connect to the web service server.

Page 72: NServicebus WCF Integration 101

Adding the Service Reference

We can add the Service Reference to the client to use the interfaces to communicate to the server.

Page 73: NServicebus WCF Integration 101

The Service shows us sample client code

Page 74: NServicebus WCF Integration 101

Implementing the client code

Page 75: NServicebus WCF Integration 101

Adding Tracing

The WCF Services, be it client or server, can have trace listeners to be added to the application as it runs. Microsoft provides a Service Trace Viewer to view these messages, as the interaction can contain WCF specific pieces. See http://msdn.microsoft.com/en-us/library/ms732023(v=vs.110).aspx

Page 76: NServicebus WCF Integration 101

Configure Tracing

We can configure the tracing through the service configuration editor, to store in the App.config.

Page 77: NServicebus WCF Integration 101

Viewing Tracing

This will allow us to get information on endpoints, such as the messages.

Page 78: NServicebus WCF Integration 101

Hosting WCF

The WCF Server listens for incoming requests from the WCF client. Therefore, it should be available when a client needs to connect. The WCF Service can be running as a Windows Service. In an IIS application. Self-Hosted as a .NET application, usually with a

command window. Or as a Windows Process Activation Service

(WAS).

Page 79: NServicebus WCF Integration 101

WCF Integration

Sept 25, 2014By Rich Helton

Page 80: NServicebus WCF Integration 101

NSB support of WCF

NSB supports WCF integration. The benefits of using NSB with WCF are: NSB simplifies the coding of contracts of WCF, as

well as hosting. This saves on development time. A web service endpoint can be handled as any

other NSB endpoint. This allows all the other benefits of NSB in WCF,

such as message encryption and retries.

Page 81: NServicebus WCF Integration 101

NSB-WCF messages

Messages in NSB are defined with the IMessage interface. This can be done instead of a data contract in WCF.

public class PaymentMessage : IMessage{

public Guid EventId { get; set; }public PaymentReq paymentReq { get; set; }

}

Page 82: NServicebus WCF Integration 101

NSB-WCF operations

Instead of using operations in WCF, NSB uses message handlers to receive a defined message and process it, and in many cases send back a message response.

Page 83: NServicebus WCF Integration 101

NSB-WCF-Saga

Becuase NSB supports Saga workflows, retries, timeouts, tools for production, rapid development, and managing services, these tools can be applied to WCF web services, and we can use NSB to retry a web service, error report on it, manage the service, and use Saga workflow to manage the business logic in the WCF web service.

Page 84: NServicebus WCF Integration 101

NSB Hosting

October 10, 2014By Rich Helton

Page 85: NServicebus WCF Integration 101

NSB hosting features

NServiceBus.Host.exe is an executable to install, and uninstall, DLL's using NSB as Windows services. This includes features to add additional configurations

during runtime, to include endpoints. The NServiceBus.Host framework and executable will

streamline the installation process by creating endpoints. It will handle the Windows service installation and provide NSB management features as well.

Page 86: NServicebus WCF Integration 101

Host and Visual Studio

NServiceBus.Host.exe is an executable that can be used as a reference in Visual Studio.This is so the service can be DLL, it can generate endpoint template code, and run the DLL in Visual Studio.

Page 87: NServicebus WCF Integration 101

Debug

In the Debug properties of the project, we can see NServiceBus.Host.exe running the DLL that is created.

Page 88: NServicebus WCF Integration 101

EndpointConfig

NServiceBus.Host.exe will create a default EndpointConfig.cs when it is added to the project's references.

Page 89: NServicebus WCF Integration 101

The Host has several deployments

NServiceBus.Host.exe has several configurations to run worker services, installations, creating endpoints, and much more.

Page 90: NServicebus WCF Integration 101

Conclusion

NSB brings a lot of features to the table with a small price.