您的位置:首页 > 其它

简单的WCF实例

2012-05-30 16:46 267 查看
在解决方案中新建两个项目,一个做服务端,一个做客户端,

在服务端的项目中添加新项,添加一个WCF服务,名为XServer ,会添加两个文件IXService.cs 和Xserver 如下

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace wcf_t2

{

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IXService”。

[ServiceContract]

public interface IXService

{

[OperationContract]

void DoWork();

[OperationContract]

string Test();

}

}

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace wcf_t2

{

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“XService”。

public class XService : IXService

{

public void DoWork()

{

}

public string Test()

{

string temp = "Hellow World!";

return temp;

}

}

}

在客户端的项目中加入IXService.cs文件,内容与服务端相同,下面在程序中通过代码即可在客户端实现对服务端XService.cs中方法的调用

服务端需要先开启服务

using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.ServiceModel;

using System.Net;

using System.Threading;

namespace wcf_t2

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private ServiceHost _host;

private void simpleButton1_Click(object sender, EventArgs e)

{

OpenSerice();

}

private void OpenSerice()

{

try

{

IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());

string srtAddress = string.Format(@"net.tcp://{0}:11230", ip[0]);

_host = new ServiceHost(typeof(XService));

_host.AddServiceEndpoint(typeof(IXService), new NetTcpBinding(), srtAddress);

_host.Open();

labelControl1.Text = "服务已开启"

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

}

}

}

客户端:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Net;

using System.Text;

using System.Windows.Forms;

using wcf_t2;

using System.ServiceModel;

using System.Threading;

namespace wcf_client

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public static IXService XService;

private void simpleButton1_Click(object sender, EventArgs e)

{

Thread thread = new Thread(Show);

thread.Start();

}

private void Show()

{

try

{

InitService();

while (true)

{

System.Threading.Thread.Sleep(100);

BeginInvoke(new MethodInvoker(() =>

labelControl1.Text = XService.Test()+count.ToString()

));

count++;

}

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

}

private int count = 0;

private bool InitService()

{

try

{

IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());

string srtAddress = string.Format(@"net.tcp://{0}:11230", ip[0]);

ChannelFactory<IXService> factory = new ChannelFactory<IXService>

(new NetTcpBinding(), new EndpointAddress(srtAddress));

XService = factory.CreateChannel();

}

catch

{

return false;

}

return true;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: