-1

I have a web application, and in the DAL file I have some methods. The methods get results by calling an API. For example:

public string GetUserName(int userID)
{
     HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
     GETRequest.Method = "GET";

     HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
     Stream GETResponseStream = GETResponse.GetResponseStream();
     StreamReader srResponse = new StreamReader(GETResponseStream);

     return srResponse.ReadToEnd();     
}

My requirement is that I need to execute these methods via command the line and show the result. I do not understand how to do this; please suggest a way to proceed.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
ANP
  • 15,287
  • 22
  • 58
  • 79
  • Are you having trouble creating a console application? What is the specific difficulty you are having? – bobbymcr Dec 26 '11 at 06:51
  • You can create a console application and then call the the above method in your code. – Nitin Rastogi Dec 26 '11 at 06:51
  • You need to understand first that Method itself is not a process or a script to execute. So make it a part of a process or a script and then execute it. So in this case, its a Console/WinForm/WebForm application (inturn a process) and then execute it. – Zenwalker Dec 26 '11 at 06:56
  • I can create a console application. But suppose I need to execute the method GetUserName(int userID) in the command line(cmd).How I can do this? I am looking for the way like the user will open the command line and type GetUserName(1) and hit enter it will display the name. – ANP Dec 26 '11 at 07:13

3 Answers3

3

Well, you need to parse the command line arguments. Let's say we define our command line format like this: yourprogram.exe function_name paramValue, then we need to do the following:

public static void Main(string[] args)
{
    switch(args[0])
    {
        case "GetUserName" :
        Console.WriteLine(GetUserName(args[1]));
        break;
        case "YourOtherMethod":
        //...
        break;
    }
}

then, you would execute yourprogram.exe GetUserName 5 in the console.

Community
  • 1
  • 1
Adam
  • 15,537
  • 2
  • 42
  • 63
1

I think there is not direct way. You have to create/add console application project, add the reference of DAL and use classes and their methods in Main().

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • Thanks for your response. I can create a console application and add the methods to it.But how can I execute those in command line(cmd)? – ANP Dec 26 '11 at 07:02
  • I think guys are right - there is no way to do it through cmd (at least without additional steps). You can try to create your object and call your method through PowerShell (it allows to use .NET objects directly) or you can try to create some command-line utility, which will get dll-filename and class/method name and call that method through reflection. But as a lot of guys said - there is no direct way to do it from cmd. – chopikadze Dec 26 '11 at 07:08
0

after reading your comments, I think you are talking about a command line utility where the user could type-in the method name to receive the output. If that's the case then I'd suggest to create a new console application, parse and check for proper arguments, and if it's matched, then make a webrequest to the appropriate url(url of the webservice), and then extract the desired output from the response stream returned by the server.

MrClan
  • 6,402
  • 8
  • 28
  • 43
  • On cmd, then the user will then have to type something like: **C:\>** D:\VSProjects\MyWebServiceApp GetUserName(55) where **D:\VSProjects\MyWebServiceApp** is obviously the full path to your application. – MrClan Dec 26 '11 at 11:24