探索プロキシを実装する方法

このトピックでは、探索プロキシの実装方法について説明します。Windows Communication Foundation (WCF) の探索機能詳細情報、「WCF Discovery の概要」を参照してください。探索プロキシを実装するには、抽象クラス DiscoveryProxy を拡張するクラスを作成します。このサンプルでは、他の多くのサポート クラスも定義され、使用されています。OnResolveAsyncResultOnFindAsyncResult、および AsyncResult の各クラスは、IAsyncResult インターフェイスを実装します。IAsyncResult 詳細情報、「System.IAsyncResult インターフェイス」を参照してください。

このトピックでは、探索プロキシの実装を 3 つの主要な部分に分けて説明します。

  • データ ストアを含み、抽象クラス DiscoveryProxy を拡張するクラスを定義する。

  • ヘルパー クラス AsyncResult を実装する。

  • 探索プロキシをホストする。

新しいコンソール アプリケーション プロジェクトを作成するには

  1. Visual Studio 2010 を起動します。

  2. 新しいコンソール アプリケーション プロジェクトを作成します。プロジェクトに「DiscoveryProxy」という名前を付け、ソリューションに「DiscoveryProxyExample」という名前を付けます。

  3. プロジェクトに次の参照を追加します。

    1. System.ServiceModel.dll

    2. System.Servicemodel.Discovery.dll

    Dd456787.Caution(ja-jp,VS.100).gif注意 :
    必ず、これらのアセンブリのバージョン 4.0 以降を参照してください。

ProxyDiscoveryService クラスを実装するには

  1. 新しいコード ファイルをプロジェクトに追加し、DiscoveryProxy.cs という名前を付けます。

  2. 次の using ステートメントを DiscoveryProxy.cs に追加します。

    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Discovery;
    using System.Xml;
    
  3. DiscoveryProxy から DiscoveryProxyService を派生させます。下の例に示すように、ServiceBehavior 属性をクラスに適用します。

    // Implement DiscoveryProxy by extending the DiscoveryProxy class and overriding the abstract methods
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class DiscoveryProxyService : DiscoveryProxy
    {
    }
    
  4. DiscoveryProxy クラス内で、登録済みサービスを保持するディクショナリを定義します。

    // Repository to store EndpointDiscoveryMetadata. 
    Dictionary<EndpointAddress, EndpointDiscoveryMetadata> onlineServices;
    
  5. ディクショナリを初期化するコンストラクターを定義します。

    public DiscoveryProxyService()
            {
                this.onlineServices = new Dictionary<EndpointAddress, EndpointDiscoveryMetadata>();
            }
    

