Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 14-Oct-16 4:09am
v3
Comments
ZurdoDev 14-Oct-16 6:57am    
You should send the xml output back as a response to the request instead of putting it into a file.
Member 12793392 14-Oct-16 7:06am    
@RyanDev: No I can't do this because, In my web service 2 temporary files 1. temp.inc and 2. tempdata.xml. I am generating XML file of Doors module data and that file generates as per the given input in URL. Input parameters stores in temp.inc file and then tempdata.xml file is overrides and display on browser.

Problems occurs when multiple users try to access web service at a time, then the temp.inc file is conflicted and gets error.
Richard Deeming 14-Oct-16 10:41am    
Change your code so that you pass the parameters directly to the method that generates the data, and the method returns the data directly, rather than relying on temporary files.

So, instead of:
void GenerateData()
{
    YourSettingsClass theSettings = ReadSomeSettingsFromAFile("temp.inc");
    YourDataClass theData = GenerateTheData(theSettings);
    SaveTheDataToAFile(theData, "tempdata.xml");
}

you would have:
YourDataClass GenerateData(YourSettingsClass theSettings)
{
    YourDataClass theData = GenerateTheData(theSettings);
    return theData;
}

Not only will you avoid the problem you're facing, you'll also dramatically increase the performance of your code.
F-ES Sitecore 14-Oct-16 7:49am    
The solution isn't to throttle WCF so that requests are handled one at a time, the solution is to not use the same temp file for all requests. See solution 1 for a possible workaround, but that is the avenue you want to pursue.

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.
 
Share this answer
 
v2
Comments
Member 12793392 14-Oct-16 7:59am    
Thank you for your solution. I will try to implement it.
#realJSOP 14-Oct-16 9:20am    
However, what RyanDev suggested is the most correct approach. You should be returning the result to the client for processing. That would resolve the issue straight away.
Member 12793392 15-Oct-16 2:24am    
Actually I can't use any external database, so It is not possible to put results into database. Instead of using database, I will try to implement approaches suggested by @RyanDev and @Richard Deeming.
You can use lock keyword and use a private static object variable, then inside the lock statement you can process your files.
lock Statement[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900