10
Answers

C# obtain information from progam it calls

dc

dc

12y
1.5k
1

I have a question about how to obtain the results, condition codes and messages from a C# 2008 console application and a web service
  Basically I have received the C# 2008 console application code that calls a remote webservice and consumes the results. The console application runs by giving it commands to know what method in the web service to call.
  Now I am going to write a C# 2010 service application that will call the C# 2008 console and run the commands in a specificed order. There are three types of calls that need to be called in a specified order.

Basically the steps would be:

1. write the new .net app to make callls in a sequential order to a console app progrm.

2. The console application calls the web service and obtains the results,

3. The new .net app obtains the results of the web service from the console application it called.
 

Let me know how I can accomplish my goal with code and/or references.


Answers (10)
0
Vulpes

Vulpes

NA 98.3k 1.5m 12y
It sounds like a WCF service is being used here.

I don't know much about WCF but I found this blog post which describes what the .svcmap file does:

http://blogs.msdn.com/b/lifenglu/archive/2007/06/15/svcmap-file.aspx

As far as the log file is concerned, the console app needs to write this in a format which the new app can understand and therefore parse out the info.

If we assume that the format will be similar to the one assumed in my previous post, then the Console app could also define a Customer class in exactly the same way as the new app.

You could then create a List<Customer> and populate it with info retrieved from the web service:

class Customer
{
   public int Number {get; set;}
   public string[] Messages {get; set;}
   // other properties

List<Customer> customers  = new List<Customer>();
// populate list with Customer objects using the info retrieved from the web service

using (StreamWriter sw = new StreamWriter("customer.log"))
{
   foreach(Customer cust in customers)
   {
      sw.WriteLine("Customer number : " + cust.Number);
      // other properties
      foreach(string message in cust.Messages) sw.WriteLine("Message : " + message);
   }

   sw.Close();
}

If the messages relate to specific customers, I don't think you need a second log file as you can deal with them by including a Messages property in the Customer class, as the above code shows.

Even if they don't relate to a particular customer, you could tack them on at the end of the  file by prefixing them with, say, "Global message : ".

Accepted
0
dc

dc

NA 629 0 12y
I would like to ask/mention the following about the log file:

1. You mentioned the following, "Basically, the log file needs to have a fixed format, so that the new app can read it."

Can  you show me code that I can add to the console application so the log file  can be in a fixed format?

2. I am also assuming that a log file from the console application should write out any  messages that have been generated by the web service.

Thus would I want the messages required for step #1 in a fixed format to be in the same file with messages that are generated by the web service? 
I would think I would want two log files? Is that possible? 
Can you show me code to have two separate log files? 
0
dc

dc

NA 629 0 12y
I wanted to mention the following about the *.xsd file  a. There are  xml files that access the xsd file. the xml files that access the *.xsd files are the 
  - *.wsdl and
  - reference.svcmap.
  I know the wsdl file is the web service but do you know what the reference.svcmap is (the code that consumes the web service?)
  b. the xsd is auto-generated is generated by a tool.
  c. the xsd is connected to the ip address of the web service. The web service is connected is the xml since it is setup to parse the dom object.

0
Vulpes

Vulpes

NA 98.3k 1.5m 12y
Basically, the log file needs to have a fixed format, so that the new app can read it.

For example, suppose the log file contains something like this:


Customer number : 1

// lines of info for c/n 1

Customer number : 2

// lines of info for c/n 2


The new app can then read the file as follows:

class Customer
{
   public int Number {get; set;}
   // other properties



// ...


List<Customer> customers  = new List<Customer>();

string[] lines = File.ReadAllLines("customer.log");

Customer currentCust = null;

foreach(string line in lines) 
{
   if (line.StartsWith("Customer number"))  // time to process a new Customer
   {
      if (currentCust != null) customers.Add(currentCust); // save existing Customer, if any
      currentCust = new Customer(); // create new Customer object
      currentCust.Number = int.Parse(line.Split(':')[1]); // set Number property
   }
 
   // set currentCust properties using data from following line(s)


customers.Add(currentCust); // add final Customer object to list

// code to process customer list          
0
dc

dc

NA 629 0 12y
Thank you very much for your answers for far!

I still want to mention/ask the following:

1. Your statemenet, "Going back to the first of your original questions, do you mean how would the console application (as opposed to the new app) obtain the values of the customers it needs to loop through?"

I do mean the new app (not the console application). The console application obtains a list of customer numbers and the console application can parses the list of customer numbers.
  However I need to pass all the customer numbers to the new application.

  Thus can you tell me how the console application can the list of customer numbers to the new application. It is the new application that needs to process each customer number in a sequential order.

2. I like the idea of writing the output from the console application to a new file each time it writes to the log file for the new application to access. However my is, do you have code that would show me how the new application can read the log file that is in a text file? (Basically how would I know  where the file starts and where the file ends, carriage return characters?)




It would be easier to use a new file for each call but,
0
Vulpes

Vulpes

NA 98.3k 1.5m 12y
Well, if there are xsd files present, then that suggests that XML files are being used by the console application to store data in a particular format.

I'd search the source code to see if the string ".xsd" occurs anywhere. If it does, then code which validates an XML file should be close by. If it doesn't then probably (as you surmised) it's just been supplied for information.

Going back to the first of your original questions, do you mean how would the console application (as opposed to the new app) obtain the values of the customers it needs to loop through?

If so, then I'd suggest you supply the customer numbers as a comma separated list being careful not to include any spaces.

So for example if you supplied 1,2,3 as the first command line argument to the console app, the latter could recover the numbers with code such as the following in its Main() method :

static void Main(string[] args)
{
   string[] tempArray = args[0].Split(',');
   int[] customerNumbers = new int[tempArray.Length];
   for(int i = 0; i < tempArray.Length; i++) customerNumbers[i] = int.Parse(tempArray[i]);
   
   // rest of code
}

With regard to the second question, if the console app quits after obtaining results from the web service and writing them to a file, the new windows service app just needs to wait for it to quit so that it knows it's safe to access the file.

It would be easier to use a new file for each call but, if for some reason, you wanted to append to the same file, the new app would need some way of telling where the new data began - perhaps a date and time on a single line.
0
dc

dc

NA 629 0 12y
The following are answers to your questions and a couple of extra points I want to make:

1. I do have  access to the console application's source code (and can therefore alter it).
 
2. Extra note: there is an *.xsd file that is included in the console application. When I just look at this *.xsd file, it looks like a large percentage of the error messages that I have seen might come from this code. My problem is that I do not know how this *.xsd code is wired into the application to cause the error messages to be displayed. Is there some kind of a way you would know how these error messages are wired into the C# 2008 application?

This *.xsd file looks like this is the schema defintion of the database the contract shop uses. Maybe this *.xsd file is not even used by this console application. Maybe it is in the code base just for me to look at. Is there a way that I can tell of the *.xsd file is not being used? if so, I would I be able to tell?

I do know that the webservice that I call must be written in java since I see alot of java runtime errors.

3. The console application is only set up to run when I run the commands from the console application.
As far as I can tell the log messages just appear on a popup dos window. 
0
Vulpes

Vulpes

NA 98.3k 1.5m 12y
Before I try to answer those questions, can you clarify a couple of points.

Do you have access to the console application's source code (and can therefore alter it) or do you have to live with it as it stands?

Is the console application designed to run continuously, writing results to the log file from time to time or does it collect all the results at once, write them to the file, and then exit?
0
dc

dc

NA 629 0 12y
You have some very good suggestions! I have the following additional questions to ask you:
1. One of the commands is a list of cusomter numbers that need to be processed. The second command in the sequence is to download informaton from the web service one customer at a time. Thus how would this new .net application obtain the values of the customers it needs to loop through?

Would I need to save the data to a database or some other method? What do you suggest?

2. How would the new .net application use the results of the text file that is written as a log file? Basically how can the .net application know how to read the log file to obtain the results it needs to work with?
0
Vulpes

Vulpes

NA 98.3k 1.5m 12y
I'd pass command line arguments, in the correct order, to the console application when you launch it from the windows service.

The console application can then read the command line arguments, call the web service, obtain the results and write them to a text file before exiting.

When the console application ends (use Process.WaitForExit method), the windows service can then read the results from the file and (if necessary) delete it.