简体   繁体   中英

Silverlight F# library and WCF

I have a WCF service(maybe multiple services) and a client in Silverlight(C#). As I read somewhere on the web F# is great for async and parallel programming. I want to give F# a try and write a library that communicates to given WCF service, handles it's faults and returns result.
Are there any benefits of doing so, except learning new language and func programming?

Yes, I think F# can be quite useful for asynchronous programming in this scenario. If you want to write your asynchronous code properly, you need to use BeginFoo / EndFoo methods or you need to use event-based API to avoid blocking the UI thread when performing some asynchronous call. As a result, you cannot write code in the usual sequential style - just by doing one operation after another. Instead, you need to wrap everything in callbacks that contain separate exception handling.

In F#, you can use asynchrornous workflow that hide all the "callbacks" and automaticaly wrap everything in exception handlers. As a result, you can write asynchronous code that does multiple calls as a sequential code and use all usual control flow constructs (such as try , for , while , ...). You can write:

let processData(userInputs) = async { 
  try
    let! temp = Service.AsyncDoSomething(userInputs)
    let! res = Service.AsyncDoSomethingElse(temp)
    return res
  with e ->
    // Handle exception

This calls the DoSomething operation asynchronously and moves the rest of the function into a callback that gets executed when the operation completes (and similarly for DoSomethingElse ). However, the exception handling can be written in the usual way. The feature is also nice for writing some user interface interactions (see for example this SO post or the recording of my F# talk at London User Group )

The disadvantage of using F# for this project that it doesn't have any direct support for WCF. You can certainly use it, but you'll have to write C#-style mutable classes, which doesn't look that nice - a good option may be to use a C# library and define the usual necessary WCF stuff there (and then just use it from F#).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM