LoginSignup
4
3

More than 5 years have passed since last update.

LinuxでOWINアプリケーションを動かす(Debian/F#編)

Last updated at Posted at 2015-07-19

環境

  • Debian 8.1
  • .NET Framework 4.6
  • Visual Studio 2015 RC
  • Visual Studio 2013

F#でOWINアプリケーションを作成する

ソースはここに置いてあります。

1. プロジェクト作成

Visual Studio 2015のバグでF#のテンプレートが壊滅しているので、プロジェクト新規作成だけはVisual Studio 2013で行います。
正式版で修正されました。

プロジェクト新規作成ウィザードからF#のコンソール アプリケーションを作成しましょう。

作成したプロジェクトをVisual Studio 2015で開き直します。
(新しいのが使いたかっただけなので、2013のまま進めても何も問題ありません)

2. 参照の追加

下記のDLLが必要なので、それぞれNuGetから取得します。

  • Microsoft.Owin
  • Microsoft.Owin.Host.HttpListener
  • Microsoft.Owin.Hosting
  • Owin

めんどくさいですが、下記のようにパッケージ マネージャー コンソールで一つずつコマンドを叩きます。
(yumとかapt-getみたいに複数まとめてできないのでしょうか・・)
PM> Install-Package Owin

3. ソース作成

F#でOWINアプリケーションを作成する場合、最低限Program.fsStartup.fsの2つのファイルが必要です。
なお、Program.fsは一番下に配置してください(Alt+カーソル上下で動かせます)。

Program.fs
open System
open Microsoft.Owin.Hosting
open Startup

[<EntryPoint>]
let main _ =
    let baseAddress = "http://*:9000"
    use app = WebApp.Start<Startup>(baseAddress)

    Console.WriteLine("Listening at {0}", baseAddress)
    Console.WriteLine("Press any key to stop")

    Console.ReadLine() |> ignore

    0
Startup.fs
module Startup

open System
open global.Owin

    type Startup() =

        member this.Configuration(app:IAppBuilder) =

            let os = "<h1>OWIN on " + Environment.OSVersion.ToString() + "</h1>"
            app.Run(fun c -> c.Response.WriteAsync(os))

ひとまずローカルで起動してみます。
bin/Debug(Release)の下にexeが生成されているので管理者で実行します。
(baseAddressの所で*を指定していると、実行にAdmin権限が必要となります)

ブラウザでhttp://localhost:9000 にアクセスすると、めでたく表示されました。

サーバー(Debian)にMonoをインストール

http://www.mono-project.com/docs/getting-started/install/linux/
上記の手順に従ってMonoをインストールします。

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
$ echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
# Debianの場合はこれも必要
$ echo "deb http://download.mono-project.com/repo/debian wheezy-libjpeg62-compat main" | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list
$ sudo apt-get update
$ sudo apt-get -y install mono-devel

インストールできました。

$ mono -V
Mono JIT compiler version 4.0.2 (Stable 4.0.2.5/c99aa0c Wed Jun 24 10:04:37 UTC 2015)

アプリケーションを起動

Monoがインストールできれば、あとは作成したexeをサーバーに置いてコマンドを叩くだけです。
bin/Debug(Release)フォルダの中身一式をアップロードします。

exeを実行します。

$ mono fsharp_owin_sample.exe

ブラウザで表示できれば成功です。

参考資料

Running ASP.NET on a Raspberry Pi with Mono and OWIN
swlaschin/Railway-Oriented-Programming-Example

4
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3