前提・実現したいこと
仮想マシン上で動作するセルフホスト方式のWCFサービスを自宅のPC(Windows10)上のWCFアプリケーションから呼び出したいのですが、掲題の通りです。
仮想マシンは、Google Cloud Platform上のMicrosoft, Windows Server, 2019 Datacenter, Server with Desktop Experience, x64 built on 20190910
です。無料で利用しています。ファイアウォールの設定は以下の通りです。
問題点
エラーが発生し、クライアントからサーバーにアクセスできない。
よろしくお願い致します。
試してみたこと
自宅PCでサーバーを実行、仮想マシンでクライアントを実行するとうまく動作した。
(自宅のルーターの設定で、ポートを開けた状態でテスト)
クライアント画面
クライアント結果
サーバーコンソール
該当のソースコード
①サービスのインターフェイス
C#
1using System.ServiceModel; 2 3namespace WcfNetTcpBinding 4{ 5 [ServiceContract] 6 public interface ITestService 7 { 8 [OperationContract] 9 string Upload(string s); 10 } 11}
②サーバーアプリケーション(コンソールアプリ)
C#
1using System; 2using System.ServiceModel; 3 4namespace WcfNetTcpBinding 5{ 6 class Program 7 { 8 static ServiceHost host; 9 static void Main(string[] args) 10 { 11 Console.CancelKeyPress += new ConsoleCancelEventHandler(Ctrl_C_Pressed); 12 13 host = new ServiceHost(typeof(TestServiceClass)); 14 NetTcpBinding binding = new NetTcpBinding(); 15 binding.Security.Mode = SecurityMode.None; 16 17 host.AddServiceEndpoint(typeof(ITestService), binding, new Uri(string.Format("net.tcp://{0}:{1}/TestService", Properties.Settings.Default["Address1"], Properties.Settings.Default["Port1"]))); 18 host.Open(); 19 Console.WriteLine("started"); 20 Console.ReadKey(); 21 22 } 23 24 public class TestServiceClass : ITestService 25 { 26 public string Upload(string s) 27 { 28 Console.WriteLine(s); 29 return "hello" + "--" + s; 30 } 31 } 32 33 protected static void Ctrl_C_Pressed(object sender, ConsoleCancelEventArgs args) 34 { 35 host.Close(); 36 Environment.Exit(0); 37 } 38 } 39} 40
③クライアントアプリケーション
C#(Client)
1using System; 2using System.ServiceModel; 3using System.Windows.Forms; 4using WcfNetTcpBinding; 5 6 7namespace WcfNetTcpBindingClient 8{ 9 public partial class Form1 : Form 10 { 11 ITestService idd; 12 public Form1() 13 { 14 InitializeComponent(); 15 } 16 17 private void Form1_Load(object sender, EventArgs e) 18 { 19 NetTcpBinding binding = new NetTcpBinding(); 20 binding.Security.Mode = SecurityMode.None; 21 EndpointAddress addr = new EndpointAddress("net.tcp://" + Properties.Settings.Default["Address1"] + ":" + Properties.Settings.Default["Port1"] + "/TestService"); 22 ChannelFactory<ITestService> chn = new ChannelFactory<ITestService>(binding, addr); 23 idd = chn.CreateChannel(); 24 25 } 26 27 private void Button1_Click(object sender, EventArgs e) 28 { 29 string y = idd.Upload(textBox1.Text); 30 MessageBox.Show(y); 31 } 32 } 33} 34
参考URL - バインドの種類(通信プロトコル)について
WCFアプリケーション間のインターネットを経由した通信を想定しているので
NetTcpBindingを使用しています。(問題ありますか?)
NetTcpBinding <netTcpBinding> WCF アプリケーション間でのコンピューター間通信に適した、セキュリティで保護され、最適化されたバインド。
https://docs.microsoft.com/ja-jp/dotnet/framework/wcf/system-provided-bindings
環境
Microsoft Windows 10 Pro (Version:1903, OS build:18362.356)
Microsoft Visual Studio Community 2017(Version 15.9.11)
Microsoft .NET Framework(Version 4.8.03752)

あなたの回答
tips
プレビュー