WCF运行时异常”;找不到引用..的默认终结点元素&”;

本文关键字:结点 默认 元素 引用 运行时 异常 找不到 WCF | 更新日期: 2023-09-27 18:19:29

我是WCF的初学者。我尝试使用ClientBase<>生成Proxyclass方法,其中程序被成功编译,但它得到运行时异常,如

"System.ServiceModel.dll中发生类型为System.InvalidOperationException的未处理异常其他信息:在ServiceModel客户端配置节中找不到引用约定"ServiceContract.ICalculator"的默认终结点元素。这可能是因为找不到应用程序的配置文件,或者因为在客户端元素中找不到与此约定匹配的端点元素。"

尽管我在配置文件中给出了端点,但我不明白为什么会发生这种情况。(我正在使用自托管)

下面是我的代码。

&lt---客户端--->

程序.cs

namespace Client
{
public class Program
{
    public static void Main(string[] args)
    {
        CalculatorProxy client = new CalculatorProxy();
        //CalcService.CalculatorClient client = new CalcService.CalculatorClient();
        Console.WriteLine("Addition of 5 & 6 is " + client.Add(5, 6));
        Console.ReadLine();
    }
}
}

CalculatorProxy.cs

namespace Client
{
class CalculatorProxy: ClientBase<ICalculator>, ICalculator
{
    public double Add(double n1, double n2)
    {
        return base.Channel.Add(n1, n2);
    }
    public double Subtract(double n1, double n2)
    {
        return base.Channel.Subtract(n1, n2);
    }
    public double Multiply(double n1, double n2)
    {
        return base.Channel.Multiply(n1, n2);
    }
    public double Divide(double n1, double n2)
    {
        return base.Channel.Divide(n1, n2);
    }
}
}

App.config(两侧相同的文件,客户端和主机)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding_ICalculator" />
    </wsHttpBinding>
  </bindings>
  <client>
    <endpoint address="http://localhost:8080/ServiceModelSamples/Service/CalculatorService"
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator"
      contract="ICalculator" name="WSHttpBinding_ICalculator" />
  </client>
</system.serviceModel>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

&lt---服务合同--->

namespace ServiceContract
{
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract]
    double Add(double n1, double n2);
    [OperationContract]
    double Subtract(double n1, double n2);
    [OperationContract]
    double Multiply(double n1, double n2);
    [OperationContract]
    double Divide(double n1, double n2);
}
}

WCF运行时异常”;找不到引用..的默认终结点元素&”;

将端点中的合约属性值更改为ServiceContract.ICalculator,因为它应该是完全合格的

尝试这样的端点定义:

<endpoint address="http://localhost:8080/ServiceModelSamples/Service/CalculatorService"
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator"
      contract="ServiceContract.ICalculator" name="WSHttpBinding_ICalculator" />