Sunday, 12 January 2014

Create a One-Way asp.net web service

In this article we are going to see the how to create a one way web service in the asp.net. so for long running process we can invoke the service and do the next process instead of waiting for the response from the service. set [SoapDocumentMethod(OneWay=true)]

web service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.Remoting.Messaging;
using System.Web.Services.Protocols;
using System.IO;
using System.Threading;

namespace MathService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class MathService : System.Web.Services.WebService
    {

        [WebMethod]
        public int Add(params int[] values)
        {
            return values.Sum();
        }

        
        [SoapDocumentMethod(OneWay=true)]
        [WebMethod]
        public void WriteLog(string message)
        {
            Thread.Sleep(10000);
            File.AppendAllText(Server.MapPath("log.txt"),message);
        }
    }

}

Client:
namespace InvokeWebService
{
    class Program
    {
        static void Main(string[] args)
        {
            Maths.MathService websrv = new Maths.MathService();
            Console.WriteLine("Connecting service . . . ");           
            int addvalue = websrv.Add(new int[] { 3,2,6,7});
            Console.WriteLine(" Add Operation result : " + addvalue);

            int subvalue = websrv.Sub(4, 2);
            Console.WriteLine(" Sub Operation result :" + subvalue);

            websrv.WriteLog("This is called as async, by Rajesh. G");
            Console.WriteLine("Done");
            Console.Read();

        }
    }
}




Output:
This is called as async, by Rajesh. G

A log file is created in the server side with the information send by the user.

No comments:

Post a Comment