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

TestSolution.zip
다운로드

[TestServer 프로젝트]

▶ ISimpleService.cs

using System.ServiceModel;

namespace TestServer
{
    /// <summary>
    /// 단순 서비스
    /// </summary>
    [ServiceContract]
    public interface ISimpleService
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 번호 증가하기 - IncreaseNumber()

        /// <summary>
        /// 번호 증가하기
        /// </summary>
        /// <returns>번호</returns>
        [OperationContract]
        int IncreaseNumber();

        #endregion
    }
}

 

728x90

 

▶ SimpleService.cs

using System.ServiceModel;

namespace TestServer
{
    /// <summary>
    /// 단순 서비스
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class SimpleService : ISimpleService
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 번호
        /// </summary>
        private int number;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 번호 증가하기 - IncreaseNumber()

        /// <summary>
        /// 번호 증가하기
        /// </summary>
        /// <returns>번호</returns>
        public int IncreaseNumber()
        {
            this.number = this.number + 1;

            return this.number;
        }

        #endregion
    }
}

 

300x250

 

▶ Program.cs

using System;
using System.ServiceModel;
 
namespace TestServer
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        ////////////////////////////////////////////////////////////////////////////////////////// Method
        //////////////////////////////////////////////////////////////////////////////// Static
        ////////////////////////////////////////////////////////////////////// Private
 
        #region 프로그램 시작하기 - Main()
 
        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            using(ServiceHost serviceHost = new ServiceHost(typeof(SimpleService)))
            {
                serviceHost.Open();
 
                Console.WriteLine("서버가 시작되었습니다 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
 
                Console.ReadKey(true);
            }
        }
 
        #endregion
    }
}

 

▶ App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="mexBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="TestServer.SimpleService" behaviorConfiguration="mexBehavior">
                <endpoint
                    address="SimpleService"
                    binding="netTcpBinding"
                    contract="TestServer.ISimpleService" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8080" />
                        <add baseAddress="net.tcp://localhost:8090" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

 

[TestClient 프로젝트]

▶ Program.cs

using System;

using TestClient.SimpleService;

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

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

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

            using(SimpleServiceClient client = new SimpleServiceClient("NetTcpBinding_ISimpleService"))
            {
                number = client.IncreaseNumber();

                Console.WriteLine("첫번째 호출 후 번호 = " + number);

                number = client.IncreaseNumber();

                Console.WriteLine("두번째 호출 후 번호 = " + number);

                number = client.IncreaseNumber();

                Console.WriteLine("세번째 호출 후 번호 = " + number);
            }
        }

        #endregion
    }
}

 

▶ App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_ISimpleService" />
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint name="NetTcpBinding_ISimpleService"
                address="net.tcp://localhost:8090/SimpleService"
                binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_ISimpleService"
                contract="SimpleService.ISimpleService" />
        </client>
    </system.serviceModel>
</configuration>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요