[英]Exception While Consume WCF service
我嘗試使用 wcf 服務,但是當我從 C# 調用綁定信息時出現錯誤。 錯誤是“在配置中找不到與鍵匹配的元素”。
你能幫我嗎?
我已經檢查並且綁定配置名稱與 app.config 文件中的相同。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
WSHttpBinding binding = new WSHttpBinding("bindingconfig name");
EndpointAddress address = new EndpointAddress("endpoint address");
當我試圖調用它時,會拋出一個異常形式 WSHttpBinding object 綁定。
請使用客戶端代理 class 調用服務。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
比如下面的代碼段。
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>
客戶端代理將使用默認綁定和端點地址。 如果配置中生成了多個服務端點,我們應該通過客戶端代理的構造function來指定用於通信的服務端點。
ServiceReference1.TestServiceClient client = new ServiceReference1.TestServiceClient(“BasicHttpBinding_ITestService”);
此外,我們也可以使用 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);
}
如果有什么我可以幫忙的,請隨時告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.