我正在尝试将来自 WCF 服务的双工回调接收到我的 Web api 操作中。但不知何故,这是行不通的。我无法在 web api 中获得回调。
如果我使用控制台应用程序而不是 asp.net 核心应用程序,那么控制台应用程序会收到回调。因此,WCF 服务设置正确并且工作正常。不知何故,问题出在 web api 控制器中(特别与回调处理程序有关)
请建议我应该如何在 web api 中编写回调处理程序?
这些是我到目前为止尝试过的步骤-
Step1:使用 netTcpBinding 创建 WCF 服务
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:使用WCF Connected Service扩展将服务参考代码生成到本文提到的名为reference.cs的文件中
第3 步:为了创建工具生成的 WCF 客户端类型的实例并与您的 Web 服务通信,我在我的 Web api 控制器中编写了以下代码 -
[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);
}
}
}
}
我的环境:
Visual Studio 2015 Update3 + WCF 连接服务扩展
带有 netTcpBinding 的 WCF 服务
ASP.NET 核心 1.0 应用程序