OperationContext.GetCallbackChannel<T> メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在の操作を呼び出したクライアント インスタンスへのチャネルを取得します。
public:
generic <typename T>
T GetCallbackChannel();
public T GetCallbackChannel<T>();
member this.GetCallbackChannel : unit -> 'T
Public Function GetCallbackChannel(Of T) () As T
型パラメーター
- T
クライアントへのコールバックに使用されるチャネルの種類。
戻り値
CallbackContract プロパティで指定された型の操作を呼び出したクライアント インスタンスへのチャネル。
例
次のコード例では、 Current プロパティと GetCallbackChannel メソッドを使用して、操作内から呼び出し元にチャネルを作成します。 この例のすべての操作は一方向の操作であり、サービスとクライアントが双方向で独立して通信できるようにします。 この場合、クライアント アプリケーションの例では、終了する前に 1 回のリターン呼び出ししか想定しませんが、別のクライアント (Windows フォーム クライアントなど) は、サービスから任意の数の呼び出しを受信できます。
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;
namespace Microsoft.WCF.Documentation
{
[ServiceContract(
Name = "SampleDuplexHello",
Namespace = "http://microsoft.wcf.documentation",
CallbackContract = typeof(IHelloCallbackContract),
SessionMode = SessionMode.Required
)]
public interface IDuplexHello
{
[OperationContract(IsOneWay = true)]
void Hello(string greeting);
}
public interface IHelloCallbackContract
{
[OperationContract(IsOneWay = true)]
void Reply(string responseToGreeting);
}
public class DuplexHello : IDuplexHello
{
public DuplexHello()
{
Console.WriteLine("Service object created: " + this.GetHashCode().ToString());
}
~DuplexHello()
{
Console.WriteLine("Service object destroyed: " + this.GetHashCode().ToString());
}
public void Hello(string greeting)
{
Console.WriteLine("Caller sent: " + greeting);
Console.WriteLine("Session ID: " + OperationContext.Current.SessionId);
Console.WriteLine("Waiting two seconds before returning call.");
// Put a slight delay to demonstrate asynchronous behavior on client.
Thread.Sleep(2000);
IHelloCallbackContract callerProxy
= OperationContext.Current.GetCallbackChannel<IHelloCallbackContract>();
string response = "Service object " + this.GetHashCode().ToString() + " received: " + greeting;
Console.WriteLine("Sending back: " + response);
callerProxy.Reply(response);
}
}
}
次のクライアントは、コールバック メッセージを受信する SampleDuplexHelloCallback を実装します。 インポートされたコールバック コントラクトは、前の例の Name プロパティを使用しているため、サービス内のコールバック コントラクトと同じ名前ではありません。 クライアントはコールバックを受け取る可能性があるかどうか、またはいつ受け取るかについて何の想定もしない点に注意してください。サーバー コールバックは、クライアントの送信呼び出しから完全に独立しています。
Note
クライアント シナリオで OperationContext クラスを使用する例については、 OperationContextScopeを参照してください。
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;
namespace Microsoft.WCF.Documentation
{
public class Client : SampleDuplexHelloCallback
{
AutoResetEvent waitHandle;
public Client()
{
waitHandle = new AutoResetEvent(false);
}
public void Run()
{
// Picks up configuration from the config file.
SampleDuplexHelloClient wcfClient
= new SampleDuplexHelloClient(new InstanceContext(this));
try
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Enter a greeting to send and press ENTER: ");
Console.Write(">>> ");
Console.ForegroundColor = ConsoleColor.Green;
string greeting = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Called service with: \r\n\t" + greeting);
wcfClient.Hello(greeting);
Console.WriteLine("Execution passes service call and moves to the WaitHandle.");
this.waitHandle.WaitOne();
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Set was called.");
Console.Write("Press ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("ENTER");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(" to exit...");
Console.ReadLine();
wcfClient.Close();
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
Console.ReadLine();
wcfClient.Abort();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message);
Console.ReadLine();
wcfClient.Abort();
}
}
public static void Main()
{
Client client = new Client();
client.Run();
}
public void Reply(string response)
{
Console.WriteLine("Received output.");
Console.WriteLine("\r\n\t" + response);
this.waitHandle.Set();
}
}
}
注釈
GetCallbackChannel<T>() メソッドを呼び出して、サービスを呼び出したクライアント インスタンスに対する操作を呼び出すために使用できるチャネルを取得します。