创建一个简单的WCF程序
1、创建一个Service项目(ConsoleApplication)
我们建立一个简单的案例,做一个计算器, 假设我们只要求他做简单的加法运算就可以了。在Service项目中新建一个WCF服务,名称为Calculator。再修改里面的接口及实现方法。
ICalculator接口:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
实现ICalculator的类CalculatorService:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
服务的配置文件:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
启动服务:
1
using System;
2
using System.Text;
3
using System.ServiceModel;
4
using System.ServiceModel.Channels;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
Console.WriteLine("Started. Press any key to quit.");
17
Console.ReadKey();
18
}
19
}
20
}
21
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
新建一个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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
客户端代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)