探索プロキシ キャッシュの更新に使用するメソッドを定義するには

  1. AddOnlineservice メソッドを実装して、サービスをキャッシュに追加します。これは、プロキシがアナウンス メッセージを受け取るたびに呼び出されます。

    void AddOnlineService(EndpointDiscoveryMetadata endpointDiscoveryMetadata)
            {
                lock (this.onlineServices)
                {
                    this.onlineServices[endpointDiscoveryMetadata.Address] = endpointDiscoveryMetadata;                
                }
    
                PrintDiscoveryMetadata(endpointDiscoveryMetadata, "Adding");
            }
    
  2. キャッシュからサービスを削除するのに使用する RemoveOnlineService メソッドを実装します。

    void RemoveOnlineService(EndpointDiscoveryMetadata endpointDiscoveryMetadata)
            {
                if (endpointDiscoveryMetadata != null)
                {
                    lock (this.onlineServices)
                    {
                        this.onlineServices.Remove(endpointDiscoveryMetadata.Address);                    
                    }
    
                    PrintDiscoveryMetadata(endpointDiscoveryMetadata, "Removing");
                }    
            }
    
  3. サービスをディクショナリ内のサービスと照合する、MatchFromOnlineService メソッドを実装します。

        void MatchFromOnlineService(FindRequestContext findRequestContext)
                {
                    lock (this.onlineServices)
                    {
                        foreach (EndpointDiscoveryMetadata endpointDiscoveryMetadata in this.onlineServices.Values)
                        {
                            if (findRequestContext.Criteria.IsMatch(endpointDiscoveryMetadata))
                            {
                                findRequestContext.AddMatchingEndpoint(endpointDiscoveryMetadata);
                            }
                        }
                    }
                }
    
        EndpointDiscoveryMetadata MatchFromOnlineService(ResolveCriteria criteria)
                {
                    EndpointDiscoveryMetadata matchingEndpoint = null;
                    lock (this.onlineServices)
                    {
                        foreach (EndpointDiscoveryMetadata endpointDiscoveryMetadata in this.onlineServices.Values)
                        {
                            if (criteria.Address == endpointDiscoveryMetadata.Address)
                            {
                                matchingEndpoint = endpointDiscoveryMetadata;
                            }
                        }
                    }
                    return matchingEndpoint;
                }
    
  4. 探索プロキシの処理に関するコンソール テキスト出力をユーザーに提供する、PrintDiscoveryMetadata メソッドを実装します。

    void PrintDiscoveryMetadata(EndpointDiscoveryMetadata endpointDiscoveryMetadata, string verb)
            {
                Console.WriteLine("\n " + verb + " service of the following type from cache. ");
                foreach (XmlQualifiedName contractName in endpointDiscoveryMetadata.ContractTypeNames)
                {
                    Console.WriteLine("** " + contractName.ToString());
                    break;
                }
                Console.WriteLine(" Operation Completed");
            }
    
  5. 次の AsyncResult クラスを DiscoveryProxyService に追加します。これらのクラスは、非同期操作の各結果を区別するために使用されます。

    sealed class OnOnlineAnnouncementAsyncResult : AsyncResult
            {
                public OnOnlineAnnouncementAsyncResult(AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.Complete(true);
                }
    
                public static void End(IAsyncResult result)
                {
                    AsyncResult.End<OnOnlineAnnouncementAsyncResult>(result);
                }
            }
    
            sealed class OnOfflineAnnouncementAsyncResult : AsyncResult
            {
                public OnOfflineAnnouncementAsyncResult(AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.Complete(true);
                }
    
                public static void End(IAsyncResult result)
                {
                    AsyncResult.End<OnOfflineAnnouncementAsyncResult>(result);
                }
            }
    
            sealed class OnFindAsyncResult : AsyncResult
            {
                public OnFindAsyncResult(AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.Complete(true);
                }
    
                public static void End(IAsyncResult result)
                {
                    AsyncResult.End<OnFindAsyncResult>(result);
                }
            }
    
            sealed class OnResolveAsyncResult : AsyncResult
            {
                EndpointDiscoveryMetadata matchingEndpoint;
    
                public OnResolveAsyncResult(EndpointDiscoveryMetadata matchingEndpoint, AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.matchingEndpoint = matchingEndpoint;
                    this.Complete(true);
                }
    
                public static EndpointDiscoveryMetadata End(IAsyncResult result)
                {
                    OnResolveAsyncResult thisPtr = AsyncResult.End<OnResolveAsyncResult>(result);
                    return thisPtr.matchingEndpoint;
                }
            }
    

探索プロキシ機能を実装するメソッドを定義するには

  1. OnBeginOnlineAnnouncement メソッドをオーバーライドします。このメソッドは、探索プロキシがオンライン アナウンス メッセージを受け取ると呼び出されます。

    // OnBeginOnlineAnnouncement method is called when a Hello message is received by the Proxy
            protected override IAsyncResult OnBeginOnlineAnnouncement(DiscoveryMessageSequence messageSequence, EndpointDiscoveryMetadata endpointDiscoveryMetadata, AsyncCallback callback, object state)
            {        
                this.AddOnlineService(endpointDiscoveryMetadata);
                return new OnOnlineAnnouncementAsyncResult(callback, state);
            }
    
  2. OnEndOnlineAnnouncement メソッドをオーバーライドします。このメソッドは、探索プロキシがアナウンス メッセージの処理を終了すると呼び出されます。

    protected override void OnEndOnlineAnnouncement(IAsyncResult result)
            {
                OnOnlineAnnouncementAsyncResult.End(result);
            }
    
  3. OnBeginOfflineAnnouncement メソッドをオーバーライドします。このメソッドは、探索プロキシがオフライン アナウンス メッセージを受け取ると呼び出されます。

    // OnBeginOfflineAnnouncement method is called when a Bye message is received by the Proxy
            protected override IAsyncResult OnBeginOfflineAnnouncement(DiscoveryMessageSequence messageSequence, EndpointDiscoveryMetadata endpointDiscoveryMetadata, AsyncCallback callback, object state)
            {
                this.RemoveOnlineService(endpointDiscoveryMetadata);
                return new OnOfflineAnnouncementAsyncResult(callback, state);
            }
    
  4. OnEndOfflineAnnouncement メソッドをオーバーライドします。このメソッドは、探索プロキシがオフライン アナウンス メッセージの処理を終了すると呼び出されます。

    protected override void OnEndOfflineAnnouncement(IAsyncResult result)
            {
                OnOfflineAnnouncementAsyncResult.End(result);
            }
    
  5. OnBeginFind メソッドをオーバーライドします。このメソッドは、探索プロキシが検索要求を受け取ると呼び出されます。

    // OnBeginFind method is called when a Probe request message is received by the Proxy
            protected override IAsyncResult OnBeginFind(FindRequestContext findRequestContext, AsyncCallback callback, object state)
            {
                this.MatchFromOnlineService(findRequestContext);
                return new OnFindAsyncResult(callback, state);
            }
    protected override IAsyncResult OnBeginFind(FindRequest findRequest, AsyncCallback callback, object state)
    {
        Collection<EndpointDiscoveryMetadata> matchingEndpoints = MatchFromCache(findRequest.Criteria);
        return new OnFindAsyncResult(
                    matchingEndpoints,
                    callback,
                    state);
    }
    
  6. OnEndFind メソッドをオーバーライドします。このメソッドは、探索プロキシが検索要求の処理を終了すると呼び出されます。

    protected override void OnEndFind(IAsyncResult result)
            {
                OnFindAsyncResult.End(result);
            }
    
  7. OnBeginResolve メソッドをオーバーライドします。このメソッドは、探索プロキシが解決メッセージを受け取ると呼び出されます。

    // OnBeginFind method is called when a Resolve request message is received by the Proxy
            protected override IAsyncResult OnBeginResolve(ResolveCriteria resolveCriteria, AsyncCallback callback, object state)
            {
                return new OnResolveAsyncResult(this.MatchFromOnlineService(resolveCriteria), callback, state);
            }
    protected override IAsyncResult OnBeginResolve(ResolveRequest resolveRequest, AsyncCallback callback, object state)
    {
        return new OnResolveAsyncResult(
            this.proxy.MatchFromOnlineService(resolveRequest.Criteria),
            callback,
            state);
    }
    
  8. OnEndResolve メソッドをオーバーライドします。このメソッドは、探索プロキシが解決メッセージの処理を終了すると呼び出されます。

            protected override EndpointDiscoveryMetadata OnEndResolve(IAsyncResult result)
            {
                return OnResolveAsyncResult.End(result);
            }
    

OnBegin../ OnEnd.. メソッドは、以降の探索操作のロジックを提供します。たとえば、OnBeginFind メソッドおよび OnEndFind メソッドは、探索プロキシの検索ロジックを実装します。探索プロキシがプローブ メッセージを受け取ると、これらのメソッドが実行されて、クライアントに応答が返されます。必要に応じて、検索ロジックを変更できます。たとえば、アルゴリズムによるカスタム スコープ一致や、検索操作の一環として解析するアプリケーション固有の XML メタデータを組み込むことができます。

AsyncResult クラスを実装するには

  1. 各種の非同期結果クラスを派生させるために使用する抽象基本クラス AsyncResult を定義します。

  2. AsyncResult.cs という名前の新しいコード ファイルを作成します。

  3. 次の using ステートメントを AsyncResult.cs に追加します。

    using System;
    using System.Threading;
    
  4. 次の AsyncResult クラスを追加します。

    abstract class AsyncResult : IAsyncResult
        {
            AsyncCallback callback;
            bool completedSynchronously;
            bool endCalled;
            Exception exception;
            bool isCompleted;
            ManualResetEvent manualResetEvent;
            object state;
            object thisLock;
    
            protected AsyncResult(AsyncCallback callback, object state)
            {
                this.callback = callback;
                this.state = state;
                this.thisLock = new object();
            }
    
            public object AsyncState
            {
                get
                {
                    return state;
                }
            }
    
            public WaitHandle AsyncWaitHandle
            {
                get
                {
                    if (manualResetEvent != null)
                    {
                        return manualResetEvent;
                    }
                    lock (ThisLock)
                    {
                        if (manualResetEvent == null)
                        {
                            manualResetEvent = new ManualResetEvent(isCompleted);
                        }
                    }
                    return manualResetEvent;
                }
            }
    
            public bool CompletedSynchronously
            {
                get
                {
                    return completedSynchronously;
                }
            }
    
            public bool IsCompleted
            {
                get
                {
                    return isCompleted;
                }
            }
    
            object ThisLock
            {
                get
                {
                    return this.thisLock;
                }
            }
    
            protected static TAsyncResult End<TAsyncResult>(IAsyncResult result)
                where TAsyncResult : AsyncResult
            {
                if (result == null)
                {
                    throw new ArgumentNullException("result");
                }
    
                TAsyncResult asyncResult = result as TAsyncResult;
    
                if (asyncResult == null)
                {
                    throw new ArgumentException("Invalid async result.", "result");
                }
    
                if (asyncResult.endCalled)
                {
                    throw new InvalidOperationException("Async object already ended.");
                }
    
                asyncResult.endCalled = true;
    
                if (!asyncResult.isCompleted)
                {
                    asyncResult.AsyncWaitHandle.WaitOne();
                }
    
                if (asyncResult.manualResetEvent != null)
                {
                    asyncResult.manualResetEvent.Close();
                }
    
                if (asyncResult.exception != null)
                {
                    throw asyncResult.exception;
                }
    
                return asyncResult;
            }
    
            protected void Complete(bool completedSynchronously)
            {
                if (isCompleted)
                {
                    throw new InvalidOperationException("This async result is already completed.");
                }
    
                this.completedSynchronously = completedSynchronously;
    
                if (completedSynchronously)
                {
                    this.isCompleted = true;
                }
                else
                {
                    lock (ThisLock)
                    {
                        this.isCompleted = true;
                        if (this.manualResetEvent != null)
                        {
                            this.manualResetEvent.Set();
                        }
                    }
                }
    
                if (callback != null)
                {
                    callback(this);
                }
            }
    
            protected void Complete(bool completedSynchronously, Exception exception)
            {
                this.exception = exception;
                Complete(completedSynchronously);
            }
        }
    

