WCF之“hello world”(一)
WCF,一个集成了安全,事务等一大堆功能于一身的通信(communication)框架。概念在网上很多了。不做多的介绍了,下面会以经典的“hello world”例子为中轴,深度剖析WCF的内部执行过程。首先还是把最原始的工程给展现出来
下面是整个Solution的结构图:
![](https://www.cnblogs.com/images/cnblogs_com/tylerlan/solution1.jpg)
从上面的截图可以看出,整个Solution包括了4个project.
1.Tyler.Contract:契约设计类(类库),定义了契约
2.Tyler.Service:服务类,实现了IHelloworld契约(类库)
3.Tyler.Host:WCF服务的宿主(Windows Console)
4.Tyler.Client:WCF客户端(Windows Console)
这是一个非常简单的解决方案,实现的功能也非常简单,就是从服务端得到一个字符串。下面将4个Project的代码粘出来。
Tyler.Contract.契约设计
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
//项目第一步:建立contract
namespace Tyler.Contract
{
[ServiceContract]
public interface IHelloWorld
{
[OperationContract]
string GetString(string input);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
//项目第一步:建立contract
namespace Tyler.Contract
{
[ServiceContract]
public interface IHelloWorld
{
[OperationContract]
string GetString(string input);
}
}
注意必须添加using System.ServiceModel;引用
Tyler.Service:实现契约,服务类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Tyler.Contract;
//第二步:实现契约
namespace Tyler.Service
{
public class MyService:IHelloWorld
{
#region 实现接口定义的方法(实现契约)
public string GetString(string input)
{
return "your input string is:" + input;
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Tyler.Contract;
//第二步:实现契约
namespace Tyler.Service
{
public class MyService:IHelloWorld
{
#region 实现接口定义的方法(实现契约)
public string GetString(string input)
{
return "your input string is:" + input;
}
#endregion
}
}
注意添加对uSystem.ServiceModel;Tyler.Contract;的引用
Tyler.Host
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tyler.Contract;
using Tyler.Service;
using System.ServiceModel;
//第三步:创建服务
namespace Tyler.Host
{
class Program
{
static void Main(string[] args)
{
BeginRequest();
}
static void BeginRequest()
{
using (ServiceHost host = new ServiceHost(typeof(MyService)))
{
host.Opened += delegate
{
Console.WriteLine("Server: Begins to listen request");
};
host.Open();
Console.Read();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tyler.Contract;
using Tyler.Service;
using System.ServiceModel;
//第三步:创建服务
namespace Tyler.Host
{
class Program
{
static void Main(string[] args)
{
BeginRequest();
}
static void BeginRequest()
{
using (ServiceHost host = new ServiceHost(typeof(MyService)))
{
host.Opened += delegate
{
Console.WriteLine("Server: Begins to listen request");
};
host.Open();
Console.Read();
}
}
}
}
请注意对System.ServiceModel.Tyler.Contract.Tyler.Service的引用
host的app.config文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Tyler.Service.MyService">
<endpoint address="" binding="basicHttpBinding" contract="Tyler.Contract.IHelloWorld"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:9876/helloworld"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
<configuration>
<system.serviceModel>
<services>
<service name="Tyler.Service.MyService">
<endpoint address="" binding="basicHttpBinding" contract="Tyler.Contract.IHelloWorld"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:9876/helloworld"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
很简单,网上已经做了很详细的介绍。不多作说明
Tyler.Client:客户端实现
MyClient.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Tyler.Contract;
namespace Tyler.Client
{
class MyClient:ClientBase<IHelloWorld>,IHelloWorld
{
public MyClient()
: base()
{ }
public string GetString(string input)
{
return this.Channel.GetString(input);
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Tyler.Contract;
namespace Tyler.Client
{
class MyClient:ClientBase<IHelloWorld>,IHelloWorld
{
public MyClient()
: base()
{ }
public string GetString(string input)
{
return this.Channel.GetString(input);
}
}
}
客户端Main函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tyler.Client
{
class Program
{
static void Main(string[] args)
{
using (MyClient client = new MyClient())
{
Console.WriteLine("Client:"+client.GetString("hello world"));
Console.Read();
}
}
}
}
客户端app.config实现
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:9876/helloworld" binding="basicHttpBinding" contract="Tyler.Contract.IHelloWorld"></endpoint>
</client>
</system.serviceModel>
</configuration>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:9876/helloworld" binding="basicHttpBinding" contract="Tyler.Contract.IHelloWorld"></endpoint>
</client>
</system.serviceModel>
</configuration>
下面是运行的结果:
server端启动:
![](https://www.cnblogs.com/images/cnblogs_com/tylerlan/service.jpg)
客户端启动:
![](https://www.cnblogs.com/images/cnblogs_com/tylerlan/client.jpg)
很简单的一个程序,后面的会一步一步的剖析这个简单程序的内部执行流程。让人对wcf内部运行过程有一个直接的认识。这样对wcf的学习就事半功倍了!
源代码可以在这里下载/Files/TylerLan/Tyler.FirstSolution.rar