编程实现WCF客户端调用

新建一个控制台项目 Wcf.Client,添加对契约项目Wcf.Contract 的引用

 1 using System;
 2 using System.ServiceModel;
 3 using Wcf.Contract;
 4 
 5 namespace Wcf.Host
 6 {
 7     class MainClass
 8     {
 9         public static void Main()
10         {
11             ChannelFactory<IOperation> channelFactory = new ChannelFactory<IOperation> (
12                 new BasicHttpBinding (), "http://localhost:8081/operation");
13             try
14             {
15                 var proxy = channelFactory.CreateChannel ();
16                 var result = proxy.Add (3, 5);
17                 Console.WriteLine (result);
18             }
19             catch(Exception ex) {
20                 //Console.BackgroundColor = ConsoleColor.Red;
21                 Console.WriteLine (ex.Message);
22                 //Console.ResetColor ();
23             }
24             Console.WriteLine ("press any key to Continue...");
25             Console.ReadKey ();
26         }
27         }

这样就简单实现了一个WCF客户端的调用,不要使用添加服务引用,也不要任何配置文件。

原文地址:https://www.cnblogs.com/yayaxxww/p/4282759.html