简体   繁体   中英

WCF Runtime Exception “Could not find default endpoint element that references…”

I am beginner to WCF. What I was trying to do is generating Proxyclass using ClientBase<> method where Program is compiled successfully but its getting runtime exception like,

"An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll Additional information: Could not find default endpoint element that references contract 'ServiceContract.ICalculator' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."

I don't get why is it happening eventhough I have given endPoints in configuration file. (I am using Self-Hosting)

Below is my code.

<---ClientSide--->

Program.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 (Same file at both side, Client & Host)

<?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>

<---ServiceContract--->

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);
}
}

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

Try endpoint definition like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM