Wednesday, 24 February 2016

how to call a web api in console app

web api code

using Nop.Admin.Models.PurchaseOrder;
using Nop.Core.Domain.PurchaseOrder;
using Nop.Core.Domain.Vendors;
using Nop.Services.Catalog;
using Nop.Services.PurchaseOrder;
using Nop.Services.Vendors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Nop.Admin.Extensions;
using Nop.Services.SalesOrder;
using System.Web.Mvc;
using Nop.Core.Configuration;
using Nop.Services.Localization;
using Nop.Core;
using Nop.Services.Security;
using Nop.Services.Account;
using Nop.Services.Helpers;
using Nop.Services.Customers;
using Nop.Core.Infrastructure;
using Nop.Services.Common;
using Nop.Services.Invoice;
using Nop.Core.Domain.Invoice;
using System.Data.Entity;
using Nop.Core.Domain.OutgoingCalls;
using Nop.Services.OutgoingCalls;
using Nop.Services.SchedulePlanner;

namespace Nop.Admin.Controllers
{
    public class ForecastController : ApiController
    {
     
        private readonly IOutgoingCallConfigurationService _OutgoingCallConfigurationService;
        private readonly IDeliveryScheduleService _DeliveryScheduleService;
        private readonly IMonthlyDeliveryScheduleService _MonthlyDeliveryScheduleService;
        public ForecastController()
            : this(
            EngineContext.Current.Resolve<IOutgoingCallConfigurationService>(),
            EngineContext.Current.Resolve<IDeliveryScheduleService>(),
            EngineContext.Current.Resolve<IMonthlyDeliveryScheduleService>()
            )
        {
        }
        public ForecastController
            (
         
            IOutgoingCallConfigurationService OutgoingCallConfigurationService,
            IDeliveryScheduleService DeliveryScheduleService,
            IMonthlyDeliveryScheduleService MonthlyDeliveryScheduleService
            )
        {
           
            this._OutgoingCallConfigurationService = OutgoingCallConfigurationService;
            this._DeliveryScheduleService = DeliveryScheduleService;
            this._MonthlyDeliveryScheduleService = MonthlyDeliveryScheduleService;
        }


        public HttpResponseMessage DemandForecastPurchaseOrderDailyScheduler()
        {
            AddDatainOutgoingCall();
         
            HttpResponseMessage response = new HttpResponseMessage();
            return response;
        }

