我是WCF的初學者。我試圖做的是使用ClientBase <產生Proxyclass>方法,其中程序編譯成功,但它讓運行時異常喜歡,WCF運行時異常「無法找到引用的默認端點元素...」
「‘System.InvalidOperationException’類型的未處理的異常出現在System.ServiceModel.dll 附加信息:找不到在ServiceModel客戶端配置部分中引用合同'ServiceContract.ICalculator'的默認端點元素,這可能是因爲沒有爲您的應用程序找到配置文件,或者因爲沒有找到匹配此合同的端點元素客戶端元素「。
我不明白爲什麼會發生這種情況,儘管我在配置文件中給出了endPoints。 (我正在使用Self-Hosting)
以下是我的代碼。
< --- ---客戶機側>
的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(在相同的文件雙方,客戶端&主機)
<?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);
}
}