About
WCF service example to explain how and when to implement callback model (WSDualHttpBinding)
[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof(IDuplexServiceCallback))]
public interface IDuplexService {
[OperationContract(IsOneWay = true)]
void Registration(string Name);
}
[ServiceContract]
public interface IDuplexServiceCallback {
[OperationContract(IsOneWay = true)]
void NotifyRegistration(string Name);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class DuplexService : IDuplexService
{
#region IDuplexService Members
string result = "Your request has been regitered by Application ";
public static Timer Timer;
public static IDuplexServiceCallback Callback;
public DuplexService()
{
}
public void Registration(string Name)
{
Callback = OperationContext.Current.GetCallbackChannel<IDuplexServiceCallback>();
result = Name + " :: " + result;
Timer = new Timer(1000);
Timer.Elapsed += OnTimerElapsed;
Timer.Enabled = true;
}
void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
string notification = result + " @ " + DateTime.Now.ToString();
Callback.NotifyRegistration(notification);
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="DuplexServiceLibrary.DuplexService">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8090/DuplexService/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="wsDualHttpBinding" contract="DuplexServiceLibrary.IDuplexService">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
static void Main(string[] args) {
ServiceHost host = new ServiceHost(typeof(DuplexService));
ConsoleColor oldColour = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("DuplexService is up and running with the following endpoints:");
foreach (ServiceEndpoint se in host.Description.Endpoints)
Console.WriteLine(se.Address.ToString());
Console.ReadLine();
host.Open();
host.Close();
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IDuplexService" clientBaseAddress="http://localhost:8091/" />
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8090/DuplexService/" binding="wsDualHttpBinding"
bindingConfiguration="WSDualHttpBinding_IDuplexService" contract="DuplexServiceReference.IDuplexService"
name="WSDualHttpBinding_IDuplexService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
private static DuplexServiceReference.DuplexServiceClient duplexClient;
public class DuplexServiceCallBackHandler : DuplexServiceReference.IDuplexServiceCallback{
public void NotifyRegistration(string Name){
ConsoleColor oldColour = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Notification : ({0})", Name);
}
}
InstanceContext context = new InstanceContext(new DuplexServiceCallBackHandler());
duplexClient = new DuplexServiceReference.DuplexServiceClient(context, "WSDualHttpBinding_IDuplexService");
duplexClient.Registration(name);
Console.ReadLine();
class Client{
private static DuplexServiceReference.DuplexServiceClient duplexClient;
static void Main(string[] args){
InstanceContext context = new InstanceContext(new DuplexServiceCallBackHandler());
duplexClient = new DuplexServiceReference.DuplexServiceClient(context, "WSDualHttpBinding_IDuplexService");
try{
Console.WriteLine("This is a client, press enter initiate request to server");
Console.ReadLine();
string name = "Sudhir";
duplexClient.Registration(name);
Console.ReadLine();
}
catch (TimeoutException timeProblem){
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
duplexClient.Abort();
Console.Read();
}
catch (CommunicationException commProblem){
Console.WriteLine("There was a communication problem. " + commProblem.Message);
duplexClient.Abort();
Console.Read();
}
}
}
public class DuplexServiceCallBackHandler : DuplexServiceReference.IDuplexServiceCallback
{
public void NotifyRegistration(string Name){
ConsoleColor oldColour = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Notification : ({0})", Name);
}
}



