Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR and SelfHost in F#

I think this will be quick answer, but I couldn't find the right answer during today. I trying create F# SignalR Self-Host application ( I followed this tutorial http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-signalr-20-self-host)

The outcome is my application don't map signalr/hubs on my localhost, there is no error message (only from JavaScript file when it doesn't find client).

namespace Program

open Owin
open Dynamic
open Microsoft.AspNet.SignalR
open Microsoft.AspNet.SignalR.Hubs
open Microsoft.Owin.Hosting
open Microsoft.Owin.Cors
open System
open System.Diagnostics

type MyHub =
   inherit Hub
   member x.Send (name : string) (message : string) =
       base.Clients.All?addMessage name message

type MyWebStartUp() =
    member x.Configuration (app :IAppBuilder) =
       app.UseCors CorsOptions.AllowAll |> ignore
       app.MapSignalR() |> ignore
       ()

module Starter =

[<EntryPoint>]
let main argv = 
    let hostUrl = "http://localhost:8085"
    let disposable = WebApp.Start<MyWebStartUp>(hostUrl)
    Console.WriteLine("Server running on "+ hostUrl)
    Console.ReadLine() |> ignore
    disposable.Dispose() 
    0 // return an integer exit code

I created C# application first and it does work perfectly, I assume my F# code is not right, but I cannot find that bug. For reference here is whole project: https://github.com/MartinBodocky/SignalRFSharp

like image 882
Martin Bodocky Avatar asked Mar 28 '14 01:03

Martin Bodocky


1 Answers

With the action overload of Start, you don't have to make a whole type just for the start up configuration. Also you should definitely use use instead of manually disposing.

    let startup (a:IAppBuilder) =
       a.UseCors(CorsOptions.AllowAll) |> ignore
       a.MapSignalR() |> ignore
    use app = WebApp.Start(hostUrl, startup)

But that's just to make the code nicer, there is a problem with your hub code because the Dynamic module you brought can only invoke a method with one argument, use FSharp.Interop.Dynamic (in nuget) for a robust DLR ? Operator.

open EkonBenefits.FSharp.Dynamic
type MyHub() =
    inherit Hub()
    member this.Send (name : string) (message : string) =
        this.Clients.All?addMessage(name,message) |> ignore
like image 64
jbtule Avatar answered Sep 18 '22 17:09

jbtule