如何在不等待C#

时间:2018-05-16 04:59:47

标签: c#

我是Web API服务的新手,我想制作两个服务并由我的桌面应用程序触发而不等待响应。

我的网络方法如下 第一

    [HttpGet]
    [AcceptVerbs("Post")]
    async public void SendSOA()
    {
        try
        {
            FunctionName = "SendSOA";
            string Subject = string.Empty;
            StringBuilder strBody = new StringBuilder();
            StringBuilder sbEmailTemp = new StringBuilder();
            string EmailBody = string.Empty;
            DateTime AsOfDate = DateTime.Today;
            TenantInfo tenantInfo = new TenantInfo();
            byte[] CompanyLogo = null;

            List<StatementOfAcc> lstSOA = new List<StatementOfAcc>();
            List<tblSMTPSetting> lstSMTPSettings = 
            CashVitae.SMTPSetting.GetTenantSMTPsetting();
            lstSOA = GetStatementOfAcc(lstSMTPSettings);

            foreach (StatementOfAcc SOA in lstSOA)
            {
                long CompanyId = cUtil.ToLong(SOA.CompanyId);
                tblSMTPSetting SMTP = lstSMTPSettings.Where(x => x.CompanyId == CompanyId).FirstOrDefault();
                if (SMTP != null)
                {
                    long TenantId = SMTP.TenantId;
                    string Host = SMTP.Host;
                    int Port = SMTP.Port;
                    string UserName = SMTP.UN;
                    string Password = cGlobalUI.decrypt(SMTP.Pwd);
                    SendSOAToCustomer(TenantId, Host, UserName, Password, Port, SOA);
                }
            }
        }
        catch (Exception ex)
        {
            SendFailedEmail(FunctionName, "Exception: " + Convert.ToString(ex.Message) + "\n" + "Inner Exception: " + Convert.ToString(ex.InnerException), oCustomerId);
        }
    }

和第二种方法是

    [HttpGet]
    [AcceptVerbs("Post")]
    async public void SendNotification()
    {
        List<Alert> lstAlert = GetAllNotifications();

        FCM obj = new CP_UI.FCM(cGlobalUI.FCMFilePath, cGlobalUI.FCMProjectId);
        foreach (Alert item in lstAlert)
        {
            try
            {
                obj.SendMessageToDevice(item.Title, item.MessageBody, item.DeviceToken);
            }
            catch (Exception ex)
            {

            }
        }
    }

我的桌面应用程序代码是

    public void CallAPI()
    {
        //1st API URL
        string APIUrl = ConfigurationManager.AppSettings["APIUrl"].ToString();
        //2nd API URL
        string Url = ConfigurationManager.AppSettings["Url"].ToString();

        string url1 = APIUrl + "/SendNotification";
        string url2 = Url + "/SendSOA";


        ASCIIEncoding encoding = new ASCIIEncoding();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
        request.Method = "Post";
        request.ContentLength = 0;
        request.ContentType = "application/json";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //Here I am getting timeout error so I want just trigger it and should not wait for response 

        ASCIIEncoding encoding1 = new ASCIIEncoding();
        HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url2);
        request.Method = "Post";
        request.ContentLength = 0;
        request.ContentType = "application/json";
        HttpWebResponse response1 = (HttpWebResponse)request.GetResponse();
    }

请帮助我在没有回应的情况下提供此服务。 我只是希望通过桌面应用程序触发这两种服务,然后在网络端代码将处理所有事情。

先谢谢。

2 个答案:

答案 0 :(得分:1)

WebMethods的定义看起来不正确。他们应该是

public async Task SendSOA()

public async Task SendNotification()

您的WebAPI方法也是发布的,您似乎没有向他们发布任何数据

如果您只是想解雇并忘记,那么您可以改为

Task<WebResponse> response = (HttpWebResponse)request.GetResponseAsync();

答案 1 :(得分:0)

你只需要打电话

request.GetResponseAsync();

取代

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

这将使您的调用异步和代码不会等待来自API的响应