简单的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;
}
}
}
在服务端的项目中添加新项,添加一个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;
}
}
}
相关文章推荐
- 【WCF】最简单的WCF实例
- WCF的简单实例(手写代码实现)--服务端
- WCF 学习总结1 -- 简单实例 (系列文章)
- [WCF REST] 一个简单的REST服务实例
- WCF的简单实例(手写代码实现)--客户端
- 一个简单的WCF开发实例
- 使用WC“.NET研究”F实现SOA面向服务编程——简单的WCF开发实例
- WCF简单实例(VS2010自动生成)--服务端
- (C#)一个WCF简单实例
- WCF简单实例(VS2010自动生成)--客户端
- WCF 学习总结1 -- 简单实例
- WCF duplex 简单实例
- 【MSH】如何开发第一个最简单的WCF模块服务(附实例源码)
- (C#)一个WCF简单实例
- 使用W“.NET技术”CF实现SOA面向服务编程——简单的WCF开发实例
- .net WCF简单实例详解(5)
- WCF简单实例---同步调用
- 一个简单的WCF实例
- WCF寄宿(Host)之自我寄宿(Self-Hosting)简单实例【Console应用为宿主】