DiscoveryProxy をホストするには

  1. DiscoveryProxyExample プロジェクトで Program.cs ファイルを開きます。

  2. 次の using ステートメントを追加します。

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Discovery;
    
  3. Main() メソッド内に次のコードを追加します。これにより、DiscoveryProxy クラスのインスタンスが作成されます。

    Uri probeEndpointAddress = new Uri("net.tcp://localhost:8001/Probe");
                Uri announcementEndpointAddress = new Uri("net.tcp://localhost:9021/Announcement");
    
                // Host the DiscoveryProxy service
                ServiceHost proxyServiceHost = new ServiceHost(new DiscoveryProxyService());
    
  4. 次に、探索エンドポイントとアナウンス エンドポイントを追加する、次のコードを追加します。

      try
                {                
                    // Add DiscoveryEndpoint to receive Probe and Resolve messages
                    DiscoveryEndpoint discoveryEndpoint = new DiscoveryEndpoint(new NetTcpBinding(), new EndpointAddress(probeEndpointAddress));
                    discoveryEndpoint.IsSystemEndpoint = false;
    
                    // Add AnnouncementEndpoint to receive Hello and Bye announcement messages
                    AnnouncementEndpoint announcementEndpoint = new AnnouncementEndpoint(new NetTcpBinding(), new EndpointAddress(announcementEndpointAddress));                
    
                    proxyServiceHost.AddServiceEndpoint(discoveryEndpoint);
                    proxyServiceHost.AddServiceEndpoint(announcementEndpoint);
    
                    proxyServiceHost.Open();
    
                    Console.WriteLine("Proxy Service started.");
                    Console.WriteLine();
                    Console.WriteLine("Press <ENTER> to terminate the service.");
                    Console.WriteLine();
                    Console.ReadLine();
    
                    proxyServiceHost.Close();
                }
                catch (CommunicationException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (TimeoutException e)
                {
                    Console.WriteLine(e.Message);
                }   
    
                if (proxyServiceHost.State != CommunicationState.Closed)
                {
                    Console.WriteLine("Aborting the service...");
                    proxyServiceHost.Abort();
                }
    

これで、探索プロキシの実装が完了しました。次に、「探索プロキシで登録される探索可能なサービスの実装方法」に進みます。

このトピックで使用するコード全体の一覧を次に示します。

    // DiscoveryProxy.cs
    //----------------------------------------------------------------
    // Copyright (c) Microsoft Corporation.  All rights reserved.
    //----------------------------------------------------------------
    
    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Discovery;
    using System.Xml;
    
    namespace Microsoft.Samples.Discovery
    {
        // Implement DiscoveryProxy by extending the DiscoveryProxy class and overriding the abstract methods
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
        public class DiscoveryProxyService : DiscoveryProxy
        {
            // Repository to store EndpointDiscoveryMetadata. A database or a flat file could also be used instead.
            Dictionary<EndpointAddress, EndpointDiscoveryMetadata> onlineServices;
    
            public DiscoveryProxyService()
            {
                this.onlineServices = new Dictionary<EndpointAddress, EndpointDiscoveryMetadata>();
            }
    
            // OnBeginOnlineAnnouncement method is called when a Hello message is received by the Proxy
            protected override IAsyncResult OnBeginOnlineAnnouncement(DiscoveryMessageSequence messageSequence, EndpointDiscoveryMetadata endpointDiscoveryMetadata, AsyncCallback callback, object state)
            {        
                this.AddOnlineService(endpointDiscoveryMetadata);
                return new OnOnlineAnnouncementAsyncResult(callback, state);
            }
    
            protected override void OnEndOnlineAnnouncement(IAsyncResult result)
            {
                OnOnlineAnnouncementAsyncResult.End(result);
            }
    
            // OnBeginOfflineAnnouncement method is called when a Bye message is received by the Proxy
            protected override IAsyncResult OnBeginOfflineAnnouncement(DiscoveryMessageSequence messageSequence, EndpointDiscoveryMetadata endpointDiscoveryMetadata, AsyncCallback callback, object state)
            {
                this.RemoveOnlineService(endpointDiscoveryMetadata);
                return new OnOfflineAnnouncementAsyncResult(callback, state);
            }
    
            protected override void OnEndOfflineAnnouncement(IAsyncResult result)
            {
                OnOfflineAnnouncementAsyncResult.End(result);
            }
    
            // OnBeginFind method is called when a Probe request message is received by the Proxy
            protected override IAsyncResult OnBeginFind(FindRequestContext findRequestContext, AsyncCallback callback, object state)
            {
                this.MatchFromOnlineService(findRequestContext);
                return new OnFindAsyncResult(callback, state);
            }
    
            protected override void OnEndFind(IAsyncResult result)
            {
                OnFindAsyncResult.End(result);
            }
    
            // OnBeginFind method is called when a Resolve request message is received by the Proxy
            protected override IAsyncResult OnBeginResolve(ResolveCriteria resolveCriteria, AsyncCallback callback, object state)
            {
                return new OnResolveAsyncResult(this.MatchFromOnlineService(resolveCriteria), callback, state);
            }
    
            protected override EndpointDiscoveryMetadata OnEndResolve(IAsyncResult result)
            {
                return OnResolveAsyncResult.End(result);
            }
    
            // The following are helper methods required by the Proxy implementation
            void AddOnlineService(EndpointDiscoveryMetadata endpointDiscoveryMetadata)
            {
                lock (this.onlineServices)
                {
                    this.onlineServices[endpointDiscoveryMetadata.Address] = endpointDiscoveryMetadata;                
                }
    
                PrintDiscoveryMetadata(endpointDiscoveryMetadata, "Adding");
            }
    
            void RemoveOnlineService(EndpointDiscoveryMetadata endpointDiscoveryMetadata)
            {
                if (endpointDiscoveryMetadata != null)
                {
                    lock (this.onlineServices)
                    {
                        this.onlineServices.Remove(endpointDiscoveryMetadata.Address);                    
                    }
    
                    PrintDiscoveryMetadata(endpointDiscoveryMetadata, "Removing");
                }    
            }
    
            void MatchFromOnlineService(FindRequestContext findRequestContext)
            {
                lock (this.onlineServices)
                {
                    foreach (EndpointDiscoveryMetadata endpointDiscoveryMetadata in this.onlineServices.Values)
                    {
                        if (findRequestContext.Criteria.IsMatch(endpointDiscoveryMetadata))
                        {
                            findRequestContext.AddMatchingEndpoint(endpointDiscoveryMetadata);
                        }
                    }
                }
            }
    
            EndpointDiscoveryMetadata MatchFromOnlineService(ResolveCriteria criteria)
            {
                EndpointDiscoveryMetadata matchingEndpoint = null;
                lock (this.onlineServices)
                {
                    foreach (EndpointDiscoveryMetadata endpointDiscoveryMetadata in this.onlineServices.Values)
                    {
                        if (criteria.Address == endpointDiscoveryMetadata.Address)
                        {
                            matchingEndpoint = endpointDiscoveryMetadata;
                        }
                    }
                }
                return matchingEndpoint;
            }
    
            void PrintDiscoveryMetadata(EndpointDiscoveryMetadata endpointDiscoveryMetadata, string verb)
            {
                Console.WriteLine("\n " + verb + " service of the following type from cache. ");
                foreach (XmlQualifiedName contractName in endpointDiscoveryMetadata.ContractTypeNames)
                {
                    Console.WriteLine("** " + contractName.ToString());
                    break;
                }
                Console.WriteLine(" Operation Completed");
            }
    
            sealed class OnOnlineAnnouncementAsyncResult : AsyncResult
            {
                public OnOnlineAnnouncementAsyncResult(AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.Complete(true);
                }
    
                public static void End(IAsyncResult result)
                {
                    AsyncResult.End<OnOnlineAnnouncementAsyncResult>(result);
                }
            }
    
            sealed class OnOfflineAnnouncementAsyncResult : AsyncResult
            {
                public OnOfflineAnnouncementAsyncResult(AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.Complete(true);
                }
    
                public static void End(IAsyncResult result)
                {
                    AsyncResult.End<OnOfflineAnnouncementAsyncResult>(result);
                }
            }
    
            sealed class OnFindAsyncResult : AsyncResult
            {
                public OnFindAsyncResult(AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.Complete(true);
                }
        
                public static void End(IAsyncResult result)
                {
                    AsyncResult.End<OnFindAsyncResult>(result);
                }
            }
        
            sealed class OnResolveAsyncResult : AsyncResult
            {
                EndpointDiscoveryMetadata matchingEndpoint;
        
                public OnResolveAsyncResult(EndpointDiscoveryMetadata matchingEndpoint, AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.matchingEndpoint = matchingEndpoint;
                    this.Complete(true);
                }
        
                public static EndpointDiscoveryMetadata End(IAsyncResult result)
                {
                    OnResolveAsyncResult thisPtr = AsyncResult.End<OnResolveAsyncResult>(result);
                    return thisPtr.matchingEndpoint;
                }
            }
        }
    }
    // AsyncResult.cs
    //----------------------------------------------------------------
    // Copyright (c) Microsoft Corporation.  All rights reserved.
    //----------------------------------------------------------------
    
    using System;
    using System.Threading;
    
    namespace Microsoft.Samples.Discovery
    {
        abstract class AsyncResult : IAsyncResult
        {
            AsyncCallback callback;
            bool completedSynchronously;
            bool endCalled;
            Exception exception;
            bool isCompleted;
            ManualResetEvent manualResetEvent;
            object state;
            object thisLock;
    
            protected AsyncResult(AsyncCallback callback, object state)
            {
                this.callback = callback;
                this.state = state;
                this.thisLock = new object();
            }
    
            public object AsyncState
            {
                get
                {
                    return state;
                }
            }
    
            public WaitHandle AsyncWaitHandle
            {
                get
                {
                    if (manualResetEvent != null)
                    {
                        return manualResetEvent;
                    }
                    lock (ThisLock)
                    {
                        if (manualResetEvent == null)
                        {
                            manualResetEvent = new ManualResetEvent(isCompleted);
                        }
                    }
                    return manualResetEvent;
                }
            }
    
            public bool CompletedSynchronously
            {
                get
                {
                    return completedSynchronously;
                }
            }
    
            public bool IsCompleted
            {
                get
                {
                    return isCompleted;
                }
            }
    
            object ThisLock
            {
                get
                {
                    return this.thisLock;
                }
            }
    
            protected static TAsyncResult End<TAsyncResult>(IAsyncResult result)
                where TAsyncResult : AsyncResult
            {
                if (result == null)
                {
                    throw new ArgumentNullException("result");
                }
    
                TAsyncResult asyncResult = result as TAsyncResult;
    
                if (asyncResult == null)
                {
                    throw new ArgumentException("Invalid async result.", "result");
                }
    
                if (asyncResult.endCalled)
                {
                    throw new InvalidOperationException("Async object already ended.");
                }
    
                asyncResult.endCalled = true;
    
                if (!asyncResult.isCompleted)
                {
                    asyncResult.AsyncWaitHandle.WaitOne();
                }
    
                if (asyncResult.manualResetEvent != null)
                {
                    asyncResult.manualResetEvent.Close();
                }
    
                if (asyncResult.exception != null)
                {
                    throw asyncResult.exception;
                }
    
                return asyncResult;
            }
    
            protected void Complete(bool completedSynchronously)
            {
                if (isCompleted)
                {
                    throw new InvalidOperationException("This async result is already completed.");
                }
    
                this.completedSynchronously = completedSynchronously;
    
                if (completedSynchronously)
                {
                    this.isCompleted = true;
                }
                else
                {
                    lock (ThisLock)
                    {
                        this.isCompleted = true;
                        if (this.manualResetEvent != null)
                        {
                            this.manualResetEvent.Set();
                        }
                    }
                }
    
                if (callback != null)
                {
                    callback(this);
                }
            }
    
            protected void Complete(bool completedSynchronously, Exception exception)
            {
                this.exception = exception;
                Complete(completedSynchronously);
            }
        }
    }
// program.cs
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//----------------------------------------------------------------

using System;
using System.ServiceModel;
using System.ServiceModel.Discovery;

namespace Microsoft.Samples.Discovery
{
    class Program
    {
        public static void Main()
        {
            Uri probeEndpointAddress = new Uri("net.tcp://localhost:8001/Probe");
            Uri announcementEndpointAddress = new Uri("net.tcp://localhost:9021/Announcement");

            // Host the DiscoveryProxy service
            ServiceHost proxyServiceHost = new ServiceHost(new DiscoveryProxyService());

            try
            {                
                // Add DiscoveryEndpoint to receive Probe and Resolve messages
                DiscoveryEndpoint discoveryEndpoint = new DiscoveryEndpoint(new NetTcpBinding(), new EndpointAddress(probeEndpointAddress));
                discoveryEndpoint.IsSystemEndpoint = false;

                // Add AnnouncementEndpoint to receive Hello and Bye announcement messages
                AnnouncementEndpoint announcementEndpoint = new AnnouncementEndpoint(new NetTcpBinding(), new EndpointAddress(announcementEndpointAddress));                

                proxyServiceHost.AddServiceEndpoint(discoveryEndpoint);
                proxyServiceHost.AddServiceEndpoint(announcementEndpoint);

                proxyServiceHost.Open();

                Console.WriteLine("Proxy Service started.");
                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();

                proxyServiceHost.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
            }   

            if (proxyServiceHost.State != CommunicationState.Closed)
            {
                Console.WriteLine("Aborting the service...");
                proxyServiceHost.Abort();
            }
        }
    }
}

参照

処理手順

探索プロキシで登録される探索可能なサービスの実装方法
探索プロキシを使用してサービスを検索するクライアント アプリケーションの実装方法
探索プロキシをテストする方法

概念

WCF Discovery の概要