Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 GetCallbackChannel(T) Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
OperationContext..::.GetCallbackChannel<(Of <(T>)>) Method

Gets a channel to the client instance that called the current operation.

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
Visual Basic (Declaration)
Public Function GetCallbackChannel(Of T) As T
Visual Basic (Usage)
Dim instance As OperationContext
Dim returnValue As T

returnValue = instance.GetCallbackChannel()
C#
public T GetCallbackChannel<T>()
Visual C++
public:
generic<typename T>
T GetCallbackChannel()
JScript
JScript does not support generic types or methods.

Type Parameters

T

The type of channel used to call back to the client.

Return Value

Type: T
A channel to the client instance that called the operation of the type specified in the CallbackContract property.

Call the GetCallbackChannel<(Of <(T>)>) property to obtain a channel that you can use to call operations on the client instance that called the service.

The following code example uses the Current property and GetCallbackChannel<(Of <(T>)>) method to create a channel back to the caller from within a operation. All operations in this example are one-way operations, enabling the service and the client to communicate in both directions independently. In this case, the example client application expects only one return call before it exits, but another client, for example a Windows Forms client, can receive any number of calls from the service.

C#
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);
    }
  }
}

The following client implements the SampleDuplexHelloCallback to receive the callback message. The imported callback contract is not the same name as the one in the service, due to the use of the Name property in the preceding example. Note that the client makes no assumptions about whether or when it might receive a callback; the server callback is entirely independent of the client's outbound call.

NoteNote:

For an example that uses the OperationContext class in a client scenario, see OperationContextScope.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Trouble with singleton model      fkiller ... Thomas Lee   |   Edit   |   Show History

When I set InstanceContextMode to singleton as follows:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

An error occurs in creating callback object statement

callback = OperationContext.Current.GetCallbackChannel<ILobbyCallback>();

The error message is as below,

Object reference not set to an instance of an object.

It works fine using PerSession or PerCall.

Anyone has the same problem?

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker