top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is one-way operation in WCF?

+1 vote
266 views
What is one-way operation in WCF?
posted Nov 2, 2016 by Jdk

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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 does not bother about success or failure of service. Only for the client does calling the service matter. 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 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 ! ");
        }

    }
}
answer Nov 10, 2016 by Manikandan J
...