如何在WCF应用程序服务中添加计时器及其事件 [英] How to add a timer and it's events in a WCF Application Service
问题描述
如何在我的WCF应用服务中添加计时器及其事件?
我的WCF从客户端接收文件并将它们保存在SQL Server数据库中。
我想将收到的文件发送到我无法控制的其他服务。但我不想让客户端在执行此操作时等待 [OperationContract] 的返回。我想事后发送。
这是我原始来源的代码示例
How do I add a timer and it's events in my WCF Application Service ?
My WCF receives files from a client and I save them in a SQL Server Database.
I would like to send the received files to an other service that I do not control. But I don't want to make the client wait for a return of the [OperationContract] while I do this. I want to send it afterwards.
Here's a code example of my original source
public ServiceFeedback UploadFileToServer(byte[] file)
{
FileModel dbFile = database.FileModel.Add(new FileModel(file)); // Adding to database...
SendToOtherService(dbFile); // Sending to other service, I want to remove this from here and put it in an other function. It makes the client wait for the return
if(NoErrors = true)
return ServiceFeedback.OK;
else
return ServiceFeedback.ERROR;
}
推荐答案
感谢评论,我发现我不需要计时器而是需要异步SendToOtherService(dbFile),因此SendToOtherService成为SendToOtherServiceAsync。非常感谢您的评论。
Thanks to the comments, I found out that I didn't need a timer but instead an async on the SendToOtherService(dbFile), so SendToOtherService became SendToOtherServiceAsync. Thank you very much for your comments.
public async void SendToOtherServiceAsync(FileModel dbFile)
{
// Preparing file for sending...
string response = await Send(formatedDbFile);
}
Send方法包含我通过https发送的一些XML
The Send method contains the some XML that I send via https
public Task<string> Send(FormatedFile file)
{
return Task<string>.Factory.StartNew(() =>
{
// Sends XML via https...
return response;
});
}
如果您有更好的解决方案,我很乐意看到它们:)
If you have better solutions I would be happy to see them :)
这篇关于如何在WCF应用程序服务中添加计时器及其事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!