WCF 是微软分布式系统开发中的一个重量级产品,也是.net中不得不学的一个通信框架,本文通过利用WCF实现一个局域网聊天,希望给大家做个抛砖引玉的作用。
下面是项目的主要结构
/// <summary>
/// 通信载体接口
/// </summary>
[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface IMessagesInterface
{
[OperationContract]
string DoMessage(string text);
}
public class NamedPipeServer
{
private readonly ServiceHost serverHost;
public string SelfName { get; set; }
public List<string> ClientNames { get; set; }
public NamedPipeServer(IMessagesInterface messageInterface, ClientNames selfName)
{
SelfName = selfName.ToString();
ClientNames = new List<string>();
RegisterClientName();
serverHost = new ServiceHost(messageInterface);
serverHost.AddServiceEndpoint((typeof(IMessagesInterface)), new NetNamedPipeBinding(), "net.pipe://localhost/Server/" + SelfName);
serverHost.Open();
}
/// <summary>
/// 调用远程 DoMessage,发送消息给远程
/// </summary>
/// <param name="clientName"></param>
/// <param name="text">消息正文</param>
/// <returns>发送结果</returns>
public void SendMessage(string clientName, string text)
{
var msgInterface = GetMessagesInterface(clientName);
if (msgInterface == null) return;
try
{
msgInterface.DoMessage(text);
}
catch (Exception ex)
{
}
finally
{
CloseChannel((ICommunicationObject)msgInterface);
}
}
/// <summary>
/// 打开通道 并 获取可供调用的远程接口
/// </summary>
/// <param name="clientKey"></param>
/// <returns></returns>
public IMessagesInterface GetMessagesInterface(string clientKey)
{
try
{
var factory = new ChannelFactory<IMessagesInterface>(new NetNamedPipeBinding(),
new EndpointAddress("net.pipe://localhost/Server/" + clientKey));
return factory.CreateChannel();
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 关闭通道
/// </summary>
/// <param name="channel"></param>
/// <returns></returns>
private string CloseChannel(ICommunicationObject channel)
{
try
{
channel.Close();
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
channel.Abort();
}
return null;
}
private void RegisterClientName()
{
foreach (var name in Enum.GetNames(typeof(ClientNames)))
{
ClientNames.Add(name);
}
}
}
public enum ClientNames
{
win1, win2, win3, win4
}
core中封装了两个类,一个是通信载体接口,一个是服务的宿主,至于具体的服务实现,每个的实现都不一样。
通过枚举ClientNames 注册各自的名字,每个win 都持有所有的服务名,想给谁发信息,只用告知宿主,对方的名字,就能自己寻找到对方。
core封装了通信过程,每个人都不用去管通信,只用实现通信载体,就行了 。
代码有点抽象,不宜理解,但是结合代码,还是很好理解的。
源码:http://pan.baidu.com/s/1gdwGtwv