VS自带WCF测试客户端简单介绍

来源:互联网 发布:基2fft算法 编辑:程序博客网 时间:2024/05/08 23:10

在目前的二次开发项目中,一些信息是放在客户那里的,只给你一个服务地址,不知道具体有什么方法,每次想调用一个服务不知道能不能实现目前的需求,只能测试。写个测试程序真的划不来,占用时间不说,而且你忙了一上午,发现那个服务,并不是你想要的。只能说白忙了......下面简单介绍一下,从同事那里学到的怎么使用VS自带的测试客户端。操作很简单,但很实用。知道这个的,就不用说了,这篇文章就是帮助那些不知道的小伙伴的......

一个简单的WCF服务端:

契约:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace Wolfy.Contract
{
[ServiceContract(Namespace="http://www.wolfy.com")]
publicinterface ICalculator
{
[OperationContract]
doubleAdd(double x,double y);
}
}

服务:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wolfy.Contract;
namespace Wolfy.Service
{
publicclass CalculatorService : ICalculator
{
#region ICalculator 成员
publicdouble Add(doublex, double y)
{
returnx + y;
}
#endregion
}
}

这里就用控制台来承载服务了

控制台代码开启服务:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wolfy.Contract;
using Wolfy.Service;
using System.ServiceModel;
namespace Wolfy.Server
{
classProgram
{
staticvoid Main(string[] args)
{
using(ServiceHost host = newServiceHost(typeof(CalculatorService)))
{
host.Opened +=delegate
{
Console.WriteLine("calculatorService已启动,按任意键停止服务");
};
host.Open();
Console.Read();
}
}
}
}

服务端配置文件:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0"sku=".NETFramework,Version=v4.5"/>
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Wolfy.Service.CalculatorService"behaviorConfiguration="MyServiceBehavior">
<endpoint contract="Wolfy.Contract.ICalculator"binding="wsHttpBinding"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:8888/calculatorservice"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>

一个简单的WCFdemo算完成了,现在开启服务....

打开VS测试客户端

在开发人员命令提示中输入:wcftestclient回车

然后右键我的项目:输入你的服务地址,当然这里你的服务要在开启的状态。

为参数赋值:

结果:

很简单吧,虽然不是很高深的东西,但很实用,如果您觉得对你有所帮助,那就【推荐】一下吧,毕竟大家知道才是真的知道。

0 0
原创粉丝点击