첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

[HelloWorldLibrary 프로젝트]

※ System.ServiceModel 참조를 추가한다.

 

▶ IHelloWorld.cs

using System.ServiceModel;

namespace HelloWorldLibrary
{
    /// <summary>
    /// Hello World 인터페이스
    /// </summary>
    [ServiceContract]
    public interface IHelloWorld
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region Hello 말하기 - SayHello()

        /// <summary>
        /// Hello 말하기
        /// </summary>
        /// <returns>Hello</returns>
        [OperationContract]
        string SayHello();

        #endregion
    }
}

 

728x90

 

▶ HelloWorldService.cs

namespace HelloWorldLibrary
{
    /// <summary>
    /// Hello World 서비스
    /// </summary>
    public class HelloWorldService : IHelloWorld
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Hello 말하기 - SayHello()

        /// <summary>
        /// Hello 말하기
        /// </summary>
        /// <returns>Hello</returns>
        public string SayHello()
        {
            return "Hello World!";
        }

        #endregion
    }
}

 

300x250

 

[HelloWorldServer 프로젝트]

※ System.ServiceModel 및 HelloWorldLibrary 참조를 추가한다.

 

▶ App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="MtomBasicHttpBinding" messageEncoding="Mtom" />
            </basicHttpBinding>
        </bindings>
        <services>
            <service name="HelloWorldLibrary.HelloWorldService">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost/wcf/helloworldservice"    />
                        <add baseAddress="net.tcp://localhost/wcf/helloworldservice" />
                    </baseAddresses>
                </host>
                <endpoint
                    contract="HelloWorldLibrary.IHelloWorld"
                    binding="basicHttpBinding"
                    bindingConfiguration="MtomBasicHttpBinding"
                    address="" />
                <endpoint
                    contract="HelloWorldLibrary.IHelloWorld"
                    binding="netTcpBinding"
                    address="" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

 

▶ Program.cs

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

using HelloWorldLibrary;

namespace HelloWorldServer
{
    /// <summary>
    /// 프로그램
    /// </summary>
    public class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

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

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            ServiceHost serviceHost = new ServiceHost(typeof(HelloWorldService));


            foreach(ServiceEndpoint serviceEndpoint in serviceHost.Description.Endpoints)
            {
                BasicHttpBinding basicHttpBinding = serviceEndpoint.Binding as BasicHttpBinding;

                if(basicHttpBinding != null)
                {
                    basicHttpBinding.MessageEncoding = WSMessageEncoding.Text;
                }
            }


            serviceHost.Open();

            Console.WriteLine("서비스 중단을 위해 아무 키나 누르세요...");

            Console.ReadKey(true);

            serviceHost.Close();
        }

        #endregion
    }
}

 

[HelloWorldConsole 프로젝트]

※ System.ServiceModel 및 HelloWorldLibrary 참조를 추가한다.

 

▶ App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="MtomBasicHttpBinding" messageEncoding="Mtom" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint name="HttpHelloWorld"
                contract="HelloWorldLibrary.IHelloWorld"
                binding="basicHttpBinding"
                bindingConfiguration="MtomBasicHttpBinding"
                address="http://localhost/wcf/helloworldservice" />
            <endpoint name="TcpHelloWorld"
                contract="HelloWorldLibrary.IHelloWorld"
                binding="netTcpBinding"
                address="net.tcp://localhost/wcf/helloworldservice" />
        </client>
    </system.serviceModel>
</configuration>

 

▶ Program.cs

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

using HelloWorldLibrary;

namespace HelloWorldConsole
{
    /// <summary>
    /// 프로그램
    /// </summary>
    public class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

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

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            CommunicateHTTP();

            CommunicateTCP();
        }

        #endregion

        #region HTTP 통신하기 - CommunicateHTTP()

        /// <summary>
        /// HTTP 통신하기
        /// </summary>
        private static void CommunicateHTTP()
        {
            ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>("HttpHelloWorld");


            BasicHttpBinding basicHttpBinding = channelFactory.Endpoint.Binding as BasicHttpBinding;

            if(basicHttpBinding != null)
            {
                basicHttpBinding.MessageEncoding = WSMessageEncoding.Text;
            }


            IHelloWorld helloWorld = channelFactory.CreateChannel();

            string result = helloWorld.SayHello();

            (helloWorld as IDisposable).Dispose();

            Console.WriteLine(result);
        }

        #endregion

        #region TCP 통신하기 - CommunicateTCP()

        /// <summary>
        /// TCP 통신하기
        /// </summary>
        private static void CommunicateTCP()
        {
            ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>("TcpHelloWorld");

            IHelloWorld helloWorld = channelFactory.CreateChannel();

            string result = helloWorld.SayHello();

            (helloWorld as IDisposable).Dispose();

            Console.WriteLine(result);
        }

        #endregion
    }
}

 

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요