zoukankan      html  css  js  c++  java
  • 通过web方式远程调用ANT编译(一)

    由于WEB程序的安全设置,特别是WIN2003+IIS6的安全设置不允许WEB程序随意调用服务器上可执行程序,因此需要使用.NET Remoting。
    Server
     1using System;
     2using System.Runtime.Remoting;
     3using System.Runtime.Remoting.Channels;
     4using System.Runtime.Remoting.Channels.Tcp;
     5
     6namespace RemotingItem
     7{
     8    public class Server
     9    {
    10        /////////////////////////////////////////////////////////////////////////////
    11        ///constructor

    12        public Server()
    13        {
    14        }

    15        /////////////////////////////////////////////////////////////////////////////
    16        ///main method

    17        public static int Main(string [] args)
    18        {
    19            Console.WriteLine("Server Activated!!!!!!!!!!!!!!!!!!");
    20            //select channel to communicate
    21            TcpChannel chan = new TcpChannel(18085);
    22            Console.WriteLine("Port:18085");
    23            ChannelServices.RegisterChannel(chan);    //register channel
    24            //register remote object
    25            RemotingConfiguration.RegisterWellKnownServiceType(
    26                typeof(RemoteObject),
    27                "RemotingServer",
    28                WellKnownObjectMode.Singleton );
    29            //inform console
    30            
    31            Console.WriteLine ("Waiting"); 
    32            Console.ReadLine (); //等待执行下一命令
    33            return 0;
    34        }

    35    }

    36}

    37
    RemoteObject.dll
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.Diagnostics;
    using System.Threading;
    using System.IO;

    namespace RemotingItem
    {
        
    public class RemoteObject : MarshalByRefObject
        
    {
            
    //////////////////////////////////////////////////////////////////////////////
            
    ///constructor

            public RemoteObject()
            
    {
                Console.WriteLine(
    "Remote object activated");
            }

        
            
    //////////////////////////////////////////////////////////////////////////////
            
    ///return message reply 测试服务端是否激活

            public String ReplyMessage(String msg)
            
    {
                Console.WriteLine(
    "Client : "+msg);//print given message on console             
                return "Server : Yeah! I'm here";
            }


            
    /// <summary>
            
    /// 
            
    /// </summary>
            
    /// <param name="WorkingDirectory">目标目录,可以为空</param>
            
    /// <param name="CmdStr">命令组合,以逗号分隔</param>
            
    /// <param name="ResultDirectory">结果存放目录,可以为空,为空时不输出日志</param>
            
    /// <returns></returns>

            public string DosCommand(String WorkingDirectory, String CmdStr,String ResultDirectory)
            
    {
                
    try
                
    {
                    
    string output="";
                    
    if (WorkingDirectory.Substring(WorkingDirectory.Length-1,1!= @"\")
                    
    {
                        WorkingDirectory 
    =WorkingDirectory + @"\";
                    }

                    
    //调用CMD
                    ProcessStartInfo psi = new ProcessStartInfo("cmd"); 
                    
    if (WorkingDirectory.Length >1)
                        psi.WorkingDirectory
    =@WorkingDirectory;
                    psi.RedirectStandardOutput 
    = true
                    psi.RedirectStandardInput 
    = true
                    psi.UseShellExecute 
    = false
                    Process p 
    = Process.Start(psi); 
                    
    string [] command = CmdStr.Split(',');
                    
    foreach (string s in command)
                    
    {
                        p.StandardInput.WriteLine(@s); 
                    }

                    p.StandardInput.WriteLine(
    @"exit"); 
                    
                    
    if (ResultDirectory.Length >0)
                    
    {
                        output 
    = p.StandardOutput.ReadToEnd(); 
                        WriteFile(ResultDirectory,output);
                    }

                    p.WaitForExit(); 
                    Console.WriteLine(output);
                    
                    
    return "true";
                }

                
    catch (Exception ex)
                
    {
                    Console.WriteLine(ex.Message.ToString());
                    
    return ex.Message.ToString();
                }
                
            }


            
    private void WriteFile(String srcPath, String text)
            
    {             
                
    if (srcPath.Substring(srcPath.Length-1,1== @"\")
                
    {
                    srcPath 
    =srcPath.Substring(0,srcPath.Length-1);
                }

                
    string OutputFile = srcPath + @"\Log.htm";
                
    if (File.Exists(OutputFile))
                
    {
                    File.Delete(OutputFile);
                }
                

                
    try
                
    {                
                    FileStream fsWriter 
    = new FileStream(OutputFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
                    StreamWriter fileWrite 
    = new StreamWriter(fsWriter, System.Text.Encoding.Default);
                    fileWrite.Write(text);
                    fileWrite.Close();
                    fileWrite 
    = null;
                    fsWriter.Close();
                    fsWriter 
    = null;
                }

                
    catch (Exception)
                
    {
                }

            }

        }

    }


  • 相关阅读:
    设计模式
    三——第二部分——第二篇论文 计划建设SQL Server镜像
    非正确使用浮点数据由项目产生BUG讨论的问题
    重写ViewPager实施单一交有关切换到这个问题,并没有缓存
    1159 Palindrome(最小插入回文串)
    CSDN-markdown编者LaTex数学公式
    找工作总结
    开展:随笔记录 OSGI的jar增加了一些小问题和注意事项
    js jquery版本号 金额千分之一转换功能(非规范,高效率)
    dom4j.jar有什么作用?
  • 原文地址:https://www.cnblogs.com/linn/p/489379.html
Copyright © 2011-2022 走看看