Skip to content

Instantly share code, notes, and snippets.

@t0yv0
Created September 4, 2012 18:52
Show Gist options
  • Save t0yv0/3624960 to your computer and use it in GitHub Desktop.
Save t0yv0/3624960 to your computer and use it in GitHub Desktop.
namespace Workers
open System
open System.Collections.Generic
open System.Diagnostics
open System.Linq
open System.Net
open System.ServiceModel
open System.Threading
open System.Threading.Tasks
open Microsoft.WindowsAzure
open Microsoft.WindowsAzure.Diagnostics
open Microsoft.WindowsAzure.ServiceRuntime
open Microsoft.WindowsAzure.StorageClient
[<ServiceContract>]
type IWorker =
[<OperationContract>]
abstract member Notify : message: string -> unit
[<Sealed>]
type Worker() =
interface IWorker with
member this.Notify(msg) =
Trace.WriteLine("Worker.Notify: {0}", msg)
module Client =
let random (collection: seq<'T>) : 'T =
let a = Seq.toArray collection
let r = new Random(DateTime.Now.Millisecond)
a.[r.Next(1, a.Length) - 1]
let Test () =
let worker =
RoleEnvironment.Roles.["Worker"].Instances
|> random
let factory = new ChannelFactory<IWorker>(NetTcpBinding(SecurityMode.None))
let chan = factory.CreateChannel(EndpointAddress(String.Format("net.tcp://{0}/work", worker.InstanceEndpoints.["WorkerEndpoint"].IPEndpoint)))
Debug.WriteLine("Sending..")
System.Threading.Thread.Sleep(1000);
chan.Notify("Sending..")
Debug.WriteLine("Sent OK..")
[<Sealed>]
type WorkerRole() =
inherit RoleEntryPoint()
override wr.Run() =
while (true) do
Thread.Sleep(10000)
Trace.TraceInformation("Working")
override wr.OnStart() =
let host =
new ServiceHost(typeof<Worker>,
Uri(String.Format("net.tcp://{0}",
RoleEnvironment.CurrentRoleInstance
.InstanceEndpoints.["WorkerEndpoint"].IPEndpoint)))
host.AddServiceEndpoint(typeof<IWorker>,
NetTcpBinding(SecurityMode.None), "work")
|> ignore
host.Open()
ServicePointManager.DefaultConnectionLimit <- 8
base.OnStart()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment