简体   繁体   中英

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

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:

在此处输入图像描述

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

在此处输入图像描述

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

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

Feel free to let me know if the problem persists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM