什么是使用" AsyncPattern"的&QUOT财产; OperationContractAttribute" + WCF? [英] What is the use of "AsyncPattern" property of "OperationContractAttribute" + wcf?

查看:166
本文介绍了什么是使用" AsyncPattern"的&QUOT财产; OperationContractAttribute" + WCF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,对于使用AJAX的WCF服务从数据库获取记录,并没有使用 OperationContractAttribute AsyncPattern 属性在客户端显示出来C> ...

  • 在什么时候应该考虑 AsyncPattern 属性?

我的OperationContract的方法示例,

  [OperationContract的]
 公共字符串GetDesignationData()
    {
        数据集DT = GetDesignationViewData();
        返回GetJSONString(dt.Tables [0]);
    }
    公共字符串GetJSONString(数据表DT)
    {
        字符串[] StrDc =新的字符串[Dt.Columns.Count]
        字符串HeadStr =的String.Empty;
        的for(int i = 0; I< D​​t.Columns.Count;我++)
        {
            StrDc [i] = Dt.Columns [I] .Caption;
            HeadStr + =\+ StrDc [I] +\:\+ StrDc [I] + i.ToString()+¾+\,;
        }
        HeadStr = HeadStr.Substring(0,HeadStr.Length  -  1);
        StringBuilder的锑=新的StringBuilder();

        Sb.Append({\+ Dt.TableName +\:[);
        的for(int i = 0; I< D​​t.Rows.Count;我++)
        {
            字符串TempStr = HeadStr;
            Sb.Append({);
            对于(INT J = 0; J< D​​t.Columns.Count; J ++)
            {
                如果(Dt.Rows [i] [j]中的ToString()。包含()==真)
                {
                    Dt.Rows [I] [J] = Dt.Rows [i] [j]中的ToString()更换(',)。
                }
                TempStr = TempStr.Replace(Dt.Columns [J] + j.ToString()+¾,Dt.Rows [I] [J]的ToString());
            }
            Sb.Append(TempStr +},);
        }
        SB =新的StringBuilder(Sb.ToString()子串(0,Sb.ToString()长度 -  1)。);
        Sb.Append(]});
        返回Sb.ToString();
    }
    公共数据集GetDesignationViewData()
    {
        尝试
        {
            字符串的connectionString = System.Configuration.ConfigurationManager.ConnectionStrings [的connectionString]的ConnectionString。
            返回SqlHelper.ExecuteDataset(的connectionString,CommandType.StoredProcedure,DataTemplate.spDesignation_View);
        }
        赶上(例外错误)
        {
            扔走错了路。
        }
    }
 

解决方案

AsyncPattern有一些使用说明 - 这是主要的服务器性能优化,使您可以腾出工作池请求的线程上阻塞操作。例如,当像数据库访问长时间运行的阻塞操作发生时,如果你使用的服务器AsyncPattern上一个异步DB API,工作线程可以返回到池中,服务等要求。原来的请求被唤醒后对数据库的访问完成时,另一个工作线程,和其他工作完成(该服务的客户端只是耐心waits-这一切都是透明的,除非你使用的是AsyncPattern感知客户端和有约束力的)。这可以让你的服务来处理更多的请求,如果用心去做。要充分利用,你需要使用的API,具有本机异步实现在服务器上。唯一一个我看到这可能是一个候选者是DB调用这是发生在你的SQLHelper.ExecuteDataset于方法,你不得不读了底层API上,以确保真正的异步选项的BeginXXX可供选择(presence / EndXXX方法并不一定意味着它是一个真正的异步implement执行)。该System.SqlClient东西是真正的异步。

注意的一点是:你要处理大量的请求,使这个worthwhile-有一个显著的成本code的复杂性和可读性拆分开来的这种方式。您还需要了解多线程编程非常良好大约有锁定,错误处理等众多缺陷,这是很好的SO职位的范围之内。

祝你好运!

Thus for used ajax enabled wcf services to get records from DB and display it in client without using AsyncPattern property of OperationContractAttribute....

  • When should i consider AsyncPattern property?

Sample of my operationcontract methods,

[OperationContract]
 public string GetDesignationData()
    {
        DataSet dt = GetDesignationViewData();
        return GetJSONString(dt.Tables[0]);
    }
    public string GetJSONString(DataTable Dt)
    {
        string[] StrDc = new string[Dt.Columns.Count];
        string HeadStr = string.Empty;
        for (int i = 0; i < Dt.Columns.Count; i++)
        {
            StrDc[i] = Dt.Columns[i].Caption;
            HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";
        }
        HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);
        StringBuilder Sb = new StringBuilder();

        Sb.Append("{\"" + Dt.TableName + "\" : [");
        for (int i = 0; i < Dt.Rows.Count; i++)
        {
            string TempStr = HeadStr;
            Sb.Append("{");
            for (int j = 0; j < Dt.Columns.Count; j++)
            {
                if (Dt.Rows[i][j].ToString().Contains("'") == true)
                {
                    Dt.Rows[i][j] = Dt.Rows[i][j].ToString().Replace("'", "");
                }
                TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "¾", Dt.Rows[i][j].ToString());
            }
            Sb.Append(TempStr + "},");
        }
        Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
        Sb.Append("]}");
        return Sb.ToString();
    }
    public DataSet GetDesignationViewData()
    {
        try
        {
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
            return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, DataTemplate.spDesignation_View);
        }
        catch (Exception err)
        {
            throw err;
        }
    }

解决方案

AsyncPattern has a few uses- it's mainly a server performance optimization that allows you to free up worker pool request threads on blocking operations. For example, when a long-running blocking operation like DB access occurs, if you're using an async DB API on the server with AsyncPattern, the worker thread can return to the pool and service other requests. The original request is "awakened" later on another worker thread when the DB access completes, and the rest of the work is done (the service client just patiently waits- this is all transparent to it unless you're using an AsyncPattern-aware client and binding). This CAN allow your service to process more requests, if done carefully. To take advantage, you need to be using APIs on the server that have native async implementations. The only one I see that might be a candidate is the DB call that's happening in your SQLHelper.ExecuteDataset method- you'd have to read up on the underlying API to make sure a TRUE asynchronous option is available (presence of BeginXXX/EndXXX methods doesn't necessarily mean it's a TRUE async impl). The System.SqlClient stuff is truly async.

A word of caution: you have to be processing a lot of requests to make this worthwhile- there's a significant cost to code complexity and readability to split things up this way. You also need to understand multi-threaded programming very well- there are numerous pitfalls around locking, error handling, etc, that are well outside the scope of a SO post.

Good luck!

这篇关于什么是使用&QUOT; AsyncPattern&QUOT;的&QUOT财产; OperationContractAttribute&QUOT; + WCF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