10

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

user3237193
  • 137
  • 1
  • 1
  • 7
  • Does this answer your question? [How to consume wcf in .net core without adding service reference?](https://stackoverflow.com/questions/42018402/how-to-consume-wcf-in-net-core-without-adding-service-reference) – devNull Aug 19 '20 at 16:19
  • @devNull I have referred the above link, but it looks the code is mainly developed for web service and some of the properties/methods in Core .Net 3.1 getting reference errors. In my case, I will get WSDL URL,Method name as Input parameters and now I need to validate the same WSDL URL and consume the same method. – user3237193 Aug 19 '20 at 19:49

1 Answers1

8

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.

Ding Peng
  • 3,702
  • 1
  • 5
  • 8
  • I don't quite understand what you mean. There are two ways to call WCF services. One is to call it by adding a service reference to generate a proxy class, and the other is to use the channel factory I described above to call it. – Ding Peng Aug 20 '20 at 09:50
  • The above method does not need to use the resource manager, you only need to add code to call the WCF service. – Ding Peng Aug 20 '20 at 09:54
  • The option you have suggested is still need to update the client code whenever WSDL URL changes and deployment is required. But in my case I will get be the following input from the webpage: 1) WSDL url (WSDL URL is dynamic) 2) Method Name to consume from WSDL url mentioed in the Step.1 3) Input values for the Method So that when WSDL URL changes, no need to rebuild/change the client code and deploy the project/solution. – user3237193 Aug 20 '20 at 09:54
  • The WsdlImporter class exists in the .net framework to meet your needs, but WsdlImporter does not exist in the core. – Ding Peng Aug 21 '20 at 02:25