 #region Outgoingcallbychinmaya
        private void SaveOutgoingCall(int AccountId, int OutgoingConfigurationId)
        {
            OutgoingSummary outgoingSummary = new OutgoingSummary();
            outgoingSummary.Date = DateTime.Today;
            outgoingSummary.AccountId = AccountId;
            outgoingSummary.OnHold = false;
            outgoingSummary.Time = "00:00:00";
            outgoingSummary.ResultId = 0;
            outgoingSummary.OutgoingConfigurationId = OutgoingConfigurationId;
            outgoingSummary.Isactive = true;
            outgoingSummary.CreatedDate = DateTime.UtcNow;
            _OutgoingCallConfigurationService.PostOutgoingSummary(outgoingSummary);
        }
        private string GetDeliverDay(int Days, string CurrentDay)
        {
            string DeliverDay = string.Empty;
            switch (Days)
            {
                case 1:
                    {
                        if (CurrentDay.Trim() == "Mon".Trim())
                            DeliverDay = "Tue";
                        else if (CurrentDay.Trim() == "Tue".Trim())
                            DeliverDay = "Wed";
                        else if (CurrentDay.Trim() == "Wed".Trim())
                            DeliverDay = "Thu";
                        else if (CurrentDay.Trim() == "Thu".Trim())
                            DeliverDay = "Fri";
                        else if (CurrentDay.Trim() == "Fri".Trim())
                            DeliverDay = "Mon";
                        break;
                    }
                case 2:
                    {
                        if (CurrentDay.Trim() == "Mon".Trim())
                            DeliverDay = "Wed";
                        else if (CurrentDay.Trim() == "Tue".Trim())
                            DeliverDay = "Thu";
                        else if (CurrentDay.Trim() == "Wed".Trim())
                            DeliverDay = "Fri";
                        else if (CurrentDay.Trim() == "Thu".Trim())
                            DeliverDay = "Mon";
                        else if (CurrentDay.Trim() == "Fri".Trim())
                            DeliverDay = "Tue";
                        break;
                    }
                case 3:
                    {
                        if (CurrentDay.Trim() == "Mon".Trim())
                            DeliverDay = "Thu";
                        else if (CurrentDay.Trim() == "Tue".Trim())
                            DeliverDay = "Fri";
                        else if (CurrentDay.Trim() == "Wed".Trim())
                            DeliverDay = "Mon";
                        else if (CurrentDay.Trim() == "Thu".Trim())
                            DeliverDay = "Tue";
                        else if (CurrentDay.Trim() == "Fri".Trim())
                            DeliverDay = "Wed";
                        break;
                    }
                case 4:
                    {
                        if (CurrentDay.Trim() == "Mon".Trim())
                            DeliverDay = "Fri";
                        else if (CurrentDay.Trim() == "Tue".Trim())
                            DeliverDay = "Mon";
                        else if (CurrentDay.Trim() == "Wed".Trim())
                            DeliverDay = "Tue";
                        else if (CurrentDay.Trim() == "Thu".Trim())
                            DeliverDay = "Wed";
                        else if (CurrentDay.Trim() == "Fri".Trim())
                            DeliverDay = "Thu";
                        break;
                    }
                case 5:
                    {
                        if (CurrentDay.Trim() == "Mon".Trim())
                            DeliverDay = "Mon";
                        else if (CurrentDay.Trim() == "Tue".Trim())
                            DeliverDay = "Tue";
                        else if (CurrentDay.Trim() == "Wed".Trim())
                            DeliverDay = "Wed";
                        else if (CurrentDay.Trim() == "Thu".Trim())
                            DeliverDay = "Thu";
                        else if (CurrentDay.Trim() == "Fri".Trim())
                            DeliverDay = "Fri";
                        break;
                    }
                default:
                    DeliverDay = null;
                    break;
            }
            return DeliverDay;
        }
        private int GetweekNumber(DateTime PassDate)
        {
            int WeekNumber = 0;
            if (PassDate.Day <= 7)
            {
                WeekNumber = 1;
            }
            else if (PassDate.Day > 7 && PassDate.Day <= 14)
            {
                WeekNumber = 2;
            }
            else if (PassDate.Day > 14 && PassDate.Day <= 21)
            {
                WeekNumber = 3;
            }
            else if (PassDate.Day > 21 && PassDate.Day <= 28)
            {
                WeekNumber = 4;
            }
            else if (PassDate.Day > 28)
            {
                WeekNumber = 5;
            }
            return WeekNumber;
        }
        private void AddDatainOutgoingCall()
        {
            #region Variable
            string CurrentDay = string.Empty;
            string DeliverDay = string.Empty;
            string WeekNumber = string.Empty;
            DateTime CurrentDate = DateTime.Now;
            int i, j;
            #endregion Variable
            try
            {
                CurrentDay = DateTime.UtcNow.DayOfWeek.ToString().Substring(0, 3);
                if (CurrentDay.Trim() == "Sat" || CurrentDay.Trim() == "Sun")
                    return;
                var lstConfiguration = _OutgoingCallConfigurationService.Gets().OrderByDescending(m => m.Id).Take(1).ToList();
                DeliverDay = GetDeliverDay(lstConfiguration[0].OutGoingCallsPeriod, CurrentDay);
                if (DeliverDay == null)
                    return;
                var lstDeliverySchedule = _DeliveryScheduleService.GetAllDeliverySchedules().Where(m => m.IsLastUpdated == true).ToList();
                for (i = 0; i < lstDeliverySchedule.Count(); i++)
                {
                    if (lstDeliverySchedule[i].DeliveryTypeID == 1)
                    {

                        //save data in outgionsummary table
                        SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                    }
                    if (lstDeliverySchedule[i].DeliveryTypeID == 2)
                    {

                        if (!string.IsNullOrEmpty(lstDeliverySchedule[i].PreferredDays))
                        {
                            string[] s = lstDeliverySchedule[i].PreferredDays.Split(',');

                            for (j = 0; j < s.GetLength(0); j++)
                                if (s[j].Trim() == DeliverDay.Trim())
                                {
                                    //save data in outgionsummary table
                                    SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);
                                    break;
                                }

                        }




                    }
                    if (lstDeliverySchedule[i].DeliveryTypeID == 3)
                    {


                        WeekNumber = "Week " + GetweekNumber(DateTime.UtcNow).ToString();
                        var MonthlyDeliverySchedules = _MonthlyDeliveryScheduleService.GetAllMonthlyDeliverySchedules(0, int.MaxValue, lstDeliverySchedule[i].Id).FirstOrDefault(m => m.WeekNumber == WeekNumber);
                        if (MonthlyDeliverySchedules.Monday)
                            if (DeliverDay.Trim() == "Mon".Trim())
                            {
                                //save data in outgionsummary table
                                SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                            }

                        if (MonthlyDeliverySchedules.Tuesday)
                            if (DeliverDay.Trim() == "Tue".Trim())
                            {
                                //save data in outgionsummary table
                                SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                            }

                        if (MonthlyDeliverySchedules.Wednesday)
                            if (DeliverDay.Trim() == "Wed".Trim())
                            {
                                //save data in outgionsummary table
                                SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                            }

                        if (MonthlyDeliverySchedules.Thursday)
                            if (DeliverDay.Trim() == "Thu".Trim())
                            {
                                //save data in outgionsummary table
                                SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                            }

                        if (MonthlyDeliverySchedules.Friday)
                            if (DeliverDay.Trim() == "Fri".Trim())
                            {
                                //save data in outgionsummary table
                                SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                            }


                    }
                }

            }
            catch (Exception ex)
            {
            }
        }
        #endregion
}

