Monday, November 25, 2019

WCF Service in Asp.net MVC


WCF Service:

Windows Communication Foundation (WCF) is not a language, it’s not a technology and it is a framework for building service-oriented applications. So, if you want any service-oriented application you go with wcf services.

Here also you can send data and receive data from one point to another point. Are created multiple end point, and you host wcf service in multiple places like IIS, local, console application, windows services. 

The Main Advantage of using wcf supports more protocols like HTTP, TCP (Transmissions Control Protocol).

Wcf combative web application that instead of only one HTTP protocol for example if the customer is required the same service for web application as well as windows applications, for web application we can use HTTP protocol and for windows application we can use TCP protocol so, then we go for wcf service.

We will get the data XML format  and SOAP body (Simple object access Protocol) in wcf service also. 

 SOAP is a method of transferring messages, or small amounts of information, over the Internet. SOAP messages are formatted in XML and are typically sent using HTTP (hypertext transfer protocol).

The disadvantage of some more complicated in configure wcf service apart from that everything is good when we deal with wcf service.

We create wcf project will have 2 files,

1.       create interface

2.       create a class

what are the interface are there that called as a contract. that particular contract class need to implemented.

  • That means when you implement the wcf service the contract going to register between client and server. 
  • If you said that particular contract this method is there, if you have to be include that method. In case that method not included it’s get error. This is a stick policy of contract.
  • In here each and every web service weather accessing in Http weather accessing in Tcp they should be provide address. If don’t provide address the default which application are you working that address taken for us. 
  • To communication with your web services either we communicated by using Ajax call either by using class object it can create and access the services    
  • Here wcf service we can create services two ways 
1.       Restful service

2.       Service-oriented service
  • Wcf services also use for creating restful services
  • Restful service means each and every is versa access by using URI. So, that type of restful service we can even creating by using wcf.
  • Even test wcf service also there is lot of ways Svcutill.exe, postman, fiddler. 
  • Wcf service we have been using in windows application. Consuming encoded binary format (like 1 and 0’s)
  • Tcp protocol only converted encoded binary format.

Create WCF Service:

Right click on Solution Explore ->Add -> New Project -> ASP.NET Web Application (.NET Freamework) using C# -> Next -> Project Name (WcfApplication) -> Create -> select Empty and select MVC folder -> Create button like below





Right Click on Project -> Add -> New Item -> WCF Service -> Add 



Project added like below


Open IMyService1.cs file copy the below code in interface method

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;



namespace WcfApplication

{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMyService1" in both code and config file together.

    [ServiceContract]

    public interface IMyService1

    {

        [OperationContract]

        void DoWork();

        [OpertaionContract]

        int Add(int a, int b);

    }

}



Open MyService1.svc.cs file copy the below code



using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;



namespace WcfApplication

{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MyService1" in code, svc and config file together.

    // NOTE: In order to launch WCF Test Client for testing this service, please select MyService1.svc or MyService1.svc.cs at the Solution Explorer and start debugging.

    public class MyService1 : IMyService1

    {

        public void DoWork()

        {

        }

        public int Add(int a, int b)

        {

            return a + b;

        }

    }

}

Run the Application (Right Click on MyService1.svc file -> click view in Browser option)



Test the wcf service:



Go to Run window type WcfTestClient.exe incase it not open go through the below paths copy that path in run Command prompt

VS 2019 Community:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\WcfTestClient.exe

VS 2017 Community:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\WcfTestClient.exe

The window open like below


In above window click on file -> Add service then window will be open then give path like below 



When u click ok button, it will show like below



Click the Add method in lift side.


In the Request box, select the Value field and type numbers

.

Click the Invoke button. If a Security Warning dialog box appears, click OK. The result displays in the Response box.



Bindings in wcf services:

Basic binding:

This binding is provided by the BasicHttpBinding class. It is designed to expose a WCF service as an ASMX web service, so that old clients (that are still using an ASMX web service) can consume the new service. By default, it uses the HTTP protocol for transport and encodes the message in UTF-8 text format. You can also use HTTPS with this binding.

Web binding:



This binding is provided by the WebHttpBinding class. It is designed to expose WCF services as HTTP requests using HTTP-GET and HTTP-POST. It is used with REST based services that may provide output in XML or JSON format. This is very much used with social networks for implementing a syndication feed.


TCP binding:

This binding is provided by the NetTcpBinding class. It uses TCP protocol for communication between two machines within intranet (means same network). It encodes the message in binary format. This is a faster and more reliable binding compared to the HTTP protocol bindings. It is only used when the communication is WCF-to-WCF which means both client and service should have WCF.





No comments:

Post a Comment