Go deep into Atlas series: Explore Application Services(2) -Support for custom server-side Profile Service-Alibaba Cloud Developer Community

In the previous article, we discussed the default NET AJAX using ASP. Profile Service. Generally speaking, it can already meet the needs of most applications. However, in addition, ASP.NET AJAX also provides a mechanism for us to customize Profile Service. To customize a Profile Service, you need to perform two steps: 1. Specify the Path of the ScriptManager in the Profile Service in ASP.NET AJAX client script, if Sys.Services.ProfileService. If the set_path method is used to specify an address that provides Profile Service, the default address will be used, which will make ASP.NET AJAX Profile Service use specific classes in the program set. In general, you do not need to manually call the Sys.Services.ProfileService.set_path method. You only need to specify it in the ScriptManager. As follows:
&lt; asp:ScriptManager ID = "ScriptManager1" runat ="server" ScriptMode ="Debug" &gt;      &lt; ProfileService Path ="CustomProfileService.asmx"   /&gt; </ asp:ScriptManager &gt;
after opening the page, you can find the following JavaScript code on the page:
Sys.Services.ProfileService.set_path (' / CustomProfileService.asmx');
"/" Appears because the page I tested is in the root directory. Therefore, Profile Service uses the specified Web Service instead of the default Web Service class. 2. Implement your own Web Service if you specify your own Web Service class, you must implement your own class. In fact, we need to implement three methods. This in for ASP.NET AJAX IN Profile Service Use the default Web Service class Microsoft.Web.Profile.ProfileService Is our Great reference. Therefore, we analyze these methods here, which is very helpful for our custom work. It may be noted that when we implement these methods, theoretically, the parameter types do not need to be completely the same as those in Microsoft.Web.Profile.ProfileService. ASP.NET AJAX can convert the obtained JSON string to the required type as much as possible according to the type of the parameter. However, in fact, it seems that the parameter selection of those methods in Microsoft.Web.Profile.ProfileService is very reasonable. In addition, the code Profile Service the client is not easy to modify (in fact, the client cannot be extended. In the most extreme case, isn't it that we implement a ProfileService ourselves?), To ensure that the returned JSON string can be correctly processed, the return values of these methods are generally unchanged. 1, GetAllPropertiesForCurrentUser method this method is used to obtain all the Profile information of the current user. It does not have input parameters. The JSON string returned is as follows:
{'ZipCode': **, 'Address. Cite':**, 'Address.State': **}
it represents the GroupName in the form of ProfileName. Profile Group. Each Profile in the Group needs to be listed separately, while "..." represents the JSON string corresponding to Profile value. In Microsoft.Web.Profile.ProfileService, the code of this method is as follows:
[WebMethod] public IDictionary &lt; string , object &gt; GetAllPropertiesForCurrentUser() { ProfileService.CheckProfileServicesEnabled(); return ProfileService.GetProfile(HttpContext.Current null ); }
2, GetPropertiesForCurrentUser method this method is used to obtain the Profile information specified by the current user. The input JSON string is as follows:
['ZipCode', 'Address.City', 'Address.State']
the returned JSON string is the same as the GetAllPropertiesForCurrentUser. In the class, the code of this method is as follows:
[WebMethod] public IDictionary &lt; string , object &gt; GetPropertiesForCurrentUser ( string [] properties) { ProfileService.CheckProfileServicesEnabled(); return ProfileService.GetProfile(HttpContext.Current, properties);}
as you can see, both GetAllPropertiesForCurrentUser and GetPropertiesForCurrentUser use the ProfileService.GetProfile static method to obtain the results. Let's take a closer look at the implementation of this method. As follows: this method is very easy to understand and write, and does not involve any serialization or deserialization operations. Those tasks are completely completed by the Web Service Access mechanism of ASP.NET. 3, SetPropertiesForCurrentUser method this method is used to save the Profile information of the current user. The input JSON string is as follows:
{'ZipCode': **, 'Address. Cite':**, 'Address.State': **}
it returns the number of correctly saved Profile. The code is as follows:
[WebMethod] public   int SetPropertiesForCurrentUser(IDictionary &lt; string , object &gt; values) { ProfileService.CheckProfileServicesEnabled(); return ProfileService.SetProfile(HttpContext.Current, values);}
the key method is ProfileService.SetProfile static method, let's take a closer look at this method: the method is not difficult to understand, but it can be required for ASP.NET AJAX have a certain understanding of the serialization and deserialization capabilities. For example, rows 22 and 23 construct a JavaScriptSerializer to use the WebServiceData information contained in the JavaScriptTypeResolver, in this way, the mapping between the type description in the form of a string and the type determination is obtained. This is actually a very useful feature, but it is a little hard to imagine that in the current WebServiceData, there is no way to add custom JavaScriptTypeResolver, so the utility of this function is 0. Will future versions take advantage of this feature? Wait and see, although I think it is more difficult. We can also see that this method uses the internal ObjectConverter.ConvertObjectToType method to convert a nested Dictionay and List to a specified type. If we want to use this method, what should we do? In fact, ASP.NET AJAX provides us with certain serialization and deserialization capabilities. See the implementation of JavaScirptSerializer public instance method ConvertToType< T>:
public T ConvertToType &lt; T &gt; ( object obj) { return (T) ObjectConverter.ConvertObjectToType(obj typeof (T) this ); }
it directly calls the internal ObjectConverter.ConverObjectToType method. Isn't this the function we need? Wait a minute! Don't be happy too early! Please note that this is a paradigm method! We can only obtain one type object that Profile the property. We cannot specify which type of model method to call at compile time. In other words, we cannot simply use this model method here. We need to "bind" the call of the model method in the later stage, because the model type can only be obtained during the execution period. Post-binding? Isn't this the function provided by Reflection? Therefore, we can use the NET Framework mechanism of. Reflection 2.0, which already supports paradigm types. Here, we can do this:
JavaScriptSerializer serializer =   new JavaScriptSerializer(); Type type = value.GetType() ; // obtain the Type object of the required Profile property. MethodInfo info =  typeof (JavaScriptSerializer) .GetMethod ( " ConvertToType " ).MakeGenericMethod(type ); return info.Invoke(serializer new   object [] { value });
In this way, we implement the "Late binding" for paradigm method calls: we decide which type of paradigm method to call only during the execution period. This may cause performance loss. For example, you can find ConvertToType method and construct a MethodInfo of the corresponding model. However, if we use Dictionary saving the type and its corresponding MethodInfo can reduce performance loss to a certain extent. However, performance loss caused by using Method.Invoke cannot be avoided. In addition, I plan to analyze ASP in detail in the following article. NET AJAX provides developers with serialization and deserialization capabilities, and how they cooperate with JavaScriptTypeResolver and JavaScriptConverter to provide certain customization capabilities.

We now know how to customize server-side Profile Service support, but if we always use the "standard" function of the client, we can't say "custom" or "extension". In the next section, let's discuss the Profile Service support of the custom client.

This article is forwarded from jeffz 51CTO blog, original link: http://blog.51cto.com/jeffz/60667,如需转载请自行联系原作者

Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now