Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume WCF service programmatic in .Net core 3.1 version

I have a WCF service, which was developed using the .Net framework 4.7.

Now I have to validate & Parse the WCF Service programmatically using .Net Core3.1 Web Application without adding the WCF Service as a Service Reference/Add Connected Service options in Visual Studio Solution Explorer

like image 345
user3237193 Avatar asked Oct 27 '22 18:10

user3237193


1 Answers

We can also use the channel factory to call WCF services, this method does not need to add a service reference,here is a demo:

            BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
            var address = new EndpointAddress("http://localhost:801/Service1.svc/Service");
           
            var factory = new ChannelFactory<IService1>(basicHttpBinding, address);
            IService1 channel = factory.CreateChannel();
            channel.GetData(1);
            Console.WriteLine(channel.GetData(1));
            Console.ReadLine();

On the client side, we need to have a ServiceContract:

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }

This ServiceContract is the same as the ServiceContract on the server side.

Because you are calling WCF in core, you need to add the following two packages:

enter image description here

If you use NetTcpBinding, you need to add the following package:

enter image description here

In addition, there are some limitations when calling WCF in core. You can refer to this link:

https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md

Feel free to let me know if the problem persists.

like image 186
Ding Peng Avatar answered Oct 29 '22 13:10

Ding Peng