Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 从通用处理程序(.ashx)进行异步调用_Asp.net_.net_Asynchronous_Delegates_Ashx - Fatal编程技术网

Asp.net 从通用处理程序(.ashx)进行异步调用

Asp.net 从通用处理程序(.ashx)进行异步调用,asp.net,.net,asynchronous,delegates,ashx,Asp.net,.net,Asynchronous,Delegates,Ashx,我在我的网站上有一个表单,它将json发布到异步处理程序,异步处理程序验证数据并返回resonse OK或error,我将根据处理程序给出的响应在客户端进行重定向 但是当响应正常时,我希望异步执行一些任务。但是异步调用在.ashx代码中不起作用。它总是同步的 你能给我一个建议吗 代码: public class ValidateHandler : IHttpHandler, IRequiresSessionState { public void ProcessReque

我在我的网站上有一个表单,它将json发布到异步处理程序,异步处理程序验证数据并返回resonse OK或error,我将根据处理程序给出的响应在客户端进行重定向

但是当响应正常时,我希望异步执行一些任务。但是异步调用在.ashx代码中不起作用。它总是同步的

你能给我一个建议吗

代码:

public class ValidateHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            NameValueCollection nvcForm = context.Request.Form;
            Dictionary<string, string> errorFieldsDict = new Dictionary<string, string>();


            foreach (string nameValueKey in nvcForm.AllKeys)
            {
                regExpToTest = string.Empty;
                switch (nameValueKey)
                {
                    case "firstName":
                      //validate
                        break;
                    case "lastName":
                       //validate
                        break;
                    case "email":
                        //validate
                        break;
                    case "phoneNumber":
                    //validate
                        break;
                    case "phoneCountryCode":
                        //validate
                        break;
                    case "country":
                     //validate
                        break;
                    case "addressLine1":
                        //validate
                        break;
                    case "addressLine2":
                       //validate
                        break;
                    case "city":
                        //validate
                        break;
                    case "postalCode":
                        //validate
                        break;
                    default:
                        //validate
                        break;
                }
                if (!string.IsNullOrEmpty(regExpToTest) && !Regex.IsMatch(nvcForm[nameValueKey], regExpToTest) && !string.IsNullOrEmpty(nvcForm[nameValueKey]))
                {
                    errorFieldsDict.Add(nameValueKey, "Please Enter Correct Value");
                    isSuccess = false;
                }
            }

            //Do your business logic here and finally

            if (isSuccess)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                try
                {
                    Dictionary<string, object> formValues = GetDictionaryForJson(nvcForm);
                    string previoiusUrl = GetRequestedURL(context);
                    string partner = string.Empty;
                    if (System.Web.HttpContext.Current.Session["yourpartner"] != null)
                        partner = System.Web.HttpContext.Current.Session["yourpartner"].ToString();
                    else if (System.Web.HttpContext.Current.Request.QueryString["utm_source"] != null)
                        partner = System.Web.HttpContext.Current.Request.QueryString["utm_source"];
                    else
                        partner = "company";
                    formValues.Add("partnerCode", partner);
                    string brochureType = string.Empty;
                    if (!string.IsNullOrEmpty(nvcForm["addressLine1"]) || !string.IsNullOrEmpty(nvcForm["addressLine2"]))
                        brochureType = "FBRO";
                    else
                        brochureType = "EBRO";
                    //Create a row in database
                    Item programItem = Sitecore.Context.Database.Items.GetItem(programRootpath + nvcForm["selectYourProgram"]); ;
                    AsyncMailSender caller = new AsyncMailSender(SendEmail);
                    IAsyncResult result = caller.BeginInvoke(programItem, nvcForm["email"], null, null);
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    Log.Error("Enquiry handler failure: " + ex.Message, ex);
                    response.response = "error";
                    response.message = ex.Message;
                    context.Response.ContentType = "application/json";
                    context.Response.Write(JsonConvert.SerializeObject(response));
                }
                if (isSuccess)
                {
                    response.response = "ok";
                    context.Response.ContentType = "application/json";
                    context.Response.Write(JsonConvert.SerializeObject(response));
                }

            }
            else
            {
                response.response = "errorFields";
                response.errorFields = errorFieldsDict;
                context.Response.ContentType = "application/json";
                string responseJson = JsonConvert.SerializeObject(response);
                context.Response.Write(JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.None));
            }

        }
        private string GetRequestedURL(HttpContext context)
        {
            string previousURL = string.Empty;
            try
            {
                previousURL = context.Request.ServerVariables["HTTP_REFERER"];
            }
            catch
            {
                previousURL = context.Request.Url.AbsolutePath;
            }
            return previousURL;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private void SendEmail(Item programItem, string toEmail)
        {

            if (programItem != null)
            {
               SendEmail()

            }
        }
        private Dictionary<string, object> GetDictionaryForJson(NameValueCollection formValues)
        {
            Dictionary<string, object> formDictionary = new Dictionary<string, object>();
            foreach (string key in formValues.AllKeys)
            {
                formDictionary.Add(key, formValues[key]);
            }

            return formDictionary;
        }

    }
    public delegate void AsyncMailSender(Item program, string toAddress);
