无法通过 Class 库使用 WCF 服务客户端

Can't use WCF service client via Class Library

我在 http://localhost:20999/WCFService

上创建了一个简单的 WCF 服务

我可以创建一个客户端来使用控制台应用程序调用 Web 服务方法,但是在 class 库中创建 WCF 客户端时出现错误。

Could not find default endpoint element that references contract ...


然后我创建了一个简单的测试应用程序,包含 ConsoleApp 和 ClassLib。两个项目都将 WCF 服务添加为服务引用。

Class 1个包含测试函数

public void Test()
    {
        ServiceReference2.TSI7WCFServiceClient client = new ServiceReference2.TSI7WCFServiceClient();
        Console.WriteLine(client.Invoke("123", "456"));
    }

和 ConsoleApp Program.cs

    Console.WriteLine("---- Call WCF directly ----");
    ServiceReference1.TSI7WCFServiceClient client = new ServiceReference1.TSI7WCFServiceClient();
    String result = client.Invoke("Test", "This is Parameter");
    Console.WriteLine("Result = {0}", result);
    client.Close();

    Console.WriteLine("\n\n---- Call WCF via ClassLib ----");
    ClassLibrary1.Class1 clientLib = new ClassLibrary1.Class1();
    clientLib.Test();

这是结果

确保将端点信息从 class 项目的 app.config 复制到控制台项目的 app.config。