如何在WCF /客户端中一次传递多个请求

时间:2013-08-07 08:34:01

标签: c# asynchronous request

使用以下代码,我可以逐个处理请求。这是一个异步过程,所以我不需要得到响应。我只是传递了请求。

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]);
    } 
}

我想要做的是一次一个地处理所有请求。

请允许任何人提供要求的代码???

2 个答案:

答案 0 :(得分:1)

对于这样的同时作业,您应该使用线程机制。在这里你可以使用backgroundworkers。

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

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

这是您应该使用的代码段,而不是:

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

这是后台工作者的DoWork方法:

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

<强>更新

我假设您的用户输入位于动态目的列表中,因为您没有提供任何有关它的具体信息。在这种情况下,您将遍历输入列表,并将值提取到对象数组中。以下是如何首先填充输入列表,然后如何将这些值分配给对象数组。

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

如果你的输入是静态的,那你就是这样做的,否则;

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)
               });
} 

休息非常简单;

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); 
}

从文本文件或xml收集输入应该是另一个问题的主题,但您可以以相同的方式执行它们。只需读取文件,或xml然后以动态方式提取值。

答案 1 :(得分:0)

您可以创建并行处理请求的线程 -

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