いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

WCF入門-003 (簡単なサンプル, バインディングにNetTcpBindingを利用)


前回までは、バインディングにBasicHttpBinding、つまりHTTPトランスポート
を使用していましたが、今回はTCPトランスポートを利用しています。
TCPトランスポートを利用するには、NetTcpBindingを利用。


WCFには、このほかにも

  • MSMQ
  • 名前付きパイプ

などのバインディングが利用可能。*1


以下、アプリケーション構成ファイル。
TCPで通信する場合、アドレスのスキームをnet.tcpに変更する。
てか、この変更のみで前回と同じサンプルがTCPで通信するようになる。凄いな〜WCF
恐らく、他のバインディングを利用した場合でも同様な筈。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="Gsf.Samples.WCF.MyService" behaviorConfiguration="MyServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8082/WCFSample003"/>
          </baseAddresses>
        </host>
        <endpoint address="SayHello" binding="netTcpBinding" contract="Gsf.Samples.WCF.IMyService" />
      </service>
    </services>
  </system.serviceModel>
</configuration>


以下、サービスインターフェースとサービスクラス。

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;

namespace Gsf.Samples.WCF
{
    [ServiceContract(Namespace="http://Gsf.Samples.WCF/")]
    public interface IMyService
    {
        [OperationContract]
        string SayHello(string name);
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Gsf.Samples.WCF
{
    public class MyService : IMyService
    {
        public string SayHello(string name)
        {
            Console.WriteLine("[CALL] MyService.SayHello()");
            return string.Format("Hello. '{0}'!", name);
        }
    }
}


ホストクラス。

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;

namespace Gsf.Samples.WCF
{
    class Program
    {
        static void Main()
        {
            //
            // サービスホストを構築する.
            //   エンドポイントアドレスなどの設定は、アプリケーション構成ファイルから読み込まれる。
            //
            using (ServiceHost host = new ServiceHost(typeof(MyService)))
            {
                host.Open();

                Console.WriteLine("サービスを開始しました。");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();

                host.Close();
            }
        }
    }
}


クライアント。

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;

namespace Gsf.Samples.WCF
{
    class Program
    {
        static void Main()
        {
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));

            Console.WriteLine(
                new ChannelFactory<IMyService>(new NetTcpBinding()).CreateChannel(
                    new EndpointAddress("net.tcp://localhost:8082/WCFSample003/SayHello")).SayHello("WCF Study")
                );

            Console.WriteLine("[CLIENT] Press any key to exit...");
            Console.ReadKey();
        }
    }
}


実行すると以下のようになります。


サンプルは以下の場所にアップしてあります。
https://sites.google.com/site/gsfzero1/Home/WCF-Sample-003.zip?attredirects=0&d=1


================================
過去の記事については、以下のページからご参照下さい。

*1:順次サンプル記述していくこと。>自分