自己使用总结,方便理解。
1、定义wcf服务接口类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WcfCallbackService
{
/// <summary>
/// 定义回调契约CallbackContract属性指定
/// </summary>
[ServiceContract(CallbackContract = typeof(ICallback))]
public interface ICalculator
{
[OperationContract(IsOneWay = true)]
void Add(double x, double y);
[OperationContract(IsOneWay = false)]
double Minus(double x, double y);
}
}
2、定义回调协定接口
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.ServiceModel;
namespace WcfCallbackService
{
/// <summary>
/// 回调协定接口
/// </summary>
public interface ICallback
{
[OperationContract(IsOneWay =true)]
void DisplayResult(double x, double y, double result);
}
}
3、服务具体实现类CalculatorService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WcfCallbackService
{
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class CalculatorService: ICalculator
{
public void Add(double x, double y)
{
double result = x + y;
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
callback.DisplayResult(x, y, result);
Console.WriteLine("客户端已经正常显示结果为:" + result.ToString());
}
public double Minus(double x, double y)
{
double result = x - y;
return result;
}
/// <summary>
/// 输出执行过程概要日志
/// </summary>
public event Action<string> OnLogOutPut;
/// <summary>
/// 概要日志
/// </summary>
/// <param name="summaryInfo"></param>
protected void LogOutPut(string summaryInfo)
{
if (OnLogOutPut != null)
{
OnLogOutPut(summaryInfo);
}
}
}
}
4、服务启动入口类
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;
using WcfCallbackService;
namespace WcfCallbackServiceWrapper
{
class Program
{
static void Main(string[] args)
{
StartServer();
}
/// <summary>
/// 启动服务器
/// </summary>
public static void StartServer()
{
Console.WriteLine("开启服务");
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService));
NetTcpBinding binding = new NetTcpBinding
{
Security =
{
Mode = SecurityMode.None,
Transport =
{
ClientCredentialType = TcpClientCredentialType.None
},
Message =
{
ClientCredentialType = MessageCredentialType.None
}
},
MaxBufferPoolSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
ReceiveTimeout = new TimeSpan(0, 0, 10, 0),
SendTimeout = new TimeSpan(0, 0, 10, 0)
};
serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, "net.tcp://127.0.0.1:2017");
serviceHost.Open();
Console.WriteLine("开启服务成功");
Console.ReadKey();
}
}
}
5、客户端回调协定接口实现类CalculateCallback
using System;
using System.Collections.Generic;
using System.Text;
using WcfCallbackService;
namespace WcfCallbackClient
{
/// <summary>
/// 回调协定接口的实现类
/// </summary>
public class CalculateCallback: ICallback
{
public void DisplayResult(double x, double y, double result)
{
Console.WriteLine("x + y = {2} when x = {0} and y = {1}", x, y, result);
}
}
}
6、客户端绑定相关属性类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.ServiceModel;
using System.Text;
namespace WcfCallbackClient
{
/// <summary>
/// 绑定相关属性类
/// </summary>
public static class TcpBindingInfo
{
private static string Ip = "127.0.0.1:2017";
/// <summary>
/// 绑定主客户端地址
/// </summary>
private static EndpointAddress _ipaddress;
/// <summary>
/// 绑定
/// </summary>
private static NetTcpBinding _tcpBinding;
public static EndpointAddress Endpointaddress
{
set { _ipaddress = value; }
get
{
_ipaddress = new EndpointAddress($"net.tcp://{Ip}");
return _ipaddress;
}
}
public static NetTcpBinding TcpBinding
{
set { _tcpBinding = value; }
get
{
_tcpBinding = new NetTcpBinding
{
Security =
{
Mode = SecurityMode.None,
Transport =
{
ClientCredentialType = TcpClientCredentialType.None
},
Message =
{
ClientCredentialType = MessageCredentialType.None
}
},
MaxBufferPoolSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
ReceiveTimeout = new TimeSpan(0, 0, 10, 0),
SendTimeout = new TimeSpan(0, 0, 10, 0)
};
return _tcpBinding;
}
}
}
}
7、客户端调用服务
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;
using WcfCallbackService;
namespace WcfCallbackClient
{
public class Program
{
static void Main(string[] args)
{
AddCallback();
Minus(20, 1);
Console.Read();
}
/// <summary>
/// 加法回调
/// </summary>
public static void AddCallback()
{
InstanceContext instanceContext = new InstanceContext(new CalculateCallback());
DuplexChannelFactory<ICalculator> channelFactory = new DuplexChannelFactory<ICalculator>(instanceContext, TcpBindingInfo.TcpBinding);
ICalculator proxy = channelFactory.CreateChannel(TcpBindingInfo.Endpointaddress);
if (proxy != null)
{
proxy.Add(2, 3);
}
}
/// <summary>
/// 调用减法服务
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public static void Minus(double x, double y)
{
InstanceContext instanceContext = new InstanceContext(new CalculateCallback());
DuplexChannelFactory<ICalculator> channelFactory = new DuplexChannelFactory<ICalculator>(instanceContext, TcpBindingInfo.TcpBinding);
ICalculator proxy = channelFactory.CreateChannel(TcpBindingInfo.Endpointaddress);
if (proxy != null)
{
double value = proxy.Minus(x, y);
}
Console.WriteLine($"x-y={x - y}");
}
}
}