Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF service client configuration from appsetting.json (.NET Core)

Is there any way to configure WCF Service Reference from configuration file? I would like to configure WCF service reference with such settings as SecurityMode, Address, ReaderQuotas etc. I would also like to be able to choose between WsHttpBinding, BasicHttpBinding, BasicHttpsBinging etc (like normal configuration provided by app.config in .NET Framework).

Is there any way to achive that in .NET Core/.NET Standard?

Thank, Bartek

like image 420
Bartek Chyży Avatar asked Nov 07 '22 09:11

Bartek Chyży


1 Answers

Certain binding, such as Wshttpbinding,Netnamedbinding is not compatible with DotNet Core framework. Consequently, we could not configure it. However, this doesn’t represent that we can’t configure Basichttpbinding, Nettcpbinding.
At present, the WCF service cannot be created by using DotNet Core without using the third-party library. Moreover, WCF client based on DotNet Core just a compatible workaround.
https://github.com/dotnet/wcf
Like the DotNet Framework project, Microsoft Corporation provides Microsoft WCF Web Service Reference Provider tool to generate a client proxy.
https://learn.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide
After adding connected service, it shall generate a new namespace contains the client proxy class. Most of the client configuration located in the Reference.cs.
Also, we could manually program the code to call the WCF service.

class Program
    {
        static void Main(string[] args)
        {
            //using the automatically generated client proxy lcoated in the Reference.cs file to call the service.
            //ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
            //var result = client.TestAsync();
            //Console.WriteLine(result.Result);

            //using the Channel Factory to call the service.
            Uri uri = new Uri("http://10.157.13.69:21012");
            BasicHttpBinding binding = new BasicHttpBinding();
            ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
            IService service = factory.CreateChannel();
            var result = service.Test();
            Console.WriteLine(result);
        }
    }
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string Test();

    }

https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
Feel free to let me know if there is anything I can help with.

like image 125
Abraham Qian Avatar answered Nov 12 '22 19:11

Abraham Qian