banner



How To Call Wcf Service In C#

WCF - Consuming WCF Service


WCF services allow other applications to access or swallow them. A WCF service can be consumed past many ways depending on the hosting type. Here, we are explaining the stride-by-footstep method to consume a WCF service for each of the following popular hosting options −

  • Consuming WCF Service hosted in IIS 5/6
  • Consuming WCF Service that is self-hosted
  • Consuming WCF Service hosted in Windows Activation Service
  • Consuming WCF Service hosted in Windows Service

Consuming WCF Service Hosted in IIS 5/6

The process of consumption of a WCF service hosted in IIS 5/6 is discussed below in detail. In improver, the discussion includes how to create proxy and console applications.

Footstep i − Once a service is hosted in IIS, nosotros take to consume information technology in client applications. Before creating the customer application, we need to create a proxy for the service. This proxy is used by the client application to interact with the service. To create a proxy, run Visual Studio 2008 control prompt. Using service utility, we can create the proxy course and its configuration information.

svcutilhttp://localhost/IISHostedService/Service.svc

Wcf Consuming Services IIS 1

After executing this command, nosotros volition get 2 files generated in the default location.

  • MyService.cs − Proxy course for the WCF service

  • output.config − Configuration data nigh the service

Step 2 − Now, nosotros will outset creating the Panel application using Visual Studio 2008 (Client application).

Wcf Consuming Services IIS 2

Pace three − Add the reference 'Arrangement.ServiceModel'; this is the core dll for WCF.

Pace 4 − Create a Proxy class.

using System; using System.Collections.Generic; using Organization.Linq; using System.Text;  namespace MyServiceClient {    Class Program {       Static void Main(string[] args) {          // Creating Proxy for the MyService          ServiceClient Client = newServiceClient();          Panel.WriteLine("Customer calling the service...");          Console.WriteLine("Hello Ram");          Console.Read();       }    } }        

The output appears as follows −

Wcf Consuming Services IIS 4

Consuming Self-hosted WCF Service

Here, the entire process of consuming a self-hosted WCF Service is explained step-by-stride along with ample coding and screenshots wherever necessary.

Step 1 − Service is hosted, at present nosotros demand to implement the proxy course for the client. In that location are unlike ways of creating the proxy.

  • Using SvcUtil.exe, we can create the proxy class and its configuration file with finish-points.

  • Adding Service reference to the client application.

  • Implementing ClientBase<T> grade

Of these three methods, Implementing ClientBase<T> is the best practice. If yous are using the other ii methods, nosotros demand to create a proxy form every fourth dimension we make whatever changes in the Service implementation. But this is not the case for ClientBase<T>. It will create the proxy only at runtime and then information technology will have intendance of everything.

For this purpose, create one proxy grade, which includes the references of Organisation.ServiceModel and MyCalculatorService.

using System; using System.Collections.Generic; using Organisation.Linq; using Organisation.Text; using System.ServiceModel; using MyCalculatorService;  namespace MyCalculatorServiceProxy {    // WCF create proxy for ISimpleCalculator using ClientBase    Public class MyCalculatorServiceProxy :     ClientBase<ISimpleCalculator>,        ISimpleCalculator {       Public int Add together(int num1, int num2) {          //Call base to do funtion          returnbase.Aqueduct.Add together(num1, num2);       }    } }        

Now, create one console application, which includes the references of Arrangement.ServiceModel and MyCalculatorServiceProxy.

using Organization; using System.Collections.Generic; using Organization.Linq; using System.Text; using Arrangement.ServiceModel; using MyCalculatorServiceProxy;  namespace MyCalculatorServiceClient {    classProgram {       Static void Main(string[] args) {          MyCalculatorServiceProxy.MyCalculatorServiceProxy proxy = newMyCalculatorServiceProxy.MyCalculatorServiceProxy();                    Console.WriteLine("Customer is running at " + DateTime.Now.ToString());          Console.WriteLine("Sum of two numbers. 5 + 5 =" + proxy.Add together(v,5));          Console.ReadLine();       }    } }        

Pace 2 − Cease-betoken (same as service) information should be added to the configuration file of the customer awarding.

<?xmlversion = "1.0"encoding = "utf-8" ?> <configuration>    <arrangement.serviceModel>       <client>          <endpoint address              ="http://localhost:8090/MyCalculatorServiceProxy/ISimpleCalculator"             binding = "wsHttpBinding" contract "MyCalculatorServiceProxy.ISimpleCalculator">             </endpoint>       </client>    </system.serviceModel> </configuration>        

Step 3 − Before running the customer application, you lot need to run the service. Shown below is the output of the client application.

Wcf Consuming Services Self 3

Consuming WCF Service Hosted in WAS

Consuming a WCF service that is hosted in WAS is a simple process involving simply a few steps. The steps are as follows −

  • Add the proxy class and the configuration file to the client application.
  • Create the object for the MathServiceClient and phone call the method.
using System; using System.Collections.Generic; using System.Linq; using System.Text;  namespaceWASHostedClient {    classProgram {       staticvoid Main(string[] args) {          MathServiceClient client = newMathServiceClient();          Console.WriteLine("Sum of two number 5,6");          Console.WriteLine(client.Add(5, 6));          Console.ReadLine();       }    } }        

The output appears as shown below.

Wcf Consuming Services WAS 2

Consuming WCF Service Hosted in Windows Service

The step-past-step procedure of how to consume a WCF service hosted in Windows Service is expressed below in detail with coding and instructions.

In one case information technology is hosted successfully, nosotros can create a proxy class for the service and start using in the customer application. Here, it is shown with the IIS hosting type consuming.

Wcf Consuming Services Windows Service 1

Add the reference of ServiceModel.

using Organisation; using Arrangement.Collections.Generic; using System.Linq; using System.Text;  namespaceWindowServiceClient {    classProgram {       staticvoid Principal(string[] args) {          //Creating Proxy for the MyService          MyServiceClient client = newMyServiceClient();          Console.WriteLine("Client calling the service...");          Panel.WriteLine("Sum of two numbers 5,6");          Console.WriteLine(client.Add(5, vi));                   Console.WriteLine("Subtraction of two numbers 6,five");          Console.WriteLine(client.Sub(6, v));                   Console.WriteLine("Multiplication of two numbers 6,five");          Panel.WriteLine(client.Mul(half dozen, v));                   Console.WriteLine("Division of two numbers 6,three");          Console.WriteLine(client.Div(six, 3));          Console.Read();       }    } }        

The output appears every bit follows −

Wcf Consuming Services Windows Service 3

Useful Video Courses


WCF Online Training

Video

WCF Demystified: A Primary Course on WCF Services &amp; Web API

Video

How To Call Wcf Service In C#,

Source: https://www.tutorialspoint.com/wcf/wcf_consuming_service.htm

Posted by: dickwaye1987.blogspot.com

0 Response to "How To Call Wcf Service In C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel