1 people like it.

Playing with NServiceBus

Don't do this at home, folks - the modules here should obviously be in separate processes to actually get any benefit from busing stuff around. Still, I think this gives a minimal implementation of NServiceBus on Rabbit to play with; just past the code below into a console app and either add a "rabbit connection" to config or pass it in via the command line. You'll need to nuget (or paket) reference NServiceBus.RabbitMQ, Argu, and their dependencies. I've put all the config in code as it makes experimentation easier.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
module Configuration =
    open Nessos.Argu
    type Config =
        | [<Mandatory>] Rabbit_Connection of string
        with 
            interface IArgParserTemplate with
                member ec.Usage =
                    match ec with
                    | Rabbit_Connection _ -> "specify host name of rabbitmq server"

    let parser = ArgumentParser.Create<Config>()

open System
open NServiceBus

module Shared =
    open Configuration
    let config = parser.Parse()
    let bus =
        let bc = BusConfiguration()
        bc.EndpointName "Here"
        bc.UseSerialization<JsonSerializer>() |> ignore
        bc.EnableInstallers()
        bc.UsePersistence<InMemoryPersistence>() |> ignore
        bc.UseTransport<RabbitMQTransport>()
            .DisableCallbackReceiver()
            .ConnectionString(config.GetResult <@ Rabbit_Connection @>) |> ignore
        bc.DiscardFailedMessagesInsteadOfSendingToErrorQueue()
        Bus.Create(bc).Start()
        
    type PrintStuff() =
        member val Message : string = "" with get, set
        interface ICommand

    type StuffPrinted() =
        member val Success : bool = false with get, set
        interface IEvent

module Server =
    open Shared
    type PrintHandler(bus : IBus) =
        interface IHandleMessages<PrintStuff> with
            member __.Handle(ps) =
                if not <| ps.Message.Contains "banana" then
                    printfn "%s" ps.Message
                    bus.Publish (StuffPrinted(Success = true))
                else
                    bus.Publish (StuffPrinted(Success = false))

module Client =
    open Shared
    type PrintedHandler() =
        interface IHandleMessages<StuffPrinted> with
            member __.Handle(ps) =
                printfn "Message printed: %b" ps.Success

    let sendPrint message =
        bus.Send("Here", PrintStuff(Message = message)) |> ignore

Console.ReadLine() |> ignore
Client.sendPrint "A message"
Client.sendPrint "And another"
// This one shouldn't get printed.
Client.sendPrint "And one with a banana"

Console.ReadLine() |> ignore
Shared.bus.Dispose()
namespace Argu
type Config =
  | Rabbit_Connection of string
  interface obj
  override Usage : string

Full name: Script.Configuration.Config
union case Config.Rabbit_Connection: string -> Config
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val ec : Config
override Config.Usage : string

Full name: Script.Configuration.Config.Usage
val parser : obj

Full name: Script.Configuration.parser
namespace System
namespace NServiceBus
Multiple items
namespace NServiceBus.Configuration

--------------------
namespace System.Configuration

--------------------
module Configuration

from Script
val config : obj

Full name: Script.Shared.config
val bus : IBus

Full name: Script.Shared.bus
val bc : BusConfiguration
Multiple items
type BusConfiguration =
  inherit ExposeSettings
  new : unit -> BusConfiguration
  member AssembliesToScan : assemblies:IEnumerable<Assembly> -> unit + 1 overload
  member Conventions : unit -> ConventionsBuilder
  member CustomConfigurationSource : configurationSource:IConfigurationSource -> unit
  member EndpointName : name:string -> unit
  member EndpointVersion : version:string -> unit
  member OverrideLocalAddress : queue:string -> unit
  member OverridePublicReturnAddress : address:Address -> unit
  member Pipeline : PipelineSettings with get, set
  member RegisterComponents : registration:Action<IConfigureComponents> -> unit
  ...

Full name: NServiceBus.BusConfiguration

--------------------
BusConfiguration() : unit
BusConfiguration.EndpointName(name: string) : unit
(extension) BusConfiguration.UseSerialization<'T (requires 'T :> Serialization.SerializationDefinition)>() : Serialization.SerializationExtentions<'T>
(extension) BusConfiguration.UseSerialization(serializerType: Type) : unit
Multiple items
type JsonSerializer =
  inherit SerializationDefinition
  new : unit -> JsonSerializer

Full name: NServiceBus.JsonSerializer

--------------------
JsonSerializer() : unit
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
(extension) BusConfiguration.EnableInstallers(?username: string) : unit
(extension) BusConfiguration.UsePersistence<'T (requires 'T :> Persistence.PersistenceDefinition)>() : PersistenceExtentions<'T>
(extension) BusConfiguration.UsePersistence<'T,'S (requires 'T :> Persistence.PersistenceDefinition and 'S :> Persistence.StorageType)>() : PersistenceExtentions<'T,'S>
(extension) BusConfiguration.UsePersistence(definitionType: Type) : PersistenceExtentions
type InMemoryPersistence =
  inherit PersistenceDefinition

