首页 > 解决方案 > 如何使用代理访问 .net core 中的 wcf 服务

问题描述

我在我的代码(.net core 2.1)中添加了一个 wcf 服务,但是我不能直接访问这个服务,我需要使用代理来做到这一点,我不知道如何在我的代码中设置代理。当我添加 wcf 服务时,vs 会生成一个像这样的 json 文件:

{
  "ProviderId": "Microsoft.VisualStudio.ConnectedService.Wcf",
  "Version": "15.0.20628.921",
  "GettingStartedDocument": {
    "Uri": "https://go.microsoft.com/fwlink/?linkid=858517"
  },
  "ExtendedData": {
    "Uri": "http://xxxxx/eisp-zk/ws/zkiService?wsdl",
    "Namespace": "EispService",
    "SelectedAccessLevelForGeneratedClass": "Public",
    "GenerateMessageContract": false,
    "ReuseTypesinReferencedAssemblies": true,
    "ReuseTypesinAllReferencedAssemblies": true,
    "CollectionTypeReference": {
      "Item1": "System.Array",
      "Item2": "System.Runtime.dll"
    },
    "DictionaryCollectionTypeReference": {
      "Item1": "System.Collections.Generic.Dictionary`2",
      "Item2": "System.Collections.dll"
    },
    "CheckedReferencedAssemblies": [],
    "InstanceId": null,
    "Name": "EispService",
    "Metadata": {}
  }
}

我想知道如何设置代理。谁能帮帮我?</p>

标签: wcfproxy.net-core

解决方案


Connectedservice.json 包含要访问的服务的端点信息和配置信息。您应该使用自动生成的代理类通过该代理类访问服务。

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
var result = client.SayHelloAsync();
Console.WriteLine(result.Result);

在此处输入图像描述
https://docs.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide
下面是网络框架项目中调用的例子。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
如果有什么我可以帮忙的,请随时告诉我。
更新。
代理设置通常在绑定设置中完成。

        <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IService"  proxyAddress="http://abcd"/>
        </wsHttpBinding>
    </bindings>

在基于 Core 的项目中,我们通常通过 调用服务Microsoft WCF Web Service Reference Provider,绑定配置在Reference.cs. 相关链接。https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.basichttpbinding.proxyaddress?redirectedfrom=MSDN&view=netframework-4.0#System_ServiceModel_BasicHttpBinding_ProxyAddress
在此处输入图像描述


推荐阅读