一、用WCF模板创建一个简单的WCF
WCF服务应用程序,在IService1类中添加string helloworld()接口,在Service1.svc中实现helloworld()接口,运行,会得到http地址,需要代理来实现,新建一个windows窗体应用程序,在引用中添加服务引用,将这个地址放在其中,生成localhost代理,在Service References文件夹中。在Form窗口中添加一个button用于显示
private void button1_Click(object sender, EventArgs e)
{
localhost.Service1Client client = new winClient.localhost.Service1Client();
string str = client.helloworld();
MessageBox.Show(str);
}
二、使用配置文件部署WCF
(1)创建类库,添加接口类
添加引用System.ServiceModel和命名空间using System.ServiceModel;
using System.ServiceModel;
using System.Data;
namespace ClassLibrary1
{
public interface myInterface
{
string getServerTime();
DataTable getData();
}
}
(2)创建windows窗体应用程序作为服务端
添加System.ServiceModel和ClassLibrary1引用,继承并实现ClassLibrary1.myInterface接口
class myClass:ClassLibrary1.myInterface
{
public string getServerTime()
{
return DateTime.Now.ToString();
}
public System.Data.DataTable getData()
{
DataTable dt = new DataTable("books");
DataColumn dc1=new DataColumn("id");
DataColumn dc2 = new DataColumn("name");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow dr = dt.NewRow();
dr["id"] = "1001";
dr["name"] = "WCF";
dt.Rows.Add(dr);
return dt;
}
}
(3)使用配置文件部署WCF服务
<configuration>
<system.serviceModel>
<services>
<service name="WinServe.myClass" behaviorConfiguration="testBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8002/test"/>
</baseAddresses>
</host>
<endpoint address="" binding="baseHttpBinding" contract="ClassLibrary1.myInterface"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="testBehavior">
<serviceMetadata httpGetEnabled="true"></serviceMetadata>/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
<service name="WinServe.myClass" behaviorConfiguration="testBehavior">
(4)Form实现通信
添加命名空间using ClassLibrary1;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ServiceHost host;
private void button1_Click(object sender, EventArgs e)
{
host = new ServiceHost(typeof(WinServe.myClass));
host.Open();
label1.Text = "start";
}
}
将WinServe作为启动项目,运行后发现提示错误,什么端口没有权限,该死,试了一会终于发现问题!vs需要用管理员运行才会有权限使用这些端口号。用管理员运行后果然没问题了!
在网页中打开http://localhost:8002/test,会看见myClass服务。
三、使用代码部署WCF
(1)创建一个windows窗体应用程序
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ServiceHost host = null;
private void button1_Click(object sender, EventArgs e)
{
host = new ServiceHost(typeof(WinServe2.myClass));
//创建一种绑定形式
NetTcpBinding tcp = new NetTcpBinding();
//地址
Uri address=new Uri("net.tcp://localhost:8001/test");
//添加服务的端点,契约,绑定,地址
host.AddServiceEndpoint(typeof(ClassLibrary1.myInterface), tcp, address);
host.Opened += delegate { label1.Text = "start"; };
host.Open();
}
private void button2_Click(object sender, EventArgs e)
{
if (host.State != CommunicationState.Closed)
{
host.Close();
label1.Text = "end";
}
}
}
(2)创建客户端WinClient
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
NetTcpBinding bind = new NetTcpBinding();
EndpointAddress address = new EndpointAddress("net.tcp://localhost:8001/test");
//通道工厂
ChannelFactory<ClassLibrary1.myInterface> factory = new ChannelFactory<myInterface>(bind,address);
ClassLibrary1.myInterface myobject = factory.CreateChannel();
string time = myobject.getServerTime();
MessageBox.Show(time);
}
}