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

12,228

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.

Share:
12,228
user3237193
Author by

user3237193

Updated on June 09, 2022

Comments

  • user3237193
    user3237193 almost 2 years

    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