コード例 #1
0
        private static void Main(string[] args)
        {
            try
            {
                // connect to the APAS.
                var client = new SystemServiceClient();
                client.Open();

                var helpText = new StringBuilder();
                var stream   = new StringWriter(helpText);
                var parser   = new Parser(config =>
                {
                    config.EnableDashDash = true;
                    config.CaseSensitive  = false;
                    config.HelpWriter     = stream;
                });

                parser.ParseArguments <Option>(args)
                .MapResult(
                    (Option opts) =>
                {
                    UserProc(client, opts: opts);
                    return(0);
                },
                    errs =>
                {
                    var erring = "";

                    if (errs.IsHelp() || errs.IsVersion())
                    {
                        erring = "";
                    }
                    else
                    {
                        erring = "脚本启动参数错误。\r\n";
                    }

                    client.__SSC_LogError(erring + helpText.ToString());
                    throw new Exception(erring + helpText.ToString());
                });
            }
            catch (AggregateException ae)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.BackgroundColor = ConsoleColor.Red;

                var ex = ae.Flatten();

                ex.InnerExceptions.ToList().ForEach(e =>
                {
                    Console.WriteLine($"Error occurred, {e.Message}");
                });

                Console.ResetColor();
            }
            //Console.WriteLine("Press any key to exit.");

            //Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            // Camera Remote Access Service.
            var camClient = new CamRAC.CamRemoteAccessContractClient();

            // APAS Remote Access Service.
            var client = new SystemServiceClient();

            try
            {
                client.Open();

                client.__SSC_Connect();

                // perform the user process.
                UserProc(client, camClient);

                client.__SSC_Disonnect();
            }
            catch (AggregateException ae)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.BackgroundColor = ConsoleColor.Red;

                var ex = ae.Flatten();

                ex.InnerExceptions.ToList().ForEach(e =>
                {
                    Console.WriteLine($"Error occurred, {e.Message}");
                });

                Console.ResetColor();
            }
            finally
            {
                client.Close();
            }
            //Console.WriteLine("Press any key to exit.");

            //Console.ReadKey();
        }
        private static void Main(string[] args)
        {
            var isExceptionThrown = false;
            var errText           = "";
            var wcfClient         = new SystemServiceClient();

            try
            {
                wcfClient.Open();
                wcfClient.__SSC_Connect();

                // print the script version
                wcfClient.__SSC_LogInfo($"Script Version: v{Assembly.GetExecutingAssembly().GetName()}");

                var helpWriter = new StringWriter();
                var parser     = new Parser(with =>
                {
                    with.CaseSensitive  = false;
                    with.EnableDashDash = true;
                    with.HelpWriter     = helpWriter;
                });

                parser.ParseArguments <TurnOnOptions, TurnOffOptions>(args)
                .MapResult(
                    (TurnOnOptions opts) =>
                {
                    TurnOn(wcfClient, opts.Channel, opts.IBias);
                    return(0);
                },
                    (TurnOffOptions opts) =>
                {
                    TurnOff(wcfClient, opts.Channel);
                    return(0);
                },
                    errs =>
                {
                    var myErr = "";
                    if (errs.IsHelp() || errs.IsVersion())
                    {
                        myErr = helpWriter.ToString();
                    }
                    else
                    {
                        myErr = $"启动参数错误。\r\n{helpWriter}";
                    }

                    throw new Exception(myErr);
                });

                wcfClient.__SSC_Disconnect();
            }
            catch (AggregateException ae)
            {
                var ex = ae.Flatten();
                ex.InnerExceptions.ToList().ForEach(e =>
                {
                    errText = ex.Message;
                    Console.Error.WriteLine(errText);
                });
                isExceptionThrown = true;
            }
            catch (TimeoutException timeProblem)
            {
                errText = "The service operation timed out. " + timeProblem.Message;
                Console.Error.WriteLine(errText);
            }
            // Catch unrecognized faults. This handler receives exceptions thrown by WCF
            // services when ServiceDebugBehavior.IncludeExceptionDetailInFaults
            // is set to true.
            catch (FaultException faultEx)
            {
                errText = "An unknown exception was received. "
                          + faultEx.Message
                          + faultEx.StackTrace;
                Console.Error.WriteLine(errText);
            }
            // Standard communication fault handler.
            catch (CommunicationException commProblem)
            {
                errText = "There was a communication problem. " + commProblem.Message + commProblem.StackTrace;
                Console.Error.WriteLine(errText);
            }
            catch (Exception ex)
            {
                errText = ex.Message;
                Console.Error.WriteLine(errText);
                isExceptionThrown = true;
            }
            finally
            {
                wcfClient.Abort();
                if (isExceptionThrown)
                {
                    // try to output the error message to the log.
                    try
                    {
                        using (wcfClient = new SystemServiceClient())
                        {
                            wcfClient.__SSC_LogError(errText);
                            wcfClient.Abort();
                        }
                    }
                    catch (Exception)
                    {
                        // ignore
                    }


                    Environment.ExitCode = -1;
                }
            }
        }
コード例 #4
0
        private static void Main(string[] args)
        {
            var isExceptionThrown = false;
            var errText           = "";
            var wcfClient         = new SystemServiceClient();

            try
            {
                wcfClient.Open();
                wcfClient.__SSC_Connect();

                // print the script version
                wcfClient.__SSC_LogInfo($"Script Version: v{Assembly.GetExecutingAssembly().GetName().Version}");

                if (args.Length != 1)
                {
                    var err = "未指定参数,或参数格式错误。";
                    throw new Exception(err);
                }

                PARAM = args[0];

                // perform the user process.
                UserProc(wcfClient);

                wcfClient.__SSC_Disconnect();
            }
            catch (AggregateException ae)
            {
                var ex = ae.Flatten();
                ex.InnerExceptions.ToList().ForEach(e =>
                {
                    errText = ex.Message;
                    Console.Error.WriteLine(errText);
                });
                isExceptionThrown = true;
            }
            catch (TimeoutException timeProblem)
            {
                errText = "The service operation timed out. " + timeProblem.Message;
                Console.Error.WriteLine(errText);
            }
            // Catch unrecognized faults. This handler receives exceptions thrown by WCF
            // services when ServiceDebugBehavior.IncludeExceptionDetailInFaults
            // is set to true.
            catch (FaultException faultEx)
            {
                errText = "An unknown exception was received. "
                          + faultEx.Message
                          + faultEx.StackTrace;
                Console.Error.WriteLine(errText);
            }
            // Standard communication fault handler.
            catch (CommunicationException commProblem)
            {
                errText = "There was a communication problem. " + commProblem.Message + commProblem.StackTrace;
                Console.Error.WriteLine(errText);
            }
            catch (Exception ex)
            {
                errText = ex.Message;
                Console.Error.WriteLine(errText);
                isExceptionThrown = true;
            }
            finally
            {
                wcfClient.Abort();
                if (isExceptionThrown)
                {
                    // try to output the error message to the log.
                    try
                    {
                        using (wcfClient = new SystemServiceClient())
                        {
                            wcfClient.__SSC_LogError(errText);
                            wcfClient.Abort();
                        }
                    }
                    catch (Exception)
                    {
                        // ignore
                    }


                    Environment.ExitCode = -1;
                }
            }
        }