One Way Operation in WCF

No.of Views1158
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : WCF , General
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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Runtime.Serialization;
  5 using System.ServiceModel;
  6 using System.Text;
  7 
  8 namespace OneWayService
  9 {
 10public class Service1 : IService1
 11     {
 12public string DoSomeThing()
 13         {
 14return string.Format(" I am going to throw error opps ! ");
 15         }
 16     }
 17 }

On loading the service the following error will encounter

Image Loading

Conclusion

I have explained various factors associated with one-way operations in WCF. Thanks for reading.Happy Coding

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top