public类ValidateHandler:IHttpHandler,iRequestSessionState
{
公共void ProcessRequest(HttpContext上下文)
{
NameValueCollection nvcForm=context.Request.Form;
Dictionary errorFieldsDict=新字典();
foreach(nvcForm.AllKeys中的字符串nameValueKey)
{
regExpToTest=string.Empty;
开关(nameValueKey)
{
案例“名字”:
//证实
打破
案例“lastName”:
//证实
打破
案例“电子邮件”:
//证实
打破
案例“电话号码”:
//证实
打破
案例“phoneCountryCode”:
//证实
打破
案例“国家”:
//证实
打破
案例“addressLine1”:
//证实
打破
案例“addressLine2”:
//证实
打破
案例“城市”:
//证实
打破
案例“postalCode”:
//证实
打破
违约:
//证实
打破
}
如果(!string.IsNullOrEmpty(regExpToTest)和&!Regex.IsMatch(nvcForm[nameValueKey],regExpToTest)和&!string.IsNullOrEmpty(nvcForm[nameValueKey]))
{
errorFieldsDict.Add(nameValueKey,“请输入正确的值”);
isSuccess=false;
}
}
//在这里和最后完成您的业务逻辑
如果(isSuccess)
{
JavaScriptSerializer serializer=新的JavaScriptSerializer();
尝试
{
Dictionary formValues=GetDictionaryForJson(nvcForm);
字符串previiusurl=GetRequestedURL(上下文);
string partner=string.Empty;
if(System.Web.HttpContext.Current.Session[“yourpartner”]!=null)
partner=System.Web.HttpContext.Current.Session[“yourpartner”].ToString();
else if(System.Web.HttpContext.Current.Request.QueryString[“utm_source”]!=null)
partner=System.Web.HttpContext.Current.Request.QueryString[“utm_source”];
其他的
合作伙伴=“公司”;
formValues.添加(“partnerCode”,partner);
string-brochureType=string.Empty;
如果(!string.IsNullOrEmpty(nvcForm[“addressLine1”])| |!string.IsNullOrEmpty(nvcForm[“addressLine2”]))
小册子Type=“FBRO”;
其他的
小册子Type=“EBRO”;
//在数据库中创建一行
Item programItem=Sitecore.Context.Database.Items.GetItem(programRootpath+nvcForm[“selectYourProgram”]);
AsyncMailSender调用者=新的AsyncMailSender(SendEmail);
IAsyncResult result=caller.BeginInvoke(programItem,nvcForm[“email”],null,null);
}
捕获(例外情况除外)
{
isSuccess=false;
日志错误(“查询处理程序故障:+ex.消息,ex”);
response.response=“error”;
response.message=ex.message;
context.Response.ContentType=“应用程序/json”;
Write(JsonConvert.SerializeObject(Response));
}
如果(isSuccess)
{
response.response=“确定”;
context.Response.ContentType=“应用程序/json”;
Write(JsonConvert.SerializeObject(Response));
}
}
其他的
{
response.response=“errorFields”;
response.errorFields=errorFields信息通信;
context.Response.ContentType=“应用程序/json”;
string responseJson=JsonConvert.SerializeObject(响应);
Write(JsonConvert.SerializeObject(Response,Newtonsoft.Json.Formatting.None));
}
}
私有字符串GetRequestedURL(HttpContext上下文)
{
string previousURL=string.Empty;
尝试
{
previousURL=context.Request.ServerVariables[“HTTP_REFERER”];
}
抓住
{
previousURL=context.Request.Url.AbsolutePath;
}
返回以前的URL;
}
公共布尔可重用
{
得到
{
返回false;
}
}
私有void sendmail(项目programItem,字符串toEmail)
{
if(programItem!=null)
{
发送电子邮件()
}
}
私有字典GetDictionaryForJson(NameValueCollection formValues)
{
词典用语
public class MyHandler : HttpTaskAsyncHandler {

    public override async Task ProcessRequestAsync(HttpContext context) {
       await WhateverAsync(context);
    }

}