WCF on .NET Core

.NET Core でも WCF を使うことはできる。提供されているのはクライアントライブラリだけなので、.NET Core で動く WCF サービスを作ることはできないけど。

試しに TCP でやってみた。WCF サービスは .NET Framework で作るしかない。

using System;
using System.Runtime.Serialization;
using System.ServiceModel;

namespace DotNetFrameworkWcf
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = ".NET Framework WCF";

            var host = new ServiceHost(typeof(BookService));
            host.AddServiceEndpoint(
                typeof(IBookService),
                new NetTcpBinding(),
                "net.tcp://localhost:8080/BookService");
            host.Open();

            Console.WriteLine("Enter で終了");
            Console.ReadLine();

            host.Close();
        }
    }

    [DataContract]
    public class Book
    {
        [DataMember]
        public string Author { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public decimal Price { get; set; }
    }

    [ServiceContract]
    public interface IBookService
    {
        [OperationContract]
        Book Echo(Book book);
    }

    public class BookService : IBookService
    {
        public Book Echo(Book book)
        {
            Console.WriteLine($"{book.Author} - {book.Title} \\{book.Price}");
            return book;
        }
    }
}

.NET Core のコンソールアプリケーションを新規作成し、System.ServiceModel.NetTcp パッケージを追加。

www.nuget.org

WCF サービスを呼び出してみる。ChannelFactory の使い方がちょっと違うけど、悩むほどではない。

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace DotNetCoreWcf
{
    using DotNetFrameworkWcf;

    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = ".NET Core WCF";

            var factory = new ChannelFactory<IBookService>(
                new NetTcpBinding(),
                new EndpointAddress("net.tcp://localhost:8080/BookService"));
            var channel = factory.CreateChannel();

            var result = channel.Echo(new Book()
            {
                Author = "赤坂アカ",
                Title = "かぐや様は告らせたい 7",
                Price = 500,
            });
            Console.WriteLine($"{result.Author} - {result.Title} \\{result.Price}");

            Console.WriteLine("Enter で終了");
            Console.ReadLine();

            ((IChannel)channel).Close();
        }
    }
}

namespace DotNetFrameworkWcf
{
    [DataContract]
    public class Book
    {
        [DataMember]
        public string Author { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public decimal Price { get; set; }
    }

    [ServiceContract]
    public interface IBookService
    {
        [OperationContract]
        Book Echo(Book book);
    }
}

先にサービスを起動しておいて、クライアントを実行。

f:id:griefworker:20171124173150p:plain

.NET Framework で作った WCF サービスを、.NET Core から問題なく呼び出せた。.NET Core で WCF サービスは需要無いだろうから、今後もパッケージが提供されることはないんだろうな。

実際、自分も必要としているのはクライアント側だけで、.NET Core で WCF サービスを作りたいとは思わない。.NET Core で動かしている ASP.NET Core アプリケーションから、レガシーな WCF サービスを呼び出せれば十分。