使用C#处理多个Web请求的最佳方法是哪种? [英] Which is the best approach to handle multiple web requests by using C#?

查看:150
本文介绍了使用C#处理多个Web请求的最佳方法是哪种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用C#创建了一个Web服务(WCF).
其中,用户在URL中指定输入参数,该输入参数输入参数存储在(temp.inc)文件中,并根据新请求进行覆盖.然后根据给定的输入生成XML输出.但是,当多个请求试图同时访问Web服务时,它将产生冲突,只有第一个请求获得XML输出,而其余所有请求都将产生错误.
有什么方法可以处理多个请求,因此一次只能有一个请求可以访问Web服务,其余的请求将等待直到第一个请求得到执行.

我想知道使用C#做到这一点的最佳方法是什么.

我尝试过的事情:

我使用多线程进行了尝试,但是由于对C#的了解不足,因此在同一问题上我还没有得到任何解决方案.

I have created a Web service(WCF) using C#.
In which, User specifies input parameters in URL, that input parameters stores into (temp.inc) file and it overrides as per the new requests. then XML output generates as per the input given. But when Multiple requests are trying to access web service at a time then it gets conflicts and only first request gets XML output and remaining all gets an error.
Is there any way to handle multiple requests, so only one request can access web service at a time and remaining requests will wait until the first requests get executes.

I wanted to know what is the best approach to do this using C#.

What I have tried:

I tried it by using multi-threading, but because of insufficient knowledge on C# I didn''t get any solution yet on the same problem.

推荐答案

更改您的网站接受GUID作为参数的服务方法,并在客户端为每个请求生成一个新的guid.然后,在保存xml数据时,将GUID用作文件名的一部分.

届时,所有请求都将为该请求生成一个新文件.之后,您可以自行决定用户之间如何解析文件.

编辑================

如果可能的话,最好将结果与GUID一起放入数据库中,以便客户端可以通过第二种Web服务方法进行检索.这样,您就不会有文件争用和命名的麻烦.
Change your web service method to accept a GUID as a parameter, and generate a new guid at the client for each request. Then, use the GUID as part of the filename when you''re saving your xml data.

At that point, all requests will generate a new file just for that request. After that, you''re kind of on your own as to how the file is resolved between users.

EDIT ================

If possible, it would probably be better to put the result into a database along with the GUID so it can be retrieved by the client via a 2nd web service method. That way, you wouldn''t have the file contention and naming difficulties.


您可以使用lock关键字并使用私有的静态对象变量,然后在lock语句中可以处理文件.
锁定声明 [
You can use lock keyword and use a private static object variable, then inside the lock statement you can process your files.
lock Statement[^]


这篇关于使用C#处理多个Web请求的最佳方法是哪种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