■ 커스텀 바인딩 사용하기

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


TestSolution.zip


[TestServer 프로젝트]

 

TestCustomBinding.cs

 

 

using System.ServiceModel.Channels;

 

namespace TestServer

{

    /// <summary>

    /// 테스트 커스텀 바인딩

    /// </summary>

    public class TestCustomBinding : Binding

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

 

        #region Field

 

        /// <summary>

        /// 트랜스포트

        /// </summary>

        private HttpTransportBindingElement transport;

 

        /// <summary>

        /// 인코딩

        /// </summary>

        private BinaryMessageEncodingBindingElement encoding;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

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

 

        #region 스킴 - Scheme

 

        /// <summary>

        /// 스킴

        /// </summary>

        public override string Scheme

        {

            get

            {

                return this.transport.Scheme;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

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

 

        #region 생성자 - TestCustomBinding()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public TestCustomBinding() : base()

        {

            this.transport = new HttpTransportBindingElement();

            this.encoding  = new BinaryMessageEncodingBindingElement();

        }

 

        #endregion

 

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

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

 

        #region 바인딩 엘리먼트 컬렉션 생성하기 - CreateBindingElements()

 

        /// <summary>

        /// 바인딩 엘리먼트 컬렉션 생성하기

        /// </summary>

        /// <returns>바인딩 엘리먼트 컬렉션</returns>

        public override BindingElementCollection CreateBindingElements()

        {

            BindingElementCollection collection = new BindingElementCollection();

 

            collection.Add(this.encoding);

            collection.Add(this.transport);

 

            return collection;

        }

 

        #endregion

    }

}

 

 

TestCustomBindingCollectionElement.cs

 

 

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Configuration;

using System.ServiceModel.Channels;

using System.ServiceModel.Configuration;

 

namespace TestServer

{

    /// <summary>

    /// 테스트 커스텀 바인딩 컬렉션 엘리먼트

    /// </summary>

    public class TestCustomBindingCollectionElement : BindingCollectionElement

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

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

 

        #region 바인딩 타입 - BindingType

 

        /// <summary>

        /// 바인딩 타입

        /// </summary>

        public override Type BindingType

        {

            get

            {

                return typeof(TestCustomBinding);

            }

        }

 

        #endregion

        #region 구성 바인딩 컬렉션 - ConfiguredBindings

 

        /// <summary>

        /// 구성 바인딩 컬렉션

        /// </summary>

        public override ReadOnlyCollection<IBindingConfigurationElement> ConfiguredBindings

        {

            get

            {

                return new ReadOnlyCollection<IBindingConfigurationElement>(new List<IBindingConfigurationElement>());

            }

        }

 

        #endregion

 

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

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

 

        #region 키 포함 여부 구하기 - ContainsKey(name)

 

        /// <summary>

        /// 키 포함 여부 구하기

        /// </summary>

        /// <param name="name">명칭</param>

        /// <returns>키 포함 여부</returns>

        public override bool ContainsKey(string name)

        {

            throw new NotImplementedException();

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 디폴트 구하기 - GetDefault()

 

        /// <summary>

        /// 디폴트 구하기

        /// </summary>

        /// <returns>바인딩</returns>

        protected override Binding GetDefault()

        {

            return new TestCustomBinding();

        }

 

        #endregion

        #region 추가 시도하기 - TryAdd(name, binding, configuration)

 

        /// <summary>

        /// 추가 시도하기

        /// </summary>

        /// <param name="name">명칭</param>

        /// <param name="binding">바인딩</param>

        /// <param name="configuration">구성</param>

        /// <returns>처리 결과</returns>

        protected override bool TryAdd(string name, Binding binding, Configuration configuration)

        {

            throw new NotImplementedException();

        }

 

        #endregion

    }

}

 

 

App.config

 

 

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

<configuration>

    <startup>

        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

    </startup>

    <system.serviceModel>

        <behaviors>

            <serviceBehaviors>

                <behavior name="mexBehavior">

                    <serviceMetadata httpGetEnabled="true" />

                    <serviceDebug includeExceptionDetailInFaults="true" />

                </behavior>

            </serviceBehaviors>

        </behaviors>

        <bindings>

            <customBinding>

                <binding name="testCustomHttp"

                    sendTimeout="00:10:00"

                    receiveTimeout="00:10:00">

                    <binaryMessageEncoding />

                    <httpTransport maxReceivedMessageSize="1262485504" />

                </binding>

            </customBinding>

            <netTcpBinding>

                <binding name="netTcp"

                    sendTimeout="00:10:00"

                    receiveTimeout="00:10:00"

                    maxReceivedMessageSize="1262485504" />

            </netTcpBinding>

        </bindings>

        <services>

            <service name="TestServer.HelloService" behaviorConfiguration="mexBehavior">

                <endpoint

                    address="HelloService"

                    binding="customBinding"

                    bindingConfiguration="testCustomHttp"

                    contract="TestServer.IHelloService" />

                <endpoint

                    address="HelloService"

                    binding="netTcpBinding"

                    bindingConfiguration="netTcp"

                    contract="TestServer.IHelloService" />

                <host>

                    <baseAddresses>

                        <add baseAddress="http://localhost:8080" />

                        <add baseAddress="net.tcp://localhost:8090" />

                    </baseAddresses>

                </host>

            </service>

        </services>

        <extensions>

            <bindingExtensions>

                <add name="testCustomBinding"

                    type="TestServer.TestCustomBindingCollectionElement, CustomBinding, Version=1.0.0.0, Culture=neutral,

                        PublicKeyToken=null" />

            </bindingExtensions>

        </extensions>

    </system.serviceModel>

</configuration>

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요