Full name: NServiceBus.InMemoryPersistence
(extension) BusConfiguration.UseTransport<'T (requires default constructor and 'T :> Transports.TransportDefinition)>() : TransportExtensions<'T>
(extension) BusConfiguration.UseTransport(transportDefinitionType: Type) : TransportExtensions
Multiple items
type RabbitMQTransport =
  inherit TransportDefinition
  new : unit -> RabbitMQTransport

Full name: NServiceBus.RabbitMQTransport

--------------------
RabbitMQTransport() : unit
(extension) BusConfiguration.DiscardFailedMessagesInsteadOfSendingToErrorQueue() : unit
type Bus =
  static member Create : configuration:BusConfiguration -> IStartableBus
  static member CreateSendOnly : configuration:BusConfiguration -> ISendOnlyBus

Full name: NServiceBus.Bus
Bus.Create(configuration: BusConfiguration) : IStartableBus
Multiple items
type PrintStuff =
  interface ICommand
  new : unit -> PrintStuff
  member Message : string
  member Message : string with set

Full name: Script.Shared.PrintStuff

--------------------
new : unit -> PrintStuff
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
type ICommand =

Full name: NServiceBus.ICommand
Multiple items
type StuffPrinted =
  interface IEvent
  new : unit -> StuffPrinted
  member Success : bool
  member Success : bool with set

Full name: Script.Shared.StuffPrinted

--------------------
new : unit -> StuffPrinted
type bool = Boolean

Full name: Microsoft.FSharp.Core.bool
Multiple items
type IEvent =

Full name: NServiceBus.IEvent

--------------------
type IEvent<'T> = IEvent<Handler<'T>,'T>

Full name: Microsoft.FSharp.Control.IEvent<_>
module Shared

from Script
Multiple items
type PrintHandler =
  interface IHandleMessages<PrintStuff>
  new : bus:IBus -> PrintHandler

Full name: Script.Server.PrintHandler

--------------------
new : bus:IBus -> PrintHandler
val bus : IBus
type IBus =
  member CurrentMessageContext : IMessageContext
  member Defer : delay:TimeSpan * message:obj -> ICallback + 1 overload
  member DoNotContinueDispatchingCurrentMessageToHandlers : unit -> unit
  member ForwardCurrentMessageTo : destination:string -> unit
  member HandleCurrentMessageLater : unit -> unit
  member InMemory : IInMemoryOperations
  member Reply : message:obj -> unit + 1 overload
  member Return<'T> : errorEnum:'T -> unit
  member SendLocal : message:obj -> ICallback + 1 overload
  member Subscribe<'T> : unit -> unit + 1 overload
  ...

Full name: NServiceBus.IBus
type IHandleMessages<'T> =
  member Handle : message:'T -> unit

Full name: NServiceBus.IHandleMessages<_>
override PrintHandler.Handle : ps:PrintStuff -> unit

Full name: Script.Server.PrintHandler.Handle
val ps : PrintStuff
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
property PrintStuff.Message: string
String.Contains(value: string) : bool
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
ISendOnlyBus.Publish<'T>() : unit
ISendOnlyBus.Publish<'T>(messageConstructor: Action<'T>) : unit
ISendOnlyBus.Publish<'T>(message: 'T) : unit
Multiple items
type PrintedHandler =
  interface IHandleMessages<StuffPrinted>
  new : unit -> PrintedHandler

Full name: Script.Client.PrintedHandler

--------------------
new : unit -> PrintedHandler
override PrintedHandler.Handle : ps:StuffPrinted -> unit

Full name: Script.Client.PrintedHandler.Handle
val ps : StuffPrinted
property StuffPrinted.Success: bool
val sendPrint : message:string -> unit

Full name: Script.Client.sendPrint
val message : string
ISendOnlyBus.Send<'T>(messageConstructor: Action<'T>) : ICallback
ISendOnlyBus.Send(message: obj) : ICallback
ISendOnlyBus.Send<'T>(address: Address, messageConstructor: Action<'T>) : ICallback
ISendOnlyBus.Send<'T>(destination: string, messageConstructor: Action<'T>) : ICallback
ISendOnlyBus.Send(address: Address, message: obj) : ICallback
ISendOnlyBus.Send(destination: string, message: obj) : ICallback
ISendOnlyBus.Send<'T>(address: Address, correlationId: string, messageConstructor: Action<'T>) : ICallback
ISendOnlyBus.Send<'T>(destination: string, correlationId: string, messageConstructor: Action<'T>) : ICallback
ISendOnlyBus.Send(address: Address, correlationId: string, message: obj) : ICallback
ISendOnlyBus.Send(destination: string, correlationId: string, message: obj) : ICallback
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.ReadLine() : string
module Client

from Script
Raw view Test code New version

More information

Link:http://fssnip.net/sM
Posted:9 years ago
Author:mavnn
Tags: nservicebus , rabbitmq