Thursday, 24 December 2015

Logger file Name

public static void LogInfo(string Message)
        {
            try
            {
                bool EnableLog = true;
                if (EnableLog)
                {
                    string LogFile = "D:\\Logs\\revenu_" +DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") +DateTime.Now.Year;
                    StreamWriter sw;
                    if (!string.IsNullOrEmpty(LogFile))
                        LogFile.Replace("revenu__Logs","revenu_" + DateTime.Today.Date.ToString().Substring(0, 10));
                    if (!File.Exists(LogFile))
                    {
                        sw = File.CreateText(LogFile);
                        sw.WriteLine(string.Format("Log File Created: {0}",DateTime.Now.ToString()));
                        sw.Close();
                    }
                    sw = File.AppendText(LogFile);
                    sw.WriteLine(string.Format("{0}: {1}",DateTime.Now.ToString(), Message));
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Thursday, 17 December 2015

WCFRestful Service cusuming using javascript

Jquery ajax functions to calling RESTFUL Service in WCF
====================================================================
Client application:
=============
<script src="Scripts/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(Document).ready(function () {
            $("#Button1").click(function () {
               debugger;

                //var input =
                //{
                 
                //    "strArgument1": "DUMMY55",
                //    "strArgument2": "TestDemo5555"
               
                //};

                var dt = {
                    "User": {
                        "Number": "5555",
                        "Name": "Raju",
                        "Id": "Dummy",
                        "Mail": "rajneto"
                    },
                    "Params": {
                        "Code": "Value",
                        "MaxCount": "1000",
                        "MaxCount": "Yeah",
                        "UserContext": "Yes"
                    },
                    "Items": [{
                        "Searchitems": "ten",
                        "Searchlist": "RAVVVVV"
                    }]
                 
                };
                $.ajax({
                    type: "POST",
                    url: "http://localhost:64650/Service.svc/EMPloyee",
                    data: JSON.stringify(dt),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        var value = JSON.stringify(response);
                        alert(value);
                    },
                    failure: function (response) {
                        alert(response.d);
                    }
                });


            }
                );
        });

    </script>
===================================================================
WCF Service  for I service class;
=============
public interface IService1
    {
        [OperationContract]
        [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate = "EMPloyee", RequestFormat = WebMessageFormat.Json, Method = "POST")]
       string SumFields(RootObject Rootobj);

    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    // You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WcfServiceLibrary3.ContractType".
   [DataContract]
    public class User
    {
        [DataMember]
        public string Number { get; set; }
         [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Id { get; set; }
       [DataMember]
        public string Mail { get; set; }
    }
    [DataContract]
    public class Params
    {
        [DataMember]
        public string Code { get; set; }
        [DataMember]
        public string MaxCount { get; set; }
        [DataMember]
        public string MaxCount { get; set; }
        [DataMember]
        public string UserContext { get; set; }
    }
    [DataContract]
    public class Item
    {
        [DataMember]
        public string Search { get; set; }
        [DataMember]
        public string SearchCode { get; set; }
    }
    [DataContract]
    public class RootObject
    {
        [DataMember]
        public User User { get; set; }
        [DataMember]
        public Params Params { get; set; }
        [DataMember]
        public List<Item> Items { get; set; }
    }
======================================================

Service.cs file source:
----------------------------

namespace WcfServiceLibrary3
{
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class Service1 : IService1
    {


       public string SumFields(RootObject RootObject)
        {
            User urs = RootObject.User;
            Params pm = RootObject.Params;
            List<Item> lt =new List<Item>();
            lt = RootObject.Items;
            string data = "" + urs.EmployeeId + "," + urs.EmployeeMail + "," + urs.EmployeeName + "" + urs.EmployeeNumber + "," + pm.CheckMaxCount + "," + pm.CheckUserContext + "," + pm.OpCode + ","+pm.MaxCount+",,,";
            for (int i = 0; i < lt.Count; i++)
            {
                data += lt[i].SearchCode.ToString();
            }

            return data;    
       }
    }
}
================================================
Service.SVC file source code:
------------------------------------
<%@ ServiceHost Service="WcfServiceLibrary3.Service1"  Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>


Tuesday, 15 December 2015

Handler

 function test() {
            alert("ss");
            var pID = $("#t").val();
            var tID = $("#t1").val();


            var urlToHandler = "Handler.ashx";

            $.ajax({
                type: "POST",
                url: urlToHandler,
                data: { a: pID, b: tID },
                dataType: "json",
                success: function (msg) {
                     alert(msg);
                    $("#t3").val(msg);
                }
            });
            alert("sss");
        }
 <input id="Button1" type="button" value="button" onclick="test();" />
  handler code for design:
using System;
using System.Web;

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
         context.Response.ContentType = "text/plain";
         string a = context.Request.Params["a"].ToString();
         string b = context.Request.Params["b"].ToString();
        context.Response.Write(add(a,b));
    }

    public string add(string a, string b)
    {
        int c = Convert.ToInt32(a) + Convert.ToInt32(b);
        return c.ToString();
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}