Using Cache in WCF data access improves the Chinese display speed of Winform fields, wcfwinform
When we develop a Winform program based on the WCF access method, we usually need to parse the fields displayed on the interface in Chinese. If it is hard-coded for Chinese display, there is no performance problem in addition to inconvenient adjustment and code bloated, but it is not recommended to do so. Generally, we put the Chinese Contrast information into the business class for unified parsing, but this will lead to a certain response time for each operation in the parsing of a WCF access request. If you use the cache to store the comparison table of Chinese fields, you do not need to request data access from WCF each time, reducing the consumption of response time and improving the user experience.
1. Use hard-coded Chinese Cultural parsing operations
In the hard-coding mode, operations on cultural fields are performed locally. Generally, the response is faster, as shown in the following code.
Public void BindData () {# region add alias parsing this. winGridViewPager1.DisplayColumns = "ID, User_ID, LoginName, FullName, Note, IPAddress, MacAddress, SystemType_ID, LastUpdated"; this. winGridViewPager1.AddColumnAlias ("ID", "No."); this. winGridViewPager1.AddColumnAlias ("User_ID", "Logon user ID"); this. winGridViewPager1.AddColumnAlias ("LoginName", "Login Name"); this. winGridViewPager1.AddColumnAlias ("FullName", "Real Name"); this. winGridViewPager1.AddColumnAlias ("Note", "log description"); this. winGridViewPager1.AddColumnAlias ("IPAddress", "ip address"); this. winGridViewPager1.AddColumnAlias ("MacAddress", "Mac address"); this. winGridViewPager1.AddColumnAlias ("LastUpdated", "record date"); this. winGridViewPager1.AddColumnAlias ("SystemType_ID", "system type"); # endregion string where = GetConditionSql (); PagerInfo pagerInfo = this. winGridViewPager1.PagerInfo; List <LoginLogInfo> list = CallerFactory <ILoginLogService>. instance. findWithPager (where, ref pagerInfo); this. winGridViewPager1.DataSource = new WHC. pager. winControl. sortableBindingList <LoginLogInfo> (list );}
This method is not very flexible. If there are many fields, there will be a lot of such code in the interface, and if there are multiple such resolutions, it is difficult to control the consistency of resolution field names.
2. Unified parsing of Chinese Culture
To overcome the disadvantages of the first solution, we can move the operations referenced by the Chinese culture to the underlying DAL for implementation. For the high-level interface, we only need to call it for resolution (method GetColumnNameAlias.
/// <Summary> /// bind list data /// </summary> private void BindData () {this. winGridViewPager1.DisplayColumns = "HandNo, CardNo, CardStatus, CardGrade, Name, Sex, Telephone, Mobile, OpenDate, ValidateDate, Discount, Balance, MaxCount, Creator, CreateTime"; this. winGridViewPager1.ColumnNameAlias = CallerFactory <IMemberService>. instance. getColumnNameAlias (); // field Column Display name escape string where = GetConditionSql (); List <MemberInfo> list = CallerFactory <IMemberService>. instance. findWithPager (where, this. winGridViewPager1.PagerInfo); this. winGridViewPager1.DataSource = new WHC. pager. winControl. sortableBindingList <MemberInfo> (list); this. winGridViewPager1.PrintTitle = "member information report ";}
After this process, the consistency of resolution is improved, and the code is much simpler, basically achieving the expected results. However, the only problem is that, if it is the data access method of WCF, each access will take a certain amount of processing time.
If we use the cache and obtain it from the local machine for the second time, the speed will be much higher. Especially when there are many reference objects for table fields, the performance will be improved significantly.
3. cache operations
Because. NET provides a MemoryCache object for cache processing, we can use it to implement it well. For convenience, we can encapsulate it for use.
First, we want to encapsulate and provide a general processing function for the culture of the field, and pass in the corresponding parameters. Therefore, a helper class is encapsulated first.
/// <Summary> /// provides cache processing for some common operations /// </summary> public class CacheDataUtil <T> where T: baseEntity {// <summary> // obtain the alias of the specified object // </summary> // <typeparam name = "T"> entity class information </typeparam> /// <param name = "service"> interface service object </param> /// <returns> </returns> public static Dictionary <string, string> GetColumnNameAlias (IBaseService <T> service) {System. reflection. methodBase method = System. reflection. methodBase. getCurrentMethod (); string key = string. format ("{0}-{1}-{2}", method. declaringType. fullName, method. name, typeof (T ). name); return MemoryCacheHelper. getCacheItem <Dictionary <string, string> (key, delegate () {return service. getColumnNameAlias () ;}, new TimeSpan (24, 0, 0); // 24 hours, that is, 1 day after expiration }}
Then, in the main interface, we can bind the paging control processing code as follows.
/// <Summary> /// bind list data /// </summary> private void BindData () {// entity this. winGridViewPager1.DisplayColumns = displayColumns; // this. winGridViewPager1.ColumnNameAlias = CallerFactory <ICustomerService>. instance. getColumnNameAlias (); // field Column Display name escape // use the alias of the cache storage table, which can effectively improve the secondary display speed. this. winGridViewPager1.ColumnNameAlias = CacheDataUtil <CustomerInfo>. getColumnNameAlias (CallerFactory <ICustomerService>. instance); // field Column Display name escape string where = GetConditionSql (); PagerInfo pagerInfo = this. winGridViewPager1.PagerInfo; List <CustomerInfo> list = CallerFactory <ICustomerService>. instance. findWithPager (where, ref pagerInfo); this. winGridViewPager1.DataSource = new WHC. pager. winControl. sortableBindingList <CustomerInfo> (list); this. winGridViewPager1.PrintTitle = "Customer Information List ";}
C # How can I solve the problem of slow use of Winform as a data source using WCF for the first time? The proxy has been disabled. Thank you!
Hosting Service are you using IIS or self-built Service?
If it is IIS, it may be suspected that the IIS application is not started, and w3wp needs to be loaded during the first access, resulting in slowness (of course, there may be other reasons ), if it is a Service, you need to check whether there are any cache or other settings in your server code. At least when I use the Service as a hosting Service, it is not slow and responds immediately.
Mvc access to wcf
I doubt how you add it. Didn't you directly reference the wcf dll?
The correct way to add a web service reference is to add a web service reference. Do not add a web reference. There is a difference between the two. In this case, a dialog box is displayed asking you to enter the service address and then query the service automatically, when the service introduces a space, it is better to name it a meaningful name. In this way, a remote wcf is referenced as a space, you can understand that this wcf is actually a local space, but it executes remote data.
There is a precondition that your wcf must be started and provide services normally. If you are not sure, you can enter the reference address, for example, localhost: 8733/sz/storehouseMockService/, directly in the address bar of your browser. If you can see the normal service, you can renew it, if not, start the wcf Service and perform wsbinding and basichttpbinding.
If you have not started the wcf Service, you cannot reference it.