in console app code

appconfic code

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="ConsoleApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <system.serviceModel>
    <bindings />
    <client />
  </system.serviceModel>
  <!--<applicationSettings>
    <ConsoleApp.Properties.Settings>
      --><!--<setting name="ConsoleApp_com_qssi_ws_PHWebServices" serializeAs="String">
      </setting>--><!--
    </ConsoleApp.Properties.Settings>
  </applicationSettings>-->
  <appSettings>
    <add key="SchedulerUrl" value="http://localhost:15536/api/Forecast/DemandForecastPurchaseOrderDailyScheduler" />
    <add key="Domain" value="http://localhost" />
    <add key="UserName" value="charan_rathi@fulcrumww.com" />
    <add key="Password" value="admin" />
  </appSettings>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

console code

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {


        static void Main(string[] args)
        {
            RunDailyScheduler();
        }

        private static void RunDailyScheduler()
        {
            try
            {
                string Domain = ConfigurationManager.AppSettings["Domain"];
                string UserName = ConfigurationManager.AppSettings["UserName"];
                string Password = ConfigurationManager.AppSettings["Password"];

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["SchedulerUrl"]);
                webRequest.Method = "POST";
                webRequest.ContentLength = 0;
                webRequest.PreAuthenticate = true;
                webRequest.Credentials = new NetworkCredential(UserName, Password, Domain);
                var response = (HttpWebResponse)webRequest.GetResponse();

                //HttpWebRequest webRequest1 = (HttpWebRequest)WebRequest.Create(ConfigurationSettings.AppSettings["ScheduleUrl"]);
                //webRequest1.Method = "POST";
                //webRequest1.ContentLength = 0;
                //webRequest1.Credentials = new NetworkCredential(UserName, Password, Domain);
                //var res = (HttpWebResponse)webRequest1.GetResponse();
            }

            catch (Exception ex)
            {
                //log the exception here
            }
        }
    }
}


No comments:

Post a Comment