8 javascript c# asp.net iis asp.net-web-api
我开发了一个Web应用程序.客户端 - Javascript/Typescript + AngularJS Server端 - c# - asp.net web api 2 IIS Express作为主机.
我注意到一个奇怪的问题,即POST requets不止一次到达服务器.例如,一个请求被多次发送的场景,每个特定文件夹电子邮件的请求被发送3次.我还在"网络"选项卡中看到这些请求已发送三次.
public class mailsService extends ImailsService{
static instance : ImailsService;
// Dictionary of folder -> emails
public Folders;
public mailsService(){
var scope = this;
myInterval = setInterval(function(){
// Making this request until getting a successful response - waiting for server to go up
scope.webService.get("url to get my own place").success(data => {
scope.myLocation = data;
scope.isLocationInit = true;
getAllMails();
window.cleanInterval(myInterval);
})
}, 3000);
}
// This function happen on it's own when running the client code.
// The service is a singleton therefore this code runs only once.
public getAllMails() {
if (!this.isLocationInit)
return;
var scope = this;
scope.webService.get("url to get all folders").then(reponse => {
var folders = response.data;
for (var i = 1; i <= folders.length; i ++){
return scope.get("url to get the mails inside specific folder").then(initMails.bind(null, i));
}});
}
public initMails (folderId : number, response){
mailsService.instance.Folders[folderId].push(response.data);
}
}
public class webService extends IwebService {
public get(url : string){
return this.$http.get(url);
}
public post(url : string, data : any){
return this.$http.post(url, data);
}
}
// Server side:
// Cross-Origin requests
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class MailsController : ApiController
{
private IMailsService _mailsService;
public MailsController(IMailsService service)
{
this._mailsService = service;
}
public IHttpActionResult GetAllFolders()
{
var result = _mailsService.GetAllFolders();
if (result == null)
return BadRequest("...");
return Ok(result);
}
public IHttpActionResult GetFolderEmails(int folderId)
{
var result = _mailsService.GetFolderEmails(folderId);
if (result == null)
return BadRequest("...");
return Ok(result);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器拟合方法命中两次甚至更多,尽管从cliend侧代码只调用一次.
这是可以配置的吗?IIS奇怪的事情?我真的不知道从哪里开始寻找.
在 中mailsService,您设置了一个间隔计时器,每 3 秒调用一次 Web 服务。是否有可能,当响应返回并且整个处理完成时,计时器已滴答一两次并再次触发该过程?您可以通过将间隔更改为更短和更长的值来测试这一点,看看这是否会影响对服务器的调用次数。
降低风险的一种方法是window.clearInterval在成功回调函数的一开始调用,然后调用getAllMails:
scope.webService.get("url to get my own place").success(data => {
window.clearInterval(myInterval);
scope.myLocation = data;
scope.isLocationInit = true;
getAllMails();
}
Run Code Online (Sandbox Code Playgroud)
另一种解决方案是避免使用setInterval. 您可以执行scope.webService.get并等待响应。如果出现错误,您可以在延迟后再次调用该服务,使用setTimeout:
public mailsService() {
var scope = this;
(function callWebService () {
scope.webService.get("url to get my own place")
.success(data => {
scope.myLocation = data;
scope.isLocationInit = true;
getAllMails();
})
.error(data => {
setTimeout(callWebService, 3000);
});
})();
}
Run Code Online (Sandbox Code Playgroud)
注意:我对Typescript不熟悉,所以可能有更好的方法来实现它。
| 归档时间: |
|
| 查看次数: |
1439 次 |
| 最近记录: |