NServicebus WCF Integration 101

Post on 14-Jun-2015

842 views 5 download

Tags:

description

NServicebus WCF Integration 101

Transcript of NServicebus WCF Integration 101

NServiceBus and WCF Integration101

October 10, 2014By Rich Helton

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.

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

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.

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.

SOA Visual

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.

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.

An ESB Visual

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.

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.

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.

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.

Some Basics

October 10, 2014By Rich Helton

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.

Some simple Message code

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.

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.

A simple Message handler

Additional Tools

October 10, 2014By Rich Helton

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.

ServiceMatrix

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

ServiceMatrixSoultion Builder

ServicePulse

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

ServiceInsight

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

ServiceInsight Messages

ServiceInsight provides detailed message views.

Message Models

Sept 25, 2014By Rich Helton

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.

Publish-subscribe

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

Request-response

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

Saga services

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

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()));

}

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.

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.

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.

Performance Monitoring

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

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.

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.

Sagas, What are they good for?

October 10, 2014By Rich Helton

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.

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.

Saga visual

Saga saves data

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

Saga maps data

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

Saga handle messages

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

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

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

ConfigureHowToFindSags()

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

Timeouts

Sagas can handle timeouts

Set a timer

Catch a timer message

Semi-conclusions

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

More on them later.

Persistence Patterns

October 10, 2014By Rich Helton

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.

Available Persistent models

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.

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.

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.

Semi-conclusions

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

More on them later.

WCF Basics

October 10, 2014By Rich Helton

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.

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.

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.

Operational Contracts

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

Data Contracts

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

The address

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

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.

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

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,

SVC Config overview

We can see all the items to graphically configure

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).

The WSDL is online

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

Viewing the WSDL

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

Adding the Service Reference

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

The Service shows us sample client code

Implementing the client code

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

Configure Tracing

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

Viewing Tracing

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

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).

WCF Integration

Sept 25, 2014By Rich Helton

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.

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; }

}

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.

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.

NSB Hosting

October 10, 2014By Rich Helton

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.

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.

Debug

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

EndpointConfig

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

The Host has several deployments

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

Conclusion

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