“AsyncPattern”有什么用? “OperationContractAttribute”的属性+ wcf?

发布于 2024-08-23 22:44:57 字数 2038 浏览 7 评论 0原文

因此,对于使用ajax启用的wcf服务,从数据库获取记录并将其显示在客户端中,而不使用OperationContractAttributeAsyncPattern属性....

  • 我什么时候应该考虑AsyncPattern< /code> 属性?

我的操作合同方法示例,

[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;
        }
    }

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;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛上残留的泪 2024-08-30 22:44:57

AsyncPattern 有一些用途 - 它主要是服务器性能优化,允许您释放阻塞操作上的工作池请求线程。例如,当发生长时间运行的阻塞操作(例如数据库访问)时,如果您在服务器上使用 AsyncPattern 的异步 DB API,则工作线程可以返回到池并为其他请求提供服务。当数据库访问完成时,原始请求稍后在另一个工作线程上“唤醒”,其余工作完成(服务客户端只是耐心等待 - 这对它来说都是透明的,除非您使用 AsyncPattern 感知客户端和绑定)。如果小心操作,这可以让您的服务处理更多请求。要利用这一优势,您需要在具有本机异步实现的服务器上使用 API。我认为唯一可能的候选者是 SQLHelper.ExecuteDataset 方法中发生的数据库调用 - 您必须阅读底层 API 以确保 TRUE 异步选项可用(存在 BeginXXX/EndXXX 方法)并不一定意味着它是一个真正的异步实现)。 System.SqlClient 确实是异步的。

需要注意的是:您必须处理大量请求才能使这一切变得有价值 - 以这种方式拆分事物会导致代码复杂性和可读性的巨大成本。您还需要很好地理解多线程编程——锁定、错误处理等方面存在许多陷阱,这些陷阱远远超出了 SO 帖子的范围。

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文