OSDN Git Service

SSTサービスが切断されてからビュアーを再度起動したときに例外が発生してたミスを修正。
[strokestylet/CsWin10Desktop3.git] / StrokeStyleT / Program.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Linq;
5 using System.ServiceModel;
6 using System.Windows.Forms;
7
8 namespace SST
9 {
10         static class Program
11         {
12                 [STAThread]
13                 static void Main( string[] args )
14                 {
15                         Application.EnableVisualStyles();
16                         Application.SetCompatibleTextRenderingDefault( false );
17
18                         // アプリを生成する。ビュアーモードかどうかの判定も行う。
19                         var app = new StrokeStyleT();
20
21                         if( StrokeStyleT.ビュアーモードである )
22                         {
23                                 #region " (A) ビュアーモードでの起動(WCFサービスあり)"
24                                 //----------------
25                                 string serviceUri = "net.pipe://localhost/StrokeStyleT";
26                                 string endPointName = "Viewer";
27                                 string endPointUri = $"{serviceUri}/{endPointName}";
28
29                                 // アプリのサービスホストを生成する。
30                                 var serviceHost = new ServiceHost( app, new Uri( serviceUri ) );
31
32                                 // 名前付きパイプにバインドしたエンドポイントをサービスホストへ追加する。
33                                 serviceHost.AddServiceEndpoint(
34                                         typeof( IStrokeStyleTService ),
35                                         new NetNamedPipeBinding( NetNamedPipeSecurityMode.None ),
36                                         endPointName );
37
38                                 // サービスの受付を開始する。
39                                 try
40                                 {
41                                         serviceHost.Open();
42                                 }
43                                 catch( AddressAlreadyInUseException )
44                                 {
45                                         MessageBox.Show( "同じアプリがすでに起動しています。多重起動はできません。", "SST Error", MessageBoxButtons.OK );
46                                         return;
47                                 }
48
49                                 // アプリを実行する。
50                                 try
51                                 {
52                                         app.Run();
53                                 }
54                                 finally
55                                 {
56                                         // サービスの受付を終了する。
57                                         serviceHost.Close();
58                                 }
59                                 //----------------
60                                 #endregion
61                         }
62                         else
63                         {
64                                 #region " (B) 通常の起動(WCFサービスなし)"
65                                 //----------------
66                                 // アプリを実行する。
67                                 app.Run();
68                                 //----------------
69                                 #endregion
70                         }
71                 }
72         }
73 }