簡體   English   中英

WebService如何不等到任務完成

[英]WebService how to not wait until task is completed

我在WebService中具有ac#函數,以保存正在修改或添加的記錄。

public bool UpdateEmployeeInfo(DataSet ds, int userId) {

    bool returnVal = true;
    Guid employeeUid = Guid.Empty;


    SqlConnection conn = new SqlConnection(InfoTacto.Framework.WebServices.Common.GetConnectionString());
    conn.Open();
    SqlTransaction trans = conn.BeginTransaction(IsolationLevel.ReadUncommitted, "UpdateEmployeeInfo");

    try {
        if (ds != null && ds.Tables.Contains("employee") && ds.Tables["employee"].Rows.Count > 0) {
            DataRow mainRow = ds.Tables["employee"].Rows[0];
            employeeUid = new Guid(mainRow["employeeUid"].ToString());
        }

        // depending on the tables being modified, _sendMessage can be true or false
        _sendMessage = EmployeeUpdate.UpdateEmployeeMethods(ds, trans, userId);

        trans.Commit();

    } catch (Exception err) {

        trans.Rollback();

        returnVal = false;

    } finally {
        //if(conn.State == ConnectionState.Open) 
        conn.Close();
    }

    // send push message for real time sync

    if (_sendMessage) {
        // this service sometimes take between 1 to 5 seconds so I dont want to wait
        // until it has finished...
        Utility.MessageService sqs = new MessageService();
        sqs.SendMessage("employeeUid=" + employeeUid);
    }

    return returnVal;
}

在成功更新所有表之后,我檢查了Web服務是否需要發送消息(到其他系統),此操作有時需要毫秒或最多5秒鍾,但是我不希望我的桌面應用程序凍結以等待Web服務功能完成。

// this service sometimes take between 1 to 5 seconds so I dont want to wait
// until it has finished...
Utility.MessageService sqs = new MessageService();
sqs.SendMessage("employeeUid=" + employeeUid);

關於如何離開服務器以完成該SendMessage函數的任何線索,以便我的UpdateEmployeeInfo不會等待其完成,以便將returnVal值返回到客戶端應用程序。

謝謝

你有沒有嘗試過:

Task t = Task.Run(()=>
{
    Utility.MessageService sqs = new MessageService();
    sqs.SendMessage("employeeUid=" + employeeUid);
});

無論是否在另一個線程上進行發送,您都應考慮不直接將消息發送給另一個系統。 而是使用隊列解耦系統。 將消息推送到隊列上並退出,然后從隊列中讀取另一個系統。

有很多隊列基礎結構要考慮,包括MSMQ,Azure存儲隊列,Azure服務總線隊列,RabbitMQ等。

如果使用最新的WCF svcutil(版本4.5.1)為Utility.MessageService生成客戶端,則它實際上將為您生成方法調用的異步版本。 然后,您可以調用sqs.SendMessageAsync() ,該消息在發送消息后將不會等待。

這是一個示例服務接口:

[ServiceContract]
public interface IEmailService
{
    [OperationContract]
    void SendEmail(EmailDTO email);
}

使用4.5.1版本的svc util,這是從生成的客戶端中摘錄的內容。 注意,有一個Async版本的SendEmail()SendEmailAsync()

public partial class EmailServiceClient : System.ServiceModel.ClientBase<IEmailService>, IEmailService
{

    . . .<SNIP> . . .

    public void SendEmail(EmailDTO email)
    {
        base.Channel.SendEmail(email);
    }

    public System.Threading.Tasks.Task SendEmailAsync(EmailDTO email)
    {
        return base.Channel.SendEmailAsync(email);
    }
}

對我來說,svcutil的最新版本在C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v8.1A\\bin\\NETFX 4.5.1 Tools\\svcutil 此版本生成了我的服務方法的隨附Async版本。 同樣在我的計算機上的svcutil早期版本未生成Async versions

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM