.Net 6.0 のWindowsフォームアプリと、Core WCF とを接続して通信するサンプルを作成しました。WCFの .NET 6.0(.Net Core)版が Core WCF です。
ソースコードはGitHubで公開しています。
開発ツール インストール
Core WCFは .NET 6.0以降で稼働し、.NET 6.0は VisualStudio2022以降じゃないと開発できないので、事前に VisualStudio2022のインストールが必要。
Core WCFの開発には、別途、Core WCF の VisualStudioプロジェクトテンプレートのインストールが必要。
CoreWCF.Templatesの installコマンドをコマンドプロンプトで実行することで、Core WCF の VisualStudioプロジェクトテンプレートをインストールできる。
1 2 3 |
> dotnet new --install CoreWCF.Templates |
CoreWCF.Templatesをインストールすると、VisualStudio2022の「新しいプロジェクトの作成」で「CoreWCF」プロジェクトを選べるようになる。
「CoreWCF」プロジェクトが Core WCFのサーバ側になる。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-89-1024x681.png)
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-90-1024x681.png)
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-91-1024x681.png)
CoreWCF のクライアント側として、.NET 6.0 の「Windowsフォームアプリ」を作成。
※WCFのクライアントは「Windowsフォームアプリ」で作られることが多い気がする。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-92-1024x681.png)
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-93-1024x681.png)
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-94-1024x681.png)
Windowsフォームアプリから CoreWCFサーバへ接続
CoreWCFサーバ側プロジェクトをデバッグ実行すると、コンソールが立ち上がり接続先情報が表示される。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-95-1024x344.png)
CoreWCFサーバのコンソールに表示された接続先URLを、ブラウザで表示した際の画面は、.Net Frameworkで実装したWCFと変わらない。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-96-1024x668.png)
Windowsフォームプロジェクト(CoreWCFクライアント)を右クリックし、「追加 > サービス参照」を選択。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-97-1024x718.png)
「WCF Web Service」を選択し「次へ」をクリック。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-98-1024x718.png)
URLに CoreWCFの single file URLを入力し「移動」をクリック。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-99-1024x718.png)
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-100-1024x301.png)
サービスが検出されたことを確認し「次へ」をクリック。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-101-1024x718.png)
特に変更せず「次へ」をクリック。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-102-1024x718.png)
特に変更せず「完了」をクリック。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-103-1024x719.png)
「閉じる」をクリック。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-104-1024x718.png)
Connected Servicesフォルダが追加される。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-105-1024x496.png)
CoreWCFのサービスを呼び出す処理をクライアント側に実装
Windowsフォーム画面のボタンクリック処理に、CoreWCFのクライアントからAPIをキックする処理を追加。
1 2 3 4 5 |
var client = new ServiceReference1.ServiceClient(); var result = await client.GetDataAsync(12345); MessageBox.Show(result); |
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-106.png)
Windowsフォームアプリを起動し、画面のボタンクリックすると、CoreWCF APIを通した結果がメッセージボックスに表示される。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-107.png)
CoreWCFサーバ側のインターフェース変更をクライアント側へ反映する
CoreWCFへ接続した後、CoreWCFサーバ側のインターフェース定義が変更した場合、CoreWCFクライアント側のインターフェース定義は自動更新されないので、手動で更新し、CoreWCFサーバ側とクライアント側のインターフェース定義を合わせる必要があります。
CoreWCFサーバ側をデバッグ実行し、CoreWCFクライアントから接続可能な状態にした上で、下記手順を行うと同期できます。
Windowsフォームプロジェクト(CoreWCFクライアント)を右クリックし、「追加 > 接続済みサービス」を選択。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-116.png)
対象サービスの「編集」を選択。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-117.png)
特に変更せず「移動」をクリック。
後は初回サービス参照時と同じ。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-118.png)
WCFと Core WCFには、ほとんど違いがないらしい。
CoreWCFは .NET Framework 4.8 上でも実装できる。
ソースコードはGitHubで公開しています。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-108-1024x682.png)
CoreWCFプロジェクトを .NET Framework 4.8 で作成した場合、Microsoft.AspNetCore Nugetパッケージもインストールされるので、 .NET Framework 4.8 ベースで開発作業は進めれらるけど、CoreWCFの実行環境は .Net Coreベースの AspNetCoreになるみたい。
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-109-1024x454.png)
.NET Framework 4.8 で実装した CoreWCFプロジェクトを実行した場合。 .NET 6.0 で実装した CoreWCFプロジェクトを実行した場合と変わらない。
CoreWCFを .NET Framework 4.8 で開発する人はいないと思いたい、、、
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-110-1024x324.png)
![](https://blog.unikktle.com/wp-content/uploads/2023/01/image-111-1024x624.png)
参考サイト
Core WCF プロジェクトサイト
GitHubに CoreWCF開発リポジトリがある。インストール手順などが分かる。
https://github.com/CoreWCF/CoreWCF
コメント