using CommonLibrary.Configuration;
using CommonLibrary.ServiceClients;
using CommonLibrary.Utility;
using InventoryService.DataAccess;
using InventoryService.Entity;
using System.Collections.Generic;
using System.Diagnostics;
namespace InventoryService.Jobs.Inventory
public class InventorySynchronizerJob
private int listeningIntervalInSec;
private byte warehouseId;
private byte maxWorkerThreadCount;
InventorySyncItemDALC inventorySyncItemDALC;
InventorySyncPerformanceLogDALC inventorySyncPerformanceLogDALC = new InventorySyncPerformanceLogDALC();
private Settings settings;
public InventorySynchronizerJob(byte warehouseId, byte regionId)
this.regionId = regionId;
this.warehouseId = warehouseId;
inventorySyncItemDALC = new InventorySyncItemDALC();
inventorySyncItemDALC.UpdateStatusSetIgnored();
mainThread = new Thread(new ThreadStart(Run));
private void InitilizeSettings()
settings = ConfigurationServiceClient.GetInstance().Settings;
listeningIntervalInSec = int.Parse(settings["InventorySyncJobListeningIntervalInSec"]);
maxWorkerThreadCount = byte.Parse(settings["InventorySyncJobMaxWorkerThreadCount"]);
Global.Log("InventorySynchronizerJob Starting...", InventoryService.SERVICENAME);
double intervalInMilis = new TimeSpan(0, 0, listeningIntervalInSec).TotalMilliseconds;
List<InventoryServiceInventorySyncItem> items = inventorySyncItemDALC.GetAllItem();
double itemCount = items.Count();
int itemPerThread = (int)Math.Ceiling(itemCount / maxWorkerThreadCount);
BlockIterator<InventoryServiceInventorySyncItem> blockItarator = new BlockIterator<InventoryServiceInventorySyncItem>(items, itemPerThread);
List<InventorySynchronizerWorker> workerList = new List<InventorySynchronizerWorker>();
Stopwatch timer = new Stopwatch();
DateTime beginDate = DateTime.Now;
while (blockItarator.HasNext())
List<InventoryServiceInventorySyncItem> syncItems = blockItarator.Next();
InventorySynchronizerWorker worker = new InventorySynchronizerWorker(syncItems, warehouseId, regionId);
foreach (InventorySynchronizerWorker worker in workerList)
InventoryServiceInventorySyncPerformanceLog performanceLog = new InventoryServiceInventorySyncPerformanceLog();
performanceLog.BeginDate = beginDate;
performanceLog.EndDate = DateTime.Now;
performanceLog.ProcessTimeInSec = timer.GetTimerTotalSecondsValue();
performanceLog.ItemCount = (int)itemCount;
inventorySyncPerformanceLogDALC.Insert(performanceLog);
catch (Exception exception)
Global.LogException(exception, InventoryService.SERVICENAME);
Global.Log("InventorySynchronizerJob Stopping...", InventoryService.SERVICENAME);
private void Wait(double timeInMilis)
while (t < timeInMilis && !stopped)
-------------------------------------------------------
public static class ServiceProvider
private static ServiceContract.Services.ILocalizationService localizationService;
public static ServiceContract.Services.ILocalizationService GetLocalizationService()
if (localizationService == null)
if (localizationService == null)
localizationService = ServiceProvider.GetVeryLargeProxy<ServiceContract.Services.ILocalizationService>(LocalizationServiceUrl, 2147483647);
return localizationService;
private static T GetVeryLargeProxy<T>(string location, int maxMessageSize)
System.ServiceModel.Channels.BinaryMessageEncodingBindingElement binaryEncodingElement = new System.ServiceModel.Channels.BinaryMessageEncodingBindingElement();
binaryEncodingElement.ReaderQuotas.MaxArrayLength = 163840;
binaryEncodingElement.ReaderQuotas.MaxDepth = 1024;
binaryEncodingElement.ReaderQuotas.MaxBytesPerRead = 8192;
binaryEncodingElement.ReaderQuotas.MaxStringContentLength = 262144;
System.ServiceModel.Channels.CustomBinding binding = new System.ServiceModel.Channels.CustomBinding(binaryEncodingElement,
System.ServiceModel.Channels.SecurityBindingElement.CreateUserNameOverTransportBindingElement(), transportElement);
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = maxMessageSize;
binding.MaxBufferSize = maxMessageSize;
binding.ReaderQuotas.MaxArrayLength = 1638400;
binding.ReaderQuotas.MaxDepth = 1024;
binding.ReaderQuotas.MaxBytesPerRead = 8192;
binding.ReaderQuotas.MaxStringContentLength = 2621440;
binding.CloseTimeout = new TimeSpan(0, 3, 0);
binding.OpenTimeout = new TimeSpan(0, 3, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 3, 0);
EndpointAddress address = new EndpointAddress(location);
ChannelFactory<T> _channelFactory = new ChannelFactory<T>(binding, address);
foreach (OperationDescription op in _channelFactory.Endpoint.Contract.Operations)
DataContractSerializerOperationBehavior dataContractBehavior =
op.Behaviors[typeof(DataContractSerializerOperationBehavior)]
as DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
_channelFactory.Credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
return _channelFactory.CreateChannel();
private static T GetVeryLargeProxyForOmniChannel<T>(string location, int maxMessageSize)
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = maxMessageSize;
binding.MaxBufferSize = maxMessageSize;
binding.ReaderQuotas.MaxArrayLength = 163840;
binding.ReaderQuotas.MaxDepth = 1024;
binding.ReaderQuotas.MaxBytesPerRead = 8192;
binding.ReaderQuotas.MaxStringContentLength = 6553600;
binding.CloseTimeout = new TimeSpan(0, 10, 0);
binding.OpenTimeout = new TimeSpan(0, 10, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 10, 0);
EndpointAddress address = new EndpointAddress(location);
ChannelFactory<T> _channelFactory = new ChannelFactory<T>(binding, address);
foreach (OperationDescription op in _channelFactory.Endpoint.Contract.Operations)
DataContractSerializerOperationBehavior dataContractBehavior =
op.Behaviors[typeof(DataContractSerializerOperationBehavior)]
as DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
_channelFactory.Credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
return _channelFactory.CreateChannel();
private static T GetVeryLargeProxy<T>(string location, int maxMessageSize, IEndpointBehavior behavior)
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = maxMessageSize;
binding.MaxBufferSize = maxMessageSize;
binding.ReaderQuotas.MaxArrayLength = 163840;
binding.ReaderQuotas.MaxDepth = 1024;
binding.ReaderQuotas.MaxBytesPerRead = 8192;
binding.ReaderQuotas.MaxStringContentLength = 2621440;
binding.CloseTimeout = new TimeSpan(0, 3, 0);
binding.OpenTimeout = new TimeSpan(0, 3, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 3, 0);
EndpointAddress address = new EndpointAddress(location);
ChannelFactory<T> _channelFactory = new ChannelFactory<T>(binding, address);
_channelFactory.Endpoint.Behaviors.Add(behavior);
foreach (OperationDescription op in _channelFactory.Endpoint.Contract.Operations)
DataContractSerializerOperationBehavior dataContractBehavior =
op.Behaviors[typeof(DataContractSerializerOperationBehavior)]
as DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
_channelFactory.Credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
return _channelFactory.CreateChannel();
private static T GetVeryLargeProxy<T>(string location, int maxMessageSize, string bindingType)
return GetVeryLargeProxy<T>(location, maxMessageSize);
WSDualHttpBinding binding = new WSDualHttpBinding();
binding.MaxReceivedMessageSize = maxMessageSize;
binding.ReaderQuotas.MaxArrayLength = 163840;
binding.ReaderQuotas.MaxDepth = 1024;
binding.ReaderQuotas.MaxBytesPerRead = 8192;
binding.ReaderQuotas.MaxStringContentLength = 262144;
binding.CloseTimeout = new TimeSpan(0, 3, 0);
binding.OpenTimeout = new TimeSpan(0, 3, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 3, 0);
EndpointAddress address = new EndpointAddress(location);
ChannelFactory<T> _channelFactory = new ChannelFactory<T>(binding, address);
foreach (OperationDescription op in _channelFactory.Endpoint.Contract.Operations)
DataContractSerializerOperationBehavior dataContractBehavior =
op.Behaviors[typeof(DataContractSerializerOperationBehavior)]
as DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
return _channelFactory.CreateChannel();
private static T GetVeryLargeProxy<T>(string location)
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = 65536000;
binding.MaxBufferSize = 65536000;
binding.ReaderQuotas.MaxArrayLength = 163840;
binding.ReaderQuotas.MaxDepth = 1024;
binding.ReaderQuotas.MaxBytesPerRead = 8192;
binding.ReaderQuotas.MaxStringContentLength = 262144;
binding.CloseTimeout = new TimeSpan(0, 3, 0);
binding.OpenTimeout = new TimeSpan(0, 3, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 3, 0);
EndpointAddress address = new EndpointAddress(location);
ChannelFactory<T> _channelFactory = new ChannelFactory<T>(binding, address);
foreach (OperationDescription op in _channelFactory.Endpoint.Contract.Operations)
DataContractSerializerOperationBehavior dataContractBehavior =
op.Behaviors[typeof(DataContractSerializerOperationBehavior)]
as DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
return _channelFactory.CreateChannel();
private static T GetVeryLargeProxyWithMtom<T>(string location)
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = 6553600;
binding.MaxBufferSize = 6553600;
binding.ReaderQuotas.MaxArrayLength = 163840;
binding.ReaderQuotas.MaxDepth = 1024;
binding.ReaderQuotas.MaxBytesPerRead = 8192;
binding.ReaderQuotas.MaxStringContentLength = 2048000;
EndpointAddress address = new EndpointAddress(location);
ChannelFactory<T> _channelFactory = new ChannelFactory<T>(binding, address);
foreach (OperationDescription op in _channelFactory.Endpoint.Contract.Operations)
DataContractSerializerOperationBehavior dataContractBehavior =
op.Behaviors[typeof(DataContractSerializerOperationBehavior)]
as DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
return _channelFactory.CreateChannel();