简体   繁体   English

使用 WCF 服务时出现异常

[英]Exception While Consume WCF service

I have tried to consume wcf service but I am getting error when I called the Binding Information from C#.我尝试使用 wcf 服务,但是当我从 C# 调用绑定信息时出现错误。 Error is "no elements matching the key were found in the configuration".错误是“在配置中找不到与键匹配的元素”。
Can you help me out?你能帮我吗?

I have checked and binding configuration name is same as in app.config file.我已经检查并且绑定配置名称与 app.config 文件中的相同。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
WSHttpBinding binding = new WSHttpBinding("bindingconfig name");
EndpointAddress address = new EndpointAddress("endpoint address");

When I tried to call it throws an exception form WSHttpBinding object binding.当我试图调用它时,会抛出一个异常形式 WSHttpBinding object 绑定。

Please use the client proxy class to call the service.请使用客户端代理 class 调用服务。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
Such as the below code segments.比如下面的代码段。

ServiceReference1.TestServiceClient client = new ServiceReference1.TestServiceClient();
try
{
    var result = client.GetResult();
    Console.WriteLine(result);
}
catch (Exception)
{
    throw;
}
client.Close();

Configuration(auto-generated).配置(自动生成)。

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

the client proxy will use the default binding and endpoint address.客户端代理将使用默认绑定和端点地址。 If the multiply service endpoints are generated in the configuration, we should specify the service endpoint used to communication by the construction function of the client proxy.如果配置中生成了多个服务端点,我们应该通过客户端代理的构造function来指定用于通信的服务端点。

ServiceReference1.TestServiceClient client = new ServiceReference1.TestServiceClient(“BasicHttpBinding_ITestService”);

Besides, we could also use ChannelFacotry to call the service, but they are essentially the same.此外,我们也可以使用 ChannelFactry 来调用服务,但它们本质上是一样的。

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

Feel free to let me know if there is anything I can help with.如果有什么我可以帮忙的,请随时告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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