Skip to main content
 首页 » 编程设计

c#之如何知道 .net WebMethod 何时返回其值

2024年01月11日33Free-Thinker

我是 c# Web 服务的新手。我有一个将返回大量数据的 WebMethod。因此应该可以查看传输的状态。我怎么知道 WebMethod 是否以及何时返回了它的值?我可以绑定(bind)到事件或其他东西吗?

有问题的方法如下所示:

[WebMethod] 
public List<TEST.Models.ArticleModel> GetArticles(string terminalSerial) 
{ 
    Communication comm = new Communication { 
        StartDate = DateTime.Now, 
        Status = "Started" 
    }; 
    var terminal = (from t in context.Terminals 
                    where t.SerialNumber == terminalSerial 
                    select t).FirstOrDefault(); 
    var articles = from a in context.Articles 
                   where a.CountryID == terminal.Customer.CountryID 
                      && a.LastEdit > terminal.LastSync 
                   select new TEST.Models.ArticleModel { 
                       ArticleID = a.ArticleID, 
                       ArticleGroupID = a.ArticleGroupID, 
                       ArticleGroupName = a.ArticleGroup.Name, 
                       CountryName = a.Country.Name, 
                       Description = a.Description, 
                       EAN = a.EAN, 
                       SAPID = a.SAPID 
                   }; 
    terminal.LastSync = DateTime.Now; 
    comm.TerminalID = terminal.TerminalID; 
    context.SubmitChanges(); 
    return articles.ToList(); 
} 

请您参考如下方法:

服务器端:
使用日志记录或将列表写入 nrmal 文件,如下所示:

[WebMethod] 
public List<TEST.Models.ArticleModel> GetArticles(string terminalSerial) 
{ 
 
//..... your code here 
 
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt"); 
List<TEST.Models.ArticleModel> theList = articles.ToList(); 
file.WriteLine("you have "+ theList.Count ); 
for(ArticleModel articleModel : theList) 
   file.WriteLine(articleModel.ToString()); 
 
file.Close(); 
} 


在客户端也一样,调用 WebService 并将结果写入文件。