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.



WCF Hosting UK - HostForLIFE.eu :: Binding and Behavior in WCF

clock June 28, 2019 11:26 by author Peter

In this article I will clarify about the Binding and Behavior of WCF. It is one of the principal of WCF. Binding represents how the client can communicate with the service.

Assume we need to make the service for two clients , first client will access SOAP using HTTP and second client access Binary using TCP. Utilizing webservice it is impossible yet utilizing WCF we can do by including additional endpoint in the configuration file. Example: in web.config.
<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true"targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MathService"
        behaviorConfiguration="MathServiceBehavior">
        <endpoint
          address="http://localhost:9080/Service1.svc"contract="IMathService"
           binding="wsHttpBinding"/>
<endpoint address="net.tcp://localhost:8080/MyService/MathService.svc"
       contract="IMathService"
                  binding="netTcpBinding"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
     </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

And now, I’ll tell you about the Behaviour at the service level. In the service behavior, I have mention the servieMetadata node with attribute httGetEnabled='true'. This attribute will specifies the publication of the service metadata. Similarly we can add more behavior to the service. <system.serviceModel>
  <services>
    <service name="MathService"
      behaviorConfiguration="MathServiceBehavior">
      <endpoint address="" contract="IMathService"
        binding="wsHttpBinding"/>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="MathServiceBehavior">
        <serviceMetadata httpGetEnabled="True"/>
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

HostForLIFE.eu European WCF Hosting

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 customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



WCF Hosting UK - HostForLIFE.eu :: Optional Parameters In WCF Service URI Template

clock December 21, 2018 10:09 by author Peter
WebGet(UriTemplate = "TestMessage/{p1}?firstname={firstname}&lastname={lastname}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Xml)]  
[OperationContract]  
string TestMessage(string message, string firstname = "", string lastname = ""); 

Whereas TestMessage is my service name and p1 parameter is compulsory whereas firstname and lastname parameters are optional but u have to specify in the URI Template but keep one thing in mind while declaring the parameters as optional u have to first preceed them with ? and then parameter name and then use & to assign the second parameter as option as per your requirement. If u see in the function declaration also i have used the optional parameter feature of .Net 4.0

Step 2 - Function Definition

public string TestMessage(string message,string firstname="",string lastname="")  
{  
   string responsedata = string.Empty;   
   responsedata = message + "" + "FirstName: " + firstname + "" + "LastName: " + lastname;  
   return responsedata;  
}  

Note
How to call the Service,

Input1
http://localhost:49785/Service.svc/parameter1

Output1 - parameter1 Firstname: LastName

In the above output, only parameter1 will be returned as u didnot specify the other 2 parameters so they will get their value as "Empty"

Input2

http://localhost:49785/Service.svc/parameter1?firstname=peter&lastname=scott

Output2 - parameter1 Firstname:kamal LastName:rawat

so u will see in the above output u passed both the parameters so you got all the values.

Input2

http://localhost:49785/Service.svc/parameter1?firstname=peter

Output2 - parameter1 Firstname:kamal LastName:
In the above output parameter1 is passed i.e firstname=peter but you wont get lastname parameter, because you didn't pass and its optional if u dont pass the parameter value and will take value as empty.

HostForLIFE.eu European WCF Hosting

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 customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



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

HostForLIFE 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 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Tag cloud

Sign in