.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

One Way Service in WCF

Posted By:Dhananjay Kumar       Posted Date: February 24, 2010    Points: 25    Category: WCF    URL: http://www.dotnetspark.com  

One Way Service in WCF. This article will discuss about One Way Operations in WCF. I am also going to explain One Way Operation with Session full service pros and cons.
 

Objective:

This article will discuss about One Way Operations in WCF. I am also going to explain One Way Operation with Session full service pros and cons.

Why we need One Way Service?

There might be requirements where Service's Operation is not going to return any value. And at the same time client also not bother about success or failure of service. Only for the client do matters calling the service. Client will call the operation and forget and continue with the operations.
One Way Operations

This is the way in WCF to achieve Fire and forget invocation of the service. Once the Client issues the call , WCF generates the request message and there is no corresponding response message for that. No Service side Exception will make their way to client.

Configuring One Way Operation

Boolean ISOneWay property of OperationContract class is used to configure service as one way service. We need to set this property to true. Setting true to this property turns a service operation as one way operation.

using System;
using System.Net.Security; 
namespace System.ServiceModel
{

  [AttributeUsage(AttributeTargets.Method)] 
  public sealed class OperationContractAttribute : Attribute
  {

  public OperationContractAttribute(); 
  public string Action { get; set; }
  public bool AsyncPattern { get; set; }
  public bool HasProtectionLevel { get; }
  public bool IsInitiating { get; set; } 
  public bool IsOneWay { get; set; }

  public bool IsTerminating { get; set; }
  public string Name { get; set; }
  public ProtectionLevel ProtectionLevel { get; set; }
  public string ReplyAction { get; set; }
  }
}


The client has nothing to do anything specific to invoke one way operation.

One Way operation and Session full Services

Theoretically it is possible to design a one way service as session full service but it is considered as a bad design. One way Service should be applied either on Per Call or single service.
Even if; if we want to make a one way operation as session full service then we should design our contact such that only the last operation is terminating the session. One more thing to make sure the last operation is adhering the one way operation rules that is returning void.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace OneWayService
{
  [ServiceContract]
  public interface IService1
  { 
  [OperationContract]
  string GetData(int value);
  [OperationContract]
  int AddTwoNumber(); 
  [OperationContract(IsOneWay=true ,IsTerminating =true ,IsInitiating = false)
  void DoSomeThing(); 
  }
}


One Way Operation and Exception


When there is no transport session; means when BasicHttpBinding or wsHttpBinding is used; if an Exception takes place during the invocation of one-way operation, the client is unaffected and can continue to issue call on the same proxy instance. But in presence of transport session if exception is occurring in invocation of one-way operation will fault the channel and client will not able to make call to same faulted proxy.

One-way property could only be set true to the operations that are returning Void. On any other return type it would throw error while loading the service

Example

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; 
namespace OneWayService
{

  [ServiceContract]
  public interface IService1
  { 
  [OperationContract(IsOneWay=true)]
  string  DoSomeThing(); 
  }
}


Service1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; 
namespace OneWayService
{

  public class Service1 : IService1
  {
  public string DoSomeThing()
  {
  return string.Format(" I am going to throw error opps ! ");
  }

  }
}

On loading the service the following error will encounter



Conclusion


I have explained various factors assoicated with one-way operations in WCF. Thanks for reading.
Happy Coding
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend