Full Trust European Hosting

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

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.



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

clock April 9, 2015 08:57 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.



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 2012 Hosting, ASP.NET 4.5 Hosting, ASP.NET MVC 4 Hosting, and SQL 2014 Hosting.

Free ASP.NET Hosting - Europe

Tag cloud

Sign in