OperationContext.GetCallbackChannel<T> Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar en kanal till klientinstansen som anropade den aktuella åtgärden.
public:
generic <typename T>
T GetCallbackChannel();
public T GetCallbackChannel<T>();
member this.GetCallbackChannel : unit -> 'T
Public Function GetCallbackChannel(Of T) () As T
Typparametrar
- T
Den typ av kanal som används för att anropa tillbaka till klienten.
Returer
En kanal till klientinstansen som anropade åtgärden av den typ som anges i CallbackContract egenskapen.
Exempel
I följande kodexempel används Current egenskapen och GetCallbackChannel metoden för att skapa en kanal tillbaka till anroparen inifrån en åtgärd. Alla åtgärder i det här exemplet är enkelriktade åtgärder som gör det möjligt för tjänsten och klienten att kommunicera i båda riktningarna oberoende av varandra. I det här fallet förväntar sig exempelklientprogrammet bara ett returanrop innan det avslutas, men en annan klient, till exempel en Windows Forms-klient, kan ta emot valfritt antal anrop från tjänsten.
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);
}
}
}
Följande klient implementerar SampleDuplexHelloCallback för att ta emot motringningsmeddelandet. Det importerade återanropskontraktet är inte samma namn som det i tjänsten, på grund av användningen av Name egenskapen i föregående exempel. Observera att klienten inte gör några antaganden om huruvida eller när den kan få ett återanrop. serveråteranropet är helt oberoende av klientens utgående anrop.
Note
Ett exempel som använder OperationContext klassen i ett klientscenario finns OperationContextScopei .
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();
}
}
}
Kommentarer
GetCallbackChannel<T>() Anropa metoden för att hämta en kanal som du kan använda för att anropa åtgärder på klientinstansen som anropade tjänsten.