WCF

WCF Examples

WCF

WCF Projects

WCF Project

adplus-dvertising
Callback reentrancy
Previous Home Next

If the single-threaded service instance tries to call back to its client, WCF will throw an InvalidOperationException to avoid a deadlock.Then possible solutions is that to configure the service for reentrancy. When configured for reentrancy, the service instance is still associated with a lock and only a single-threaded access is allowed. However, if the service is calling back to its client, WCF will silently release the lock first.

Example:

Configure for reentrancy to allow callbacks

[ServiceContract(CallbackContract = typeof(IMyContractCallback))]
interface IMyContract
{
[OperationContract]
void operation( );
}
interface IMyContractCallback
{
[OperationContract]
void OnCallback( );
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
class MyService : IMyContract
{
public void operation( )
{
IMyContractCallback imcc = OperationContext.Current.
GetCallbackChannel<IMyContractCallback>( );
imcc.OnCallback( );
}
}

Example:

One-way callbacks are allowed by default

[ServiceContract(CallbackContract = typeof(IMyContractCallback))]
interface IMyContract
{
[OperationContract]
void operation( );
}
interface IMyContractCallback
{
[OperationContract(IsOneWay = true)]
void OnCallback( );
}
class MyService : IMyContract
{
public void operation( )
{
IMyContractCallback imcc = OperationContext.Current.
GetCallbackChannel<IMyContractCallback>( );
  imcc.OnCallback( );
}
}
Previous Home Next