C# 使用WCF服务时发生异常
我曾尝试使用wcf服务,但从C#调用绑定信息时出错。错误是“在配置中找不到与密钥匹配的元素”。C# 使用WCF服务时发生异常,c#,wcf,soap,C#,Wcf,Soap,我曾尝试使用wcf服务,但从C#调用绑定信息时出错。错误是“在配置中找不到与密钥匹配的元素”。 你能帮我吗 我已检查并绑定配置名称与app.config文件中的相同 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; WSHttpBinding binding = new WSHttpBinding("bindingconfig name"); EndpointAddress address = new Endpo
你能帮我吗 我已检查并绑定配置名称与app.config文件中的相同
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
WSHttpBinding binding = new WSHttpBinding("bindingconfig name");
EndpointAddress address = new EndpointAddress("endpoint address");
当我试图调用它时,抛出一个异常表单WSHttpBinding对象绑定。请使用客户端代理类调用该服务。
例如下面的代码段
ServiceReference1.TestServiceClient client = new ServiceReference1.TestServiceClient();
try
{
var result = client.GetResult();
Console.WriteLine(result);
}
catch (Exception)
{
throw;
}
client.Close();
配置(自动生成)。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ITestService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://10.157.13.69:16666/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ITestService" contract="ServiceReference1.ITestService"
name="BasicHttpBinding_ITestService" />
</client>
</system.serviceModel>
此外,我们也可以使用ChannelFacotry调用该服务,但它们本质上是相同的
Uri uri = new Uri("http://10.157.13.69:16666");
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(binding,new EndpointAddress(uri));
ICalculator service = factory.CreateChannel();
try
{
var result = service.Add(34.32, 2.34);
Console.WriteLine(result);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw;
}
}
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add(double a, double b);
}
Uri=新的Uri(“http://10.157.13.69:16666");
BasicHttpBinding=新的BasicHttpBinding();
ChannelFactory=newchannelfactory(绑定,新端点地址(uri));
ICalculator服务=factory.CreateChannel();
尝试
{
var结果=服务添加(34.32,2.34);
控制台写入线(结果);
}
捕获(例外e)
{
Console.WriteLine(如ToString());
投掷;
}
}
[服务合同]
公共接口计算器
{
[经营合同]
双加(双a,双b);
}
如果有什么我可以帮忙的,请随时告诉我。您的(web/app).config文件是什么样子的?
Uri uri = new Uri("http://10.157.13.69:16666");
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(binding,new EndpointAddress(uri));
ICalculator service = factory.CreateChannel();
try
{
var result = service.Add(34.32, 2.34);
Console.WriteLine(result);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw;
}
}
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add(double a, double b);
}