.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のサーバ側になる。



CoreWCF のクライアント側として、.NET 6.0 の「Windowsフォームアプリ」を作成。
※WCFのクライアントは「Windowsフォームアプリ」で作られることが多い気がする。



Windowsフォームアプリから CoreWCFサーバへ接続
CoreWCFサーバ側プロジェクトをデバッグ実行すると、コンソールが立ち上がり接続先情報が表示される。

CoreWCFサーバのコンソールに表示された接続先URLを、ブラウザで表示した際の画面は、.Net Frameworkで実装したWCFと変わらない。

Windowsフォームプロジェクト(CoreWCFクライアント)を右クリックし、「追加 > サービス参照」を選択。

「WCF Web Service」を選択し「次へ」をクリック。

URLに CoreWCFの single file URLを入力し「移動」をクリック。


サービスが検出されたことを確認し「次へ」をクリック。

特に変更せず「次へ」をクリック。

特に変更せず「完了」をクリック。

「閉じる」をクリック。

Connected Servicesフォルダが追加される。

CoreWCFのサービスを呼び出す処理をクライアント側に実装
Windowsフォーム画面のボタンクリック処理に、CoreWCFのクライアントからAPIをキックする処理を追加。
1 2 3 4 5 |
var client = new ServiceReference1.ServiceClient(); var result = await client.GetDataAsync(12345); MessageBox.Show(result); |

Windowsフォームアプリを起動し、画面のボタンクリックすると、CoreWCF APIを通した結果がメッセージボックスに表示される。

CoreWCFサーバ側のインターフェース変更をクライアント側へ反映する
CoreWCFへ接続した後、CoreWCFサーバ側のインターフェース定義が変更した場合、CoreWCFクライアント側のインターフェース定義は自動更新されないので、手動で更新し、CoreWCFサーバ側とクライアント側のインターフェース定義を合わせる必要があります。
CoreWCFサーバ側をデバッグ実行し、CoreWCFクライアントから接続可能な状態にした上で、下記手順を行うと同期できます。
Windowsフォームプロジェクト(CoreWCFクライアント)を右クリックし、「追加 > 接続済みサービス」を選択。

対象サービスの「編集」を選択。

特に変更せず「移動」をクリック。
後は初回サービス参照時と同じ。

WCFと Core WCFには、ほとんど違いがないらしい。
CoreWCFは .NET Framework 4.8 上でも実装できる。
ソースコードはGitHubで公開しています。

CoreWCFプロジェクトを .NET Framework 4.8 で作成した場合、Microsoft.AspNetCore Nugetパッケージもインストールされるので、 .NET Framework 4.8 ベースで開発作業は進めれらるけど、CoreWCFの実行環境は .Net Coreベースの AspNetCoreになるみたい。

.NET Framework 4.8 で実装した CoreWCFプロジェクトを実行した場合。 .NET 6.0 で実装した CoreWCFプロジェクトを実行した場合と変わらない。
CoreWCFを .NET Framework 4.8 で開発する人はいないと思いたい、、、


参考サイト
Core WCF プロジェクトサイト
GitHubに CoreWCF開発リポジトリがある。インストール手順などが分かる。
https://github.com/CoreWCF/CoreWCF
コメント