One-Way Operations in WCF Services

When an operation has no return value, and the client does not care about the success or failure of the invocation. WCF offers one-way operations to support this sort of fire-and-forget invocation,: once the client issues the call, WCF generates a request message, but no correlated reply message will ever return to the client. The one-way message exchange pattern is useful when a client needs to send information to a service but doesn't receive a response. In reality, it's "fire and acknowledge" because the caller receives an acknowledgement that the message was successfully committed to the communication channel.

when a client sends a message to a service endpoint whose operation is marked as one-way, control is returned to the caller before the service operation completes. One-way operations are specified on the [OperationContract] attribute by using the IsOneWay=true modifier.

Configuring One-Way Operations :

The OperationContract has a property IsOneWay which is Boolean type.

[AttributeUsage(AttributeTargets.Method)]
public sealed class OperationContractAttribute : Attribute
{
   public bool IsOneWay
   { get; set; }
}

Default value of IsOneWay property is false. You need to set IsOneWay to true to configures the method as a one-way operation:

[ServiceContract]
public interface IArticleService
{
   [OperationContract(IsOneWay=true)]
   void OneWayMethod();
}


One way Operation:

namespace WCFService
{  
    [ServiceContract]
    public interface IArticleService
    {
        [OperationContract(IsOneWay=true)]
        void OneWayMethod();

        [OperationContract]
        DataTable GetAllArticles();
    }
}

namespace WCFService
{  
    public class ArticleService : IArticleService
    {

         public void  OneWayMethod()
         {
            // Some code here
            /////////////
         }

        public DataTable GetAllArticles()
        {
            using (var Context = new ArticleDBEntities())
            {
                DataTable objDt = new DataTable();
                objDt = Context.Articles.ToList().CopyToDataTable();
                return objDt;
            }
        }
    }
}


Both service operations in service contract are implemented same but one is marked as one-way operation. When any client calls OneWayMethod the client-side proxy call returns immediately and doesn't wait for the response. When the client calls GetAllArticles the client-side proxy call blocks while the service executes the statement.

Client implemented code :

ArticleServiceClient proxy = new ArticleServiceClient();

proxy.OneWayMethod();

DataTable objDt = new DataTable();
objDt = proxy.GetAllArticles();
proxy.Close();


You should turn on reliability for your services, even for one-way calls. This will ensure delivery of the requests to the service

Sessionful Services with One-Way Operations :

You can design a sessionful contract with one-way operations:

[ServiceContract(SessionMode = SessionMode.Required)]
interface IArticleService
{
   [OperationContract(IsOneWay = true)]
   void OneWayMethod();
}


If the client issues a one-way call and then closes the proxy while the method executes, the client will still be blocked until the operation completes.
 

Up Next
    Ebook Download
    View all
    Learn
    View all