model:
using Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
{
public class Client
{
public String Id { get; set; }
public ICallBack CallBack { get; set; }
}
}
service:
using Models;
using Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace Service
{
public class NoticeOperator : INoticeOperator
{
private static List<Client> clientList = new List<Client>();
public void Register(string id)
{
Client client = clientList.Find(l => l.Id == id);
if (client != null)
{
clientList.Remove(client);
}
ICallBack callBack = OperationContext.Current.GetCallbackChannel<ICallBack>();
clientList.Add(new Client() { Id = id, CallBack = callBack });
Console.WriteLine("register:" + id);
}
public void UnRegister(string id)
{
Console.WriteLine("unRegister:" + id);
Client client = clientList.Find(c => c.Id == id);
if (client != null)
{
clientList.Remove(client);
}
}
public void SendMessage(string from, string to, string message)
{
Client client = clientList.Find(c => c.Id == to);
if (client != null)
{
String longMessage = String.Format("message from {0} to {1} at {2} : {3}", from, to, DateTime.Now.ToString("HH:mm:ss"), message);
Console.WriteLine(longMessage);
client.CallBack.Notice(message);
}
}
}
}
interface:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace Service.Interface
{
public interface ICallBack
{
[OperationContract(IsOneWay = true)]
void Notice(String message);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace Service.Interface
{
[ServiceContract(CallbackContract = typeof(ICallBack))]
public interface INoticeOperator
{
[OperationContract]
void Register(String id);
[OperationContract]
void UnRegister(String id);
[OperationContract]
void SendMessage(String from, String to, String message);
}
}
挂起服务host:
using Service;
using Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
namespace Hosting
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(NoticeOperator)))
{
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
host.AddServiceEndpoint(typeof(INoticeOperator), binding, "net.tcp://111.111.1.111:1111/NoticeOperator");
host.Opened += (s, e) => Console.WriteLine("service is running...");
host.Open();
Console.ReadLine();
}
}
}
}
调用:
using System.Collections.Generic;
using System.Data;
using System.ServiceModel;
using System;
using Service.Interface;
namespace Test
{
/// <summary>
/// </summary>
public class MessageBLL
{
/// <summary>
/// Wcf接口实例
/// </summary>
private INoticeOperator _proxy;
private INoticeOperator proxy
{
get
{
if (_proxy == null)
{
System.ServiceModel.InstanceContext context = new System.ServiceModel.InstanceContext(new CallBack());
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
System.ServiceModel.ChannelFactory<INoticeOperator> factory = new DuplexChannelFactory<INoticeOperator>(context, binding, "net.tcp://111.111.1.111:1111/NoticeOperator");
_proxy = factory.CreateChannel();
}
return _proxy;
}
}
/// <summary>
/// 声明全局接口或类
/// </summary>
private static MessageBLL _instance = new MessageBLL();
/// <summary>
/// 外部接口获取实例
/// </summary>
/// <returns></returns>
public static MessageBLL GetInstance()
{
return _instance;
}
/// <summary>
/// 注册
/// </summary>
/// <param name="name"></param>
public void Register(string name)
{
proxy.Register(name);
}
/// <summary>
/// 发送
/// </summary>
/// <param name="name"></param>
public void SendMessage(string from, string to, string message)
{
proxy.SendMessage(from, to, message);
}
}
}