C# 异步方法中同步web服务调用的缺点
我正在从事一个项目,该项目需要创建一个包含特定格式的特定数据的XML,将其发送给第三方服务,然后处理结果。这里第三方的功能是验证数据和XML格式,然后创建一个编码字符串(“戳记”),证明所有内容都符合要求。然后必须将该戳记添加到原始XML中并存储在数据库中 现在,第三方仍然有待选择,因此我必须在发布之前(到目前为止)以一种方式创建我的项目,它可以与两个不同的web服务一起工作,有机会添加另一个web服务,并在发布之后更改所选的web服务。虽然最终的结果是相同的(XML的“戳记”),但每个web服务的操作都不同,例如,一个服务只返回戳记字符串,而另一个服务返回包含戳记的XML,而挂起的服务返回包含XML的zip文件的字节数组(我不知道是谁告诉他们这是个好主意,但是,嗯,那是另一个故事) 考虑到这一点,我决定创建一个静态类来包装web服务调用(每个web服务一个方法)XML或带有错误代码和消息的XML,以防出错。处理数据的类只需创建所需的XML、调用其中一个方法并处理结果 到目前为止,它看起来像这样:C# 异步方法中同步web服务调用的缺点,c#,.net,asynchronous,C#,.net,Asynchronous,我正在从事一个项目,该项目需要创建一个包含特定格式的特定数据的XML,将其发送给第三方服务,然后处理结果。这里第三方的功能是验证数据和XML格式,然后创建一个编码字符串(“戳记”),证明所有内容都符合要求。然后必须将该戳记添加到原始XML中并存储在数据库中 现在,第三方仍然有待选择,因此我必须在发布之前(到目前为止)以一种方式创建我的项目,它可以与两个不同的web服务一起工作,有机会添加另一个web服务,并在发布之后更改所选的web服务。虽然最终的结果是相同的(XML的“戳记”),但每个web服
public class EDocument
{
//This class handles all the data that will be included in the XML
public void Stamp()
{
//Here the XML string is created, sent to the correspondig third-party web service, and processed back
string xmlString;
//code to create the XML
//...
//If needed, I only have to change this line to call the corresponding method
WebServiceCalls.MainWebServiceCall stamper = WebServiceCalls.FirstServiceCall;
stamper.BeginInvoke(xmlString, StampCallback, null);
}
public void StampCallback(IAsyncResult ar)
{
AsyncResult result = (AsyncResult)ar;
WebServiceCalls.MainWebServiceCall caller = (WebServiceCalls.MainWebServiceCall)result.AsyncDelegate;
string response = caller.EndInvoke(ar);
//Call another async method to save to database the stamp, create the XML file, e-mail and store it, and notify the results...
//or create the exception with the error details and raise event here to notify the error
}
}
Web服务调用
public static class WebServiceCalls
{
//Here I'll put the necessary web service calls. In the end there will be only one,
//but if on future the web service changes, a method with the same signature will be created here
//replacing the previous one
public delegate string MainWebServiceCall(string XmlData);
public static string FirstServiceCall(string XmlData)
{
FirstWebService firstWs = new FirstWebService();
string serviceResult = firstWs.Stamp(XmlData); //This returns only the stamp string
//Check the result, add the stamp to the original XML or create the error XML, and return...
return serviceResult;
}
public static string SecondServiceCall(string XmlData)
{
SecondWebService secondWs = new SecondWebService();
string serviceResult = secondWs.Stamp(XmlData); //This returns the XML with the stamp already added
//Check the result, create the error XML if something went wrong, and return...
return serviceResult;
}
public static string ThirdServiceCall(string XmlData)
{
ThirdWebService thirdWs = new ThirdWebService();
string serviceResultString;
byte[] serviceResult = thirdWs.Stamp(XmlData); //This (sigh) returns the byte array of a ZIP file...
//Unzip the file, check the result, create the corresponding XML and return...
return serviceResultString;
}
}
但我突然想到…尽管我将异步调用包装器方法,但web服务方法仍将同步调用
问题是:这样做的缺点是什么?我应该在每个调用方方法上异步调用web服务,处理回调,引发通知事件,在Edocument类上捕获它,处理结果并引发它的相应事件吗?考虑到未来可能发生的变化,这不会让事情变得过于复杂吗这个项目可能有什么好处
还是说这是一个错误的解决问题的方法
请记住,这是C#4.0,因此(遗憾的是)async await超出了范围。您根本没有使用async IO。
委托。BeginInvoke
使用线程池。在服务器应用程序中,这几乎在所有情况下都是完全有害的
使用同步代码或异步IO
您似乎认为异步调用内部同步的webservice(或者相反)可能有缺点。事实并非如此。在某一层异步只会影响该层