我正在尝试在VS2005项目中实现WCF客户端。在VS2010中它一切正常,因为我只能添加服务引用。对于VS2005,我使用svcutil构建了.config和Service1.cs。这两个文件已添加到项目中,我还将服务引用添加为Web引用。 您还可以找到生成的两个文件。
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="SDTRfServerClass.WCFService.WSHttpBinding_IService1" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.0.102:8732/Design_Time_Addresses/Calc/Service1/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
contract="SDTRfServerClass.WCFService.IService1" name="WSHttpBinding_IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
这是app.config,我也尝试将其更改为web.config,但这没有任何区别。
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName = "IService1")]
public interface IService1
{
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/Add", ReplyAction = "http://tempuri.org/IService1/AddResponse")]
double Add(double n1, double n2);
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/Substract", ReplyAction = "http://tempuri.org/IService1/SubstractResponse")]
double Substract(double n1, double n2);
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/Multiply", ReplyAction = "http://tempuri.org/IService1/MultiplyResponse")]
double Multiply(double n1, double n2);
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/Divide", ReplyAction = "http://tempuri.org/IService1/DivideResponse")]
double Divide(double n1, double n2);
}
这是Service1.cs的一部分。 当我尝试使用以下代码调用服务时,我收到错误“无法找到名为WSHttpBinding_IService1的endpointelement并在ServiceMode-client的配置部分中签订IService1。如果应用程序的配置不是发现或找到了一个均衡名称的端点。 我试图在所有地方使用完整的解决方案名称更改名称。 有人作为解决方案吗?
client = new Service1Client("WSHttpBinding_IService1");
double result = client.Add(Double.Parse("2"), Double.Parse("4"));
我还尝试使用VS2005的插件来添加创建.map和.cs文件的服务引用,但我一直都会遇到同样的错误。
答案 0 :(得分:0)
我认为您遇到了一些拼写错误或命名空间错误,因为错误意味着.NET无法找到有关您的终端的信息。
尝试发布完整的web.config并查看它
或者你可以像这样使用动态频道工厂,不用担心链接到你的web.config
WSHttpBinding myBinding = new WSHttpBinding();
//define endpoint url (where service is deployed)
EndpointAddress myEndpoint = new EndpointAddress("http://192.168.0.102:8732/Design_Time_Addresses/Calc/Service1/"); //change to real endpoint
//Use channel factory instead of generated one
ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding, myEndpoint); //Change to you WCF interface
IService1 myServiceClient = myChannelFactory.CreateChannel();
//and call it
var result = myServiceClient.Add(1,1); //input to your method
//other methods
((IClientChannel)myServiceClient).Close();
myChannelFactory.Close();