Actually you can do this in an SPA too. An SPA is still served by IIS by default, only it has no WebSharper server-side functonality because it only serves index.html. That being said, you can add the RPC module to your Web.config and then RPC calls should work fine.

1
2
3
4
5
<system.webServer>
	<modules>
		<add name="WebSharper.RemotingModule" type="WebSharper.Web.RpcModule, WebSharper.Web" />
	</modules>
</system.webServer>

Add these lines to your <configuration> section in Web.config and then remoting will work.

Also you will get warning for using synchronous RPC requests. This is because it is not advisable to use synchronous requests because they will block until the response arrives. This means all UI updates and every part of the script is blocked until the request returns. You should consider using only asynchronous RPCs. In your example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module Server =
    let rnd = System.Random()
    [<Rpc>]
    let Next() = 
        async.Return (rnd.Next().ToString())
     
[<JavaScript>]     
module Client =
	
//...

    let addName (name) =
    	async {
            match augmentType with
            | UnAugmented   -> People.Add name
            | Locally       -> People.Add (name + Next())
            | Remotely      ->
            	let! nxt = Server.Next()
            	People.Add(name + nxt)
        }

And then use Async.Start (or View.MapAsync if that's more appropriate) to run it.

By on 3/24/2016 5:54 AM ()

Thanks István Gansperger.

The above seemed to almost work but generated an exception at the evaluation of the server expression (line 13 of the original source: rnd.Next().ToString())

1
2
3
4
5
6
7
8
9
System.TypeInitializationException was unhandled by user code

Message: 
  An exception of type 'System.TypeInitializationException' 
  occurred in UINextSinglePage.dll but was not handled in user code

Additional information: 
  The type initializer for '<StartupCode$UINextSinglePage>.$Client' 
  threw an exception.

I believe it is enough to say that SPA's are not merely single page Client/Server apps, which is OK. :)

By on 3/29/2016 11:56 AM ()

This happens when you have some top level values in your ClientSide module (for example let doc = JS.Document) which only make sense on the client and are implemented as failwith in F#. When the server loads your server-side module it probably picks up the client-side one too and tries to compute the top-level values. In this case make your client-side values either lazy or functions so that they are not evaluated unless used (let doc = lazy JS.Document).

By on 3/29/2016 1:45 PM ()

I think what you want is a sitelet not a single page app. Your RPC will only work if you bootup a sitelet.

The spa template assume that you only need to build JS.

You can still use UI.Next to build the front end of your sitelet.

Maybe you could have a look at the Suave template, there is an RPC call inside, might be what you looking for.

Hope this help.

By on 3/23/2016 3:46 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper