Welcome

首页 / 软件开发 / WCF / VS自带WCF测试客户端简介

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

一个简单的WCF服务端:

契约:

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")]public interface ICalculator{[OperationContract]double Add(double x,double y);}}
服务:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Wolfy.Contract;namespace Wolfy.Service{public class CalculatorService : ICalculator{#region ICalculator 成员public double Add(double x, double y){return x + y;}#endregion}}
这里就用控制台来承载服务了

控制台代码开启服务:

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{class Program{static void Main(string[] args){using (ServiceHost host = new ServiceHost(typeof(CalculatorService))){host.Opened += delegate{Console.WriteLine("calculatorService已启动,按任意键停止服务");};host.Open();Console.Read();}}}}