首先定义要访问的接口。
[ServiceContract]
public interface IStockService
{
[OperationContract]
double GetPrice(string ticler);
}
客户端创建信道来与服务进行通信。
private static void GetWCFCode()
{
ChannelFactory<IStockService> myChannelFactory =
new ChannelFactory<IStockService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/WCFGetPrice"));
IStockService wcfClient = myChannelFactory.CreateChannel();
double d = wcfClient.GetPrice("Code");
Console.WriteLine("调用服务返回:" + d);
Console.ReadKey();
}
调用结果如图: