user2659865
user2659865

Reputation: 19

How to pass multiple requests at a time in WCF/client

With the below code I'm able to process the requests one by one. This is an asynchronous process so I don't need to get a response. I am only passing the requests.

class Program
{
    static void Main(string[] args)
    {
        ProfileRequestData(); 
    }

    private static void ProfileRequestData()
    { 
        ProfileRequest.Request[] req = new ProfileRequest.Request[4];
        //req.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

        req[0] = new ProfileRequest.Request();
        req[1] = new ProfileRequest.Request();
        req[2] = new ProfileRequest.Request();
        req[3] = new ProfileRequest.Request();

        req[0].Record = new ProfileRequest.RequestRecord();
        req[1].Record = new ProfileRequest.RequestRecord();
        req[2].Record = new ProfileRequest.RequestRecord();
        req[3].Record = new ProfileRequest.RequestRecord();

        req[0].Record.Id = "asdasd123";
        req[1].Record.Id = "asdasd456";
        req[2].Record.Id = "asdasd789";
        req[3].Record.Id = "addjlk123"; 

        ProfileRequest.ProfileRequestClient serviceClient = new ProfileRequest.ProfileRequestClient();

        serviceClient.ClientCredentials.UserName.UserName = @"Liberty\nzy0105";
        serviceClient.ClientCredentials.UserName.Password = "abcddf"; 

        serviceClient.ProfileRequest(req[0]);
        serviceClient.ProfileRequest(req[1]);
        serviceClient.ProfileRequest(req[2]);
        serviceClient.ProfileRequest(req[3]);
    } 
}

What I want to do is to process all the requests at a time not one by one.

Can anyone, please, provide the code for the requirement???

Upvotes: 0

Views: 175

Answers (2)

Tolga Evcimen
Tolga Evcimen

Reputation: 7352

For simultaneous jobs like this you should use a threading mechanism. Here you can use backgroundworkers.

var asyncServiceRequester = new BackgroundWorker();
asyncServiceRequester.DoWork += asyncServiceRequester_DoWork;

foreach(var request in req)
{
    asyncServiceRequester.RunWorkerAsync(req);
}

this is the code snippet that you should use instead of:

serviceClient.ProfileRequest(req[0]);
serviceClient.ProfileRequest(req[1]);
serviceClient.ProfileRequest(req[2]);
serviceClient.ProfileRequest(req[3]);

and here is the DoWork method of the backgroundworker:

static void asyncServiceRequester_DoWork(object sender, DoWorkEventArgs e)
{
    var request = e.Argument as ProfileRequest;//whatever your request type is 
    serviceClient.ProfileRequest(request);
}

UPDATE:

I am assuming that your user inputs are in a list for dynamic purposes, since you didn't gave any specific information about it. In this case you'll traverse through the list of inputs, and extract the values into your object array. Here is how you would first fill the list of inputs, then how you'll assign those values to your object array.

List<TextBox> inputs = new List<TextBox>();
inputs.Add(input1);
inputs.Add(input2);
...
inputs.Add(inputN);

If your inputs are static than this is how you do it, else;

List<TextBox> inputs = new List<TextBox>();
for(int i = 0; i < someNumber; i++)
{
    inputs.Add(new TextBox(){
                   Text = "some text",
                   Location = new Point(10, i * 75),
                   Height = 60,
                   Width = 150,
                   Font = new Font(ActiveControl.Font.FontFamily, 10, FontStyle.Regular)
               });
} 

Rest is pretty straightforward;

var index = 0;
foreach(var tb in inputs)
{
    var newReq = new ProfileRequest.Request(){
                  Record = new ProfileRequest.RequestRecord(){
                               Id = tb.Text
                           }
              } 
    req[index++] = req; 
    // I would suggest you to use generic lists here too since they are simpler and more flexible.
    // req.Add(newReq); 
}

Gathering the input from a text file or xml should be the topic of another question, but you can do them in the same manner. Just read the file, or xml then extract the values in dynamic manner.

Upvotes: 1

siddharth
siddharth

Reputation: 688

You can create threads to process requests in parallel -

for (int i = 0; i < req.Length; i++)
{
    new System.Threading.Thread(() => serviceClient.ProfileRequest(req[i])).Start();
}

Upvotes: 0

Related Questions