Reputation: 101
I am new on Web API services I want to make two services and trigger by my Desktop Application without waiting response.
My web Methods are below 1st
[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);
}
}
and 2nd Method is
[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)
{
}
}
}
and my desktop application code is
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();
}
please help me to make this service without response. I just want to trigger both services by my desktop application and then on web side code will take care for everything.
Thanks in Advance.
Upvotes: 1
Views: 17867
Reputation: 128
The definition of your WebMethods don't look right. They should be
public async Task SendSOA()
and
public async Task SendNotification()
Also your WebAPI methods are post and you do not seem to be posting any data to them
If you just want to fire and forget then you can change to
Task<WebResponse> response = (HttpWebResponse)request.GetResponseAsync();
Upvotes: 1
Reputation: 2927
You just simply need to call
request.GetResponseAsync();
in place of
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
This will make your call async and code will not wait for the response from the API
Upvotes: 3