Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of Hosting the Service

Hosting the Service

Many introductory talks on F# emphasize a common set of things:

  • Because F# is .NET, it's interoperable with other .NET applications.
  • As a functional language, in many cases F# allows for shorter code blocks.
  • Some types of development, such as user interface (UI) development, are easier to do in languages other than F#.

That last point about F# not always being the right tool is very important. In software that mostly handles user input, such as a web application, implementing most of the application in a language like C# makes sense because the underlying controls and forms are built with an expectation that variables change value frequently. A functional language is a poor choice in these cases. Instead, it might make sense to integrate F# into our solutions. While it's possible to have pure F# web applications, it's better to continue using languages like C# where they make sense (such as for UI).

For this example, we use a C# web application. To integrate our solution, we need to add three things:

  • Appropriate project references
  • A .svc file to host the endpoint
  • Configuration to tell WCF what binding to use for our service

For the first item on the list above, add a reference to the System.ServiceModel and System.Runtime.Serialization assemblies. Then add a reference to the F# library (called FSharpWCF in the download archive).

Next, add a .svc file named SimpleService.svc. Do this by right-clicking the web application (SimpleWebApp in this example) and selecting New Item. To minimize the amount of work you have to undo from the Visual Studio wizard, select Text File for the file type, name the file SimpleService.svc, and click Add. When the file comes up, add the ServiceHost tag to the file like this:

<%@ ServiceHost Debug="true"
    Service="FSharpWCF.SimpleService" %>

This tells the environment that any requests for SimpleService.svc need to go through a type named FSharpWCF.SimpleService.

All that remains to do is to tell WCF the binding and other behavior needed to call the service. Follow these steps:

  1. Build the project. This action moves the F# assembly into the bin directory of the web project.
  2. Choose Tools > WCF Service Configuration Editor.
  3. In the Microsoft Service Configuration Editor, select File > Open > Config File. Navigate to the web.config file and click Open.
  4. Choose Tasks > Create a New Service.
  5. For Service type, click Browse. You should be in the same directory as the web.config file.
  6. Navigate to bin\FSharpWCF.dll\FSharpWCF.SimpleService. (This is a type browser that integrates with the file browser.) Select Open.
  7. Click Next.
  8. FSharpWCF.ISimpleService should be selected. Click Next. (If the type implemented multiple service contracts, they would all appear in the drop-down list.)
  9. Select the communication mode. Leave this option set to the default, HTTP. Click Next.
  10. Select the type of interoperability desired. Leave this option set to the default, Basic Web Services Interoperability, and click Next.
  11. Type /Simple for the endpoint address, and then click Next.
  12. Click Finish.
  13. Select File > Save.

System.ServiceModel should now be configured as follows:

<system.serviceModel>
    <services>
        <service name="FSharpWCF.SimpleService">
            <endpoint address="/Simple"
               binding="basicHttpBinding"
               contract="FSharpWCF.ISimpleService" />
        </service>
    </services>
</system.serviceModel>

If you like, you can also configure a metadata endpoint. The F# download has this handled in the config file. Once you change the setting, you can send messages to a F# service through any .NET-based web application.

  • + Share This
  • 🔖 Save To Your Account