如何在 Azure 函数中定义 WCF 端点?

How to define WCF End Point in Azure function?

我一直在使用这个解决方案,其中客户端应用程序应该通过中继访问 WCF 服务。

我已按照本教程进行操作,并且能够使用客户端控制台应用程序访问控制台应用程序中托管的 WCF 服务。

我想要实现的是,通过函数应用程序访问本地机器上托管的 WCF 服务。

所以我将我在客户端控制台应用程序中所做的代码迁移到 azure 函数应用程序 here

客户端控制台应用程序有一个配置文件,如图所示 here

我有2个疑问

我有两个疑惑

1) 我无法理解如何在下面的控制台应用程序的情况下在 App.Config 文件中定义的 azure 函数应用程序中定义端点。

<client>
      <endpoint name="RelayEndpoint"
                      contract="Microsoft.ServiceBus.Samples.IEchoContract"
                      binding="netTcpRelayBinding"/>
    </client>

2)有什么方法可以在函数应用程序的代码中动态定义端点吗?

  log.Info("C# HTTP trigger function processed a request.");
            ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;

            string serviceNamespace = "MyTestRelay";
            string sasKey = "mpQKrfJ6L4Ftdsds2v6Leg3X0e9+Q8MOfjxwghj7xk2qSA=";


            Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "EchoService");
            TransportClientEndpointBehavior sasCredential = new TransportClientEndpointBehavior();
            sasCredential.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", sasKey);

            DynamicEndpoint dynamicEndpoint = new DynamicEndpoint(ContractDescription.GetContract(typeof(IEchoContract)), new WSHttpBinding() );

//我在下面的行中遇到错误

            ChannelFactory<IEchoChannel> channelFactory = new ChannelFactory<IEchoChannel>("RelayEndpoint", new EndpointAddress(serviceUri));

            channelFactory.Endpoint.Behaviors.Add(sasCredential);

            IEchoChannel channel = channelFactory.CreateChannel();
            channel.Open();

            Console.WriteLine("Enter text to echo (or [Enter] to exit):");
            string input = Console.ReadLine();
            while (input != String.Empty)
            {
                try
                {
                    Console.WriteLine("Server echoed: {0}", channel.Echo(input));
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
                input = Console.ReadLine();
            }

            channel.Close();
            channelFactory.Close();

任何人都可以建议如何使用它吗?

在代码中创建绑定的语法映射到 app.config 中的 XML,您可以像这样使用:

var endpoint = new EndpointAddress(serviceUri);
var binding = new NetTcpRelayBinding()
{
     // Example properties that might be in your app.config
     ReceiveTimeout = TimeSpan.FromMinutes(2),
     SendTimeout = TimeSpan.FromMinutes(2),
};

var channelFactory = new ChannelFactory<IEchoChannel>(binding, endpoint);