How to define WCF End Point in Azure function?

1.1k views Asked by At

I have been working with this solution where A client application is supposed to access WCF service via Relay.

I have followed this tutorial and was able to access WCF service hosted in console app with client console app.

What I want to achieve is, To access WCF service hosted in local machine via a function app.

So I migrated the code which i did in client console app as shown here to the azure function app.

The client console app had a config file as shown here

I have 2 doubts

I have two doubts.

1) I can not understand how to define endpoint in azure function app which was defined in App.Config file in case of console app as below.

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

2) Is there any way to dynamically define endpoint right in the code of function app?

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

//I AM GETTING ERROR IN Below Line

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

Can anyone suggest how to work with this?

1

There are 1 answers

3
joehoper On BEST ANSWER

The syntax for creating bindings in code maps to the XML in app.config and you can use like so:

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