zoukankan      html  css  js  c++  java
  • 使用Topshelf创建Windows服务(转)

    Calculator
    
       public class Calculator : ICalculator
        {
            public Calculator()
            {
                Logger = LogManager.GetCurrentClassLogger();
            }
    
            public ILogger Logger { get; set; }
    
            public CalculationResult Add(string n1, string n2)
            {
                Logger?.Info($"received request to Add {n1} to {n2}");
               
                return Calculate(n1, n2, _add);
            }
    
            public CalculationResult Subtract(string n1, string n2)
            {
                Logger?.Info($"received request to Subtract {n2} from {n1}");
                
                return Calculate(n1, n2, _subtract);
            }
    
            public CalculationResult Multiply(string n1, string n2)
            {
                Logger?.Info($"received request to Multiply {n1} by {n2}");
                
                return Calculate(n1, n2, _multiply);
            }
    
            public CalculationResult Divide(string n1, string n2)
            {
                Logger?.Info($"received request to Divide {n1} by {n2}");
               
                return Calculate(n1, n2, _divide);
            }
    
            private static CalculationResult Calculate(string n1, string n2, Func<double, double, double> calculate)
            {
                var value1 = n1.ToDouble();
                if (!value1.HasValue)
                {
                    return GetCouldNotConvertToDoubleResult(n1);
                }
    
                var value2 = n2.ToDouble();
                if (!value2.HasValue)
                {
                    return GetCouldNotConvertToDoubleResult(n2);
                }
    
                double result = calculate(value1.Value, value2.Value);
                return new CalculationResult
                {
                    Answer = result
                };
            }
    
            private static readonly Func<double, double, double> _add = (value1, value2) => value1 + value2;
            private static readonly Func<double, double, double> _subtract = (value1, value2) => value1 - value2;
            private static readonly Func<double, double, double> _multiply = (value1, value2) => value1 * value2;
            private static readonly Func<double, double, double> _divide = (value1, value2) => value2 == 0 ? double.NaN : value1 / value2;
            
            private static CalculationResult GetCouldNotConvertToDoubleResult(string input)
            {
                return new CalculationResult
                {
                    Message = $"Could not convert '{input}' to a double"
                };
            }
        }


    https://github.com/NotMyself/CalculatorService.git

    http://www.cnblogs.com/jys509/p/4614975.html

  • 相关阅读:
    [IOI1994][USACO1.5]数字三角形 Number Triangles
    和为给定数
    小凯的疑惑
    棋盘
    【2020NOI.AC省选模拟#2】C. 送分题
    【NOI OL #2】涂色游戏
    【NOI OL #3】小结
    【NOI OL #1】最小环
    【NOI OL #1】冒泡排序
    【NOI OL #1】序列
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/7425295.html
Copyright © 2011-2022 走看看