Full Trust European Hosting

BLOG about Full Trust Hosting and Its Technology - Dedicated to European Windows Hosting Customer

European WCF Hosting - HostForLIFE.eu :: Why Should We Use WCF?

clock August 28, 2020 07:58 by author Peter

What is WCF?
WCF stands for ‘Windows Communication Foundation’. It is a Microsoft platform for building distributed and interoperable applications.

What is a distributed application?
A distributed application is an application where part of it runs on two or more computer nodes. Distributed applications are called ‘Connected systems’ also.

Now, let us see the above diagram where a Windows application is stored on one computer and the web service that it is consuming is running on another computer (which can be situated anywhere in the world). Well, this is a connected system.

Why build distributed applications?
    Interdependency
    An enterprise application may need to use the service provide by other enterprise, For example, an ecommerce application using Paytm for payments.

    For better scalability
    Scalability of an application implies the number of visitors an application can handle without degrading the performance. We can break down the application into different layers that run on different computers. Each of these computers will have its own memory and processor, which helps with improving the scalability of the application.

What is an interoperable application?
An application that can communicate with any other application that is built on any platform is called an interoperable application. Web Services can communicate with any application built on any platform, whereas .NET remoting service can be consumed only by another .NET application.

Why should we use WCF?

Without WCF,

Consider a situation where we have 2 clients and we need to implement a service for them.

The first client is using a Java application to interact with our services. This client wants a message to be in XML format and the protocol to be in HTTP. Without WCF, to satisfy the first client requirement, we will have to create an ASMX web service.


The second Client is using .NET and for better performance, this client wants a message format in binary format and protocol to be in TCP. Without WCF, to satisfy the second client requirement, we will create a .NET Remoting service.

 

.NET remoting and ASMX are two different technologies, and have completely different programming models. Therefore, developers have to learn two different technologies, which is not only time and cost consuming but needs two people with expertise.

So to unite and bring all these communication technologies under one roof, Microsoft has come up with a single programming model that is called WCF. WCF is going to unify everything, such as .NET Remoting, IPC, MSMQ queue, TCP, Peer networking, and all other communication technologies we have.

With WCF
With WCF, for both the clients, we will implement only one service and to satisfy the requirement of different clients, we will configure different end points.

What Is WCF? Why Should We Use WCF?
Here, the first endpoint will transport a message in XML format over HTTP protocol and the second endpoint will transport the message in binary format over TCP protocol. Here, we don’t need to change the service code to configure these endpoints. Now, if we have a third client who needs a binary message over HTTP protocol, to satisfy that client requirement, all we need to do is to create a new endpoint.

So, we have a single service but then to satisfy the requirements of different clients, we are creating/configuring different endpoints.



European WCF Hosting - HostForLIFE.eu :: Message Exchange Patterns In WCF

clock February 23, 2017 08:24 by author Peter

A message exchange pattern means how the client and WCF service exchange the message (data) internally. There are a total three types of patterns,

  • Request – Reply
  • One Way
  • Duplex

We will learn each of them in detail below.

Request – Reply
In request reply pattern client sends the request to WCF service and waits for the response from the WCF service. Until they get a reply no other process runs. After getting a response from the WCF service they will continue its work. If WCF service has a method with void return type then the client will wait for the reply.

So in this pattern if any error occurs then it will directly display on the client side. Default pattern for any methods is request – reply. You can declare any method to request a reply by assigning ‘IsOneWay=false’ as show below.
    [ServiceContract] 
            public interface IMessageExchPatterns 
            { 
                [OperationContract] 
                string GetDataRequestReply (string name); 
    }

OR
    [ServiceContract] 
            public interface IMessageExchPatterns 
            { 
                [OperationContract(IsOneWay=false)] 
                string GetDataRequestReply (string name); 
    } 

As shown in the above example you can declare request reply pattern in two ways.

One Way
In One way pattern client sends the data or request to WCF service but it doesn't wait for any reply from the service and also service doesn't send any reply to them, so here the client will not show any thing on service then will only send the message to service. It doesn't check if that message reaches the service or not. You can set this pattern by IsOneway=true.

Example
    [ServiceContract] 
            public interface IMessageExchPatterns 
            { 
                [OperationContract(IsOneWay=true)] 
                void GetDataOneWay (string name) 
    } 

Duplex
In Duplex pattern you have to predict that there are  two clients (client-1, Client-2); client-1 makes request to service and then service will sent request to client-2 and then service gets response from client-2 and give this replay to client-1.

Here this pattern is to use the callback method for the internal (client to client ) communication. So here we have to define the callback interface as show below,
    [ServiceContract(CallbackContract=typeof(IMessageExchPatternsCallback))] 
            public interface IMessageExchPatterns 
            { 
                [OperationContract(IsOneWay = false)] 
                string GetDataRequestReply(string name); 
     
                [OperationContract(IsOneWay=true)] 
                void GetDataOneWay(string name); 
     
            [OperationContract] 
                void GetDataDuplex(string name); 
            } 
     
            public interface IMessageExchPatternsCallback 
            { 
                [OperationContract] 
                void SendData(string name); 
    } 

As shown in above example we specify the,
    [ServiceContract(CallbackContract=typeof(IMessageExchPatternsCallback))] 
Here we specify the call back contract for the interface. This callback interface (IMessageExchPatternsCallback) is implementing on client side.
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)] 
    public class MessageExchPatterns : IMessageExchPatterns 
    { 
             
            public string GetDataRequestReply(string name) 
            { 
                return "You have enter : " + name; 
            } 
     
            public void GetDataOneWay(string name) 
            { 
                Console.Write("You have enter : " + name); 
            } 
     
            public void GetDataDuplex(string name) 
            { 
                OperationContext.Current.GetCallbackChannel<IMessageExchPatternsCallback>().SendData("Hi, " + name); 
            } 
    } 


Now host this service on console application
ServiceHost host = new ServiceHost(typeof(MessageExchPatterns.MessageExchPatterns)); 
host.Open(); 
Console.Write("Service started..."); 
Console.Read(); 


Now create client application with Window form application as below.

Write code as shown below,
[CallbackBehavior(UseSynchronizationContext = false)] 
public partial class Form1 : Form,ServiceReference1.IMessageExchPatternsCallback 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        InstanceContext instanceContext; 
        private void button1_Click(object sender, EventArgs e) 
        { 
            
            ServiceReference1.MessageExchPatternsClient clnt = new ServiceReference1.MessageExchPatternsClient(instanceContext); 
            label1.Text = clnt.GetDataRequestReply(textBox1.Text); 
        } 
 
        void ServiceReference1.IMessageExchPatternsCallback.SendData(string name) 
        { 
            Label3.Text = name; 
        } 
 
        private void button2_Click(object sender, EventArgs e) 
        { 
             
MessageExchPatternsClient clnt = new MessageExchPatternsClient(instanceContext); 
            clnt.GetDataOneWay(textBox2.Text); 
        } 
 
        private void button3_Click(object sender, EventArgs e) 
        { 
ServiceReference1.MessageExchPatternsClient clnt = new ServiceReference1.MessageExchPatternsClient(instanceContext); 
            clnt.GetDataDuplex(textBox3.Text); 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            instanceContext  = new InstanceContext(this); 
        } 
    } 


Now run the WCF service host. Then run client application.



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in