创建一个简单的WCF程序
1、创建一个Service项目(ConsoleApplication)
我们建立一个简单的案例,做一个计算器, 假设我们只要求他做简单的加法运算就可以了。在Service项目中新建一个WCF服务,名称为Calculator。再修改里面的接口及实现方法。
ICalculator接口:
实现ICalculator的类CalculatorService:
服务的配置文件:
启动服务:
1
using System;
2
using System.Text;
3
using System.ServiceModel;
4
using System.ServiceModel.Channels;
5
6
namespace Service
7
{
8
class Program
9
{
10
static void Main(string[] args)
11
{
12
using (ServiceHost srv = new ServiceHost(new CalculatorService()))
13
{
14
srv.Open();
15
16
Console.WriteLine("Started. Press any key to quit.");
17
Console.ReadKey();
18
}
19
}
20
}
21
}
using System;2
using System.Text;3
using System.ServiceModel;4
using System.ServiceModel.Channels;5

6
namespace Service7
{8
class Program9
{10
static void Main(string[] args)11
{12
using (ServiceHost srv = new ServiceHost(new CalculatorService()))13
{14
srv.Open();15

16
Console.WriteLine("Started. Press any key to quit.");17
Console.ReadKey();18
}19
}20
}21
}
新建一个Client项目(ConsoleApplication)
配置Endpoint
1
<?xml version="1.0" encoding="utf-8" ?>
2
<configuration>
3
<system.serviceModel>
4
<bindings>
5
<netTcpBinding>
6
<binding name="TCPBinding" closeTimeout="1.00:00:00" openTimeout="1.00:00:00"
7
receiveTimeout="1.00:00:00" sendTimeout="1.00:00:00" maxBufferPoolSize="2147483647"
8
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
9
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
10
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
11
<security mode="None">
12
<message clientCredentialType="None" />
13
</security>
14
</binding>
15
</netTcpBinding>
16
</bindings>
17
<client>
18
<endpoint address="net.tcp://localhost:30000/CalculatorService"
19
binding="netTcpBinding" bindingConfiguration="TCPBinding"
20
contract="Service.ICalculator" name="Calculator" />
21
</client>
22
</system.serviceModel>
23
</configuration>
24
<?xml version="1.0" encoding="utf-8" ?>2
<configuration>3
<system.serviceModel> 4
<bindings>5
<netTcpBinding>6
<binding name="TCPBinding" closeTimeout="1.00:00:00" openTimeout="1.00:00:00"7
receiveTimeout="1.00:00:00" sendTimeout="1.00:00:00" maxBufferPoolSize="2147483647"8
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">9
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"10
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />11
<security mode="None">12
<message clientCredentialType="None" />13
</security>14
</binding>15
</netTcpBinding>16
</bindings>17
<client>18
<endpoint address="net.tcp://localhost:30000/CalculatorService"19
binding="netTcpBinding" bindingConfiguration="TCPBinding"20
contract="Service.ICalculator" name="Calculator" />21
</client>22
</system.serviceModel>23
</configuration>24

客户端代码:


