他人様のお知恵を拝借してF#+WCFでRESTfulなサービスをこしらえる
今回お知恵を拝借したのはこちらの方です。
F#+WCFでRESTfulサービス、いいですね面白そうですね。
WCFはC#で書くと.svcとかいうよく分からないファイルが増えるとかいう程度の認識ですが、F#とのコラボレーションはどんな感じになるのか愉しみですね。
お知恵を拝借してコピペで動かしたんだ!!!!
先のブログのお知恵を拝借して書いたコードがこちらです。
#light namespace FSharpWCF open System open System.Runtime.Serialization open System.ServiceModel open System.ServiceModel.Web [<ServiceContract>] type IGetDataService = interface [<WebGet(UriTemplate="temp/{value}")>] [<OperationContract>] abstract GetData : value:string -> string [<WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate="temp2/{value}")>] [<OperationContract>] abstract GetData2 : value:string -> string end type GetDataService() = interface IGetDataService with member this.GetData value = sprintf "You entered: %s" value member this.GetData2 value = sprintf "You entered: %s" value
- "namespace FSharpWCF"は"module FSharpWCF"では動作しませんでした。さもありなん
- ResponseFormatの指定でJSONも指定できて便利、モダン
FsharpWCF.dll.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="FSharpWCF.GetDataService" behaviorConfiguration="FSharpWCF.GetDataServiceBehavior"> <host> <baseAddresses> <add baseAddress = "http://localhost:8080/FSharpWCF/GetDataService/" /> </baseAddresses> </host> <endpoint binding="webHttpBinding" contract="FSharpWCF.IGetDataService" behaviorConfiguration="Web"> <identity> <dns value="localhost"/> </identity> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="FSharpWCF.GetDataServiceBehavior"> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="Web"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> </configuration>
- FSharpWCF.dll.configのプロパティで"出力ディレクトリにコピーする"を忘れずに
動作方法
- プロジェクトの[プロパティ]-[デバッグ]-[外部プログラムの開始]に"WcfSvcHost.exe(""C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\WcfSvcHost.exe")を指定する
- [コマンドライン引数]に"/service:FSharpWCF.dll /config:FSharpWCF.dll.config"を指定する
- [作業ディレクトリ]に出力ディレクトリを指定する
- デバッグ実行する
- "http://localhost:8080/FSharpWCF/GetDataService/temp/hoge"にアクセスして"You entered: hoge"が得られる事を確認する