I am trying to receive duplex callback from WCF service into my web api action. But somehow this is not working. I am not able to get the callback in web api.
If I use console application instead of asp.net core application then console application receives callback. Hence, WCF service is setup correctly and working fine. Somehow problem is in web api controller (specially related to callback handler)
Please suggest how should I write callback handler inside web api?
These are steps I tried so far-
Step1: Created WCF service with netTcpBinding
namespace WcfServiceLibrary1
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class CalculatorService : ICalculatorDuplex
{
double result;
string equation;
ICalculatorDuplexCallback callback = null;
public CalculatorService()
{
result = 0.0D;
equation = result.ToString();
callback = OperationContext.Current.GetCallbackChannel<ICalculatorDuplexCallback>();
}
public void Clear()
{
callback.Equation(equation + " = " + result.ToString());
result = 0.0D;
equation = result.ToString();
System.IO.File.AppendAllText(@"D:\Sanket\wcf_test.txt", DateTime.Now.ToShortTimeString() + "\t" + equation + "\t" + result + Environment.NewLine);
}
public void AddTo(double n)
{
result += n;
equation += " + " + n.ToString();
callback.Equals(result);
}
public void SubtractFrom(double n)
{
result -= n;
equation += " - " + n.ToString();
callback.Equals(result);
}
public void MultiplyBy(double n)
{
result *= n;
equation += " * " + n.ToString();
callback.Equals(result);
}
public void DivideBy(double n)
{
result /= n;
equation += " / " + n.ToString();
callback.Equals(result);
}
}
Step2: Used WCF Connected Service extension to generate the service reference code into a file named reference.cs as mentioned in this article
Step3: To create instances of the WCF client types generated by the tool and communicate with your web service, I have written below code in my web api controller-
[Produces("application/json")]
[Route("api/[controller]")]
public class WcfServiceTestingController : Controller
{
CalculatorDuplexClient wcfClient
= new CalculatorDuplexClient(CalculatorDuplexClientBase.EndpointConfiguration.NetTcpBinding_ICalculatorDuplex);
// GET: api/values
[HttpGet]
public ActionResult Get()
{
try
{
// Call the AddTo service operation.
double value = 100.00D;
wcfClient.AddToAsync(value);
// Call the SubtractFrom service operation.
value = 50.00D;
wcfClient.SubtractFromAsync(value);
// Call the MultiplyBy service operation.
value = 17.65D;
wcfClient.MultiplyByAsync(value);
// Call the DivideBy service operation.
value = 2.00D;
wcfClient.DivideByAsync(value);
// Complete equation.
wcfClient.ClearAsync();
// Wait for callback messages to complete before
// closing.
System.Threading.Thread.Sleep(5000);
// Close the WCF client.
wcfClient.Close();
Console.WriteLine("Done!");
return Json("Done!");
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
wcfClient.Abort();
Console.Read();
return Json("TimeoutException");
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message);
wcfClient.Abort();
Console.Read();
return Json("CommunicationException");
}
}
private class CallbackHandler : ICalculatorDuplexCallback
{
public CallbackHandler()
{
Console.WriteLine("CallbackHandler");
}
public void Equals(double result)
{
Console.WriteLine(" Result({0})", result);
}
public void Equation(string eqn)
{
Console.WriteLine(" Equation({0})", eqn);
}
}
}
}
My Environment:
Visual Studio 2015 Update3 + WCF Connected Service extension
WCF service with netTcpBinding
ASP.NET core 1.0 application