在目前的二次開發項目中,一些信息是放在客戶那裡的,只給你一個服務地址,不知道具體有什麼方法,每次想調用一個服務不知道能不能實現目前的需求,只能測試。寫個測試程序真的劃不來,占用時間不說,而且你忙了一上午,發現那個服務,並不是你想要的。只能說白忙了......下面簡單介紹一下,從同事那裡學到的怎麼使用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();
}
}
}
}
服務端配置文件:
<?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回車

然後右鍵我的項目:輸入你的服務地址,當然這裡你的服務要在開啟的狀態。


為參數賦值:

結果:

很簡單吧,雖然不是很高深的東西,但很實用,如果您覺得對你有所幫助,那就【推薦】一下吧,畢竟大家知道才是真的知道。
作者:Wolfy
出處:http://www.cnblogs.com/wolf-sun/