첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

[C#/WCF] WCF 사용하기

C#/WCF 2017. 5. 9. 07:05

■ WCF 사용하기

----------------------------------------------------------------------------------------------------

※ Visual Studio 실행시 관리자 권한으로 실행해야 한다.

 

[서버 실행 프로그램]

 

 

[클라이언트 실행 프로그램]

 


TestSolution.zip


[TestWCFLibrary 프로젝트]

 

IHelloWorld.cs

 

 

using System.ServiceModel;

 

namespace TestWCFLibrary

{

    /// <summary>

    /// Hello World 인터페이스

    /// </summary>

    [ServiceContract]

    public interface IHelloWorld

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

 

        #region Hello 말하기 - SayHello()

 

        /// <summary>

        /// Hello 말하기

        /// </summary>

        /// <returns>Hello</returns>

        [OperationContract]

        string SayHello();

 

        #endregion

    }

}

 

 

HelloWorldService.cs

 

 

using System;

using System.Text;

 

namespace TestWCFLibrary

{

    /// <summary>

    /// Hello World 서비스

    /// </summary>

    public class HelloWorldService : IHelloWorld

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region Hello 말하기 - SayHello()

 

        /// <summary>

        /// Hello 말하기

        /// </summary>

        /// <returns>Hello</returns>

        public string SayHello()

        {

            Console.WriteLine("클라이언트 요청 처리를 시작합니다.");

 

            StringBuilder stringBuilder = new StringBuilder();

 

            for(int i = 0; i < 50000; i++)

            {

                stringBuilder.Append("0123456789");

            }

 

            Console.WriteLine("클라이언트 요청 처리를 종료합니다.");

 

            return stringBuilder.ToString();

        }

 

        #endregion

    }

}

 

 

[TestWCFServer 프로젝트]

 

App.config

 

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <system.serviceModel>

        <bindings>

            <basicHttpBinding>

                <binding name="HelloWorldHttpBinding"

                    messageEncoding="Mtom"

                    maxReceivedMessageSize="2147483647"

                    maxBufferSize="2147483647" />

            </basicHttpBinding>

            <netTcpBinding>

                <binding name="HelloWorldTcpBinding"

                    maxReceivedMessageSize="2147483647"

                    maxBufferSize="2147483647">

                    <reliableSession enabled="false" ordered="true"/>

                    <security mode="None" />

                </binding>

            </netTcpBinding>

        </bindings>

        <services>

            <service name="TestWCFLibrary.HelloWorldService">

                <endpoint address="" binding="basicHttpBinding" bindingConfiguration="HelloWorldHttpBinding"

                    contract="TestWCFLibrary.IHelloWorld" />

                <endpoint address="" binding="netTcpBinding" bindingConfiguration="HelloWorldTcpBinding"

                    contract="TestWCFLibrary.IHelloWorld" />

                <host>

                    <baseAddresses>

                        <add baseAddress="http://192.168.0.2/wcf/helloworldservice" />

                        <add baseAddress="net.tcp://192.168.0.2/wcf/helloworldservice" />

                    </baseAddresses>

                </host>

            </service>

        </services>

    </system.serviceModel>

</configuration>

 

 

Program.cs

 

 

using System;

using System.ServiceModel;

 

using TestWCFLibrary;

 

namespace TestWCFServer

{

    /// <summary>

    /// 프로그램

    /// </summary>

    public class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            ServiceHost serviceHost = new ServiceHost(typeof(HelloWorldService));

 

            serviceHost.Open();

 

            Console.WriteLine("서비스 중단을 위해 아무 키나 누르시기 바랍니다...");

 

            Console.ReadKey(true);

 

            serviceHost.Close();

        }

 

        #endregion

    }

}

 

 

[TestWCFClient 프로젝트]

 

App.config

 

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <system.serviceModel>

        <bindings>

            <basicHttpBinding>

                <binding name="HelloWorldHttpBinding"

                    messageEncoding="Mtom"

                    maxReceivedMessageSize="2147483647"

                    maxBufferSize="2147483647" />

            </basicHttpBinding>

            <netTcpBinding>

                <binding name="HelloWorldTcpBinding"

                    maxReceivedMessageSize="2147483647"

                    maxBufferSize="2147483647">

                    <reliableSession enabled="false" ordered="true"/>

                    <security mode="None" />

                </binding>

            </netTcpBinding>

        </bindings>

        <client>

            <endpoint name="HttpHelloWorld"

                contract="TestWCFLibrary.IHelloWorld"

                binding="basicHttpBinding"

                bindingConfiguration="HelloWorldHttpBinding"

                address="http://192.168.0.2/wcf/helloworldservice" />

            <endpoint name="TcpHelloWorld"

                contract="TestWCFLibrary.IHelloWorld"

                binding="netTcpBinding"

                bindingConfiguration="HelloWorldTcpBinding"

                address="net.tcp://192.168.0.2/wcf/helloworldservice" />

        </client>

    </system.serviceModel>

</configuration>

 

 

Program.cs

 

 

using System;

using System.Diagnostics;

using System.ServiceModel;

 

using TestWCFLibrary;

 

namespace TestWCFClient

{

    /// <summary>

    /// 프로그램

    /// </summary>

    public class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Stopwatch stopWatch = new Stopwatch();

 

            Console.WriteLine("HTTP 통신을 시작합니다.");

 

            stopWatch.Start();

 

            CommunicateHTTP();

 

            stopWatch.Stop();

 

            Console.WriteLine("HTTP 통신을 종료합니다.");

 

            Console.WriteLine("HTTP 통신 시간 : " + stopWatch.Elapsed.ToString());

 

            stopWatch.Reset();

 

            Console.WriteLine("TCP 통신을 시작합니다.");

 

            stopWatch.Start();

 

            CommunicateTCP();

 

            stopWatch.Stop();

 

            Console.WriteLine("TCP 통신을 종료합니다.");

 

            Console.WriteLine("TCP 통신 시간 : " + stopWatch.Elapsed.ToString());

 

            Console.ReadKey();

        }

 

        #endregion

 

        #region HTTP 통신하기 - CommunicateHTTP()

 

        /// <summary>

        /// HTTP 통신하기

        /// </summary>

        private static void CommunicateHTTP()

        {

            ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>("HttpHelloWorld");

 

            IHelloWorld helloWorld = channelFactory.CreateChannel();

 

            for(int i = 0; i < 100; i++)

            {

                string result = helloWorld.SayHello();

            }

 

            (helloWorld as IDisposable).Dispose();

        }

 

        #endregion

        #region TCP 통신하기 - CommunicateTCP()

 

        /// <summary>

        /// TCP 통신하기

        /// </summary>

        private static void CommunicateTCP()

        {

            ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>("TcpHelloWorld");

 

            IHelloWorld helloWorld = channelFactory.CreateChannel();

 

            for(int i = 0; i < 100; i++)

            {

                string result = helloWorld.SayHello();

            }

 

            (helloWorld as IDisposable).Dispose();

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요