读书笔记之《Introducing Microsoft .NET》 (Second Edition) :(Chinese Name——《.Net 精髓》)

Reading Note
Charles Yan From 2007-2-1 To ……
一、            Introducing Microsoft .NET (Second Edition) Chinese Name——.Net 精髓 AuthorDavid S. Platt   ISBN: 0735615713 
Summary:
Overviews Microsoft .NET as a prefabricated infrastructure for solving common problems in internet applications running on the Windows 2000 operating system. The author discusses garbage collection, code reuse, ASP.NET and web forms, web services that allow internet servers to expose functions to any client, and windows forms that provide prefabricated user interface elements.
1.2-5 Assemblies
The .Net Framework makes extensive use of assemblies for .net code, resources, and metadata. All code that the .net common language runtime executes must reside in an assembly. In addition, all security, namespace resolution, and versioning features work on a per-assembly basis. Since assemblies are used so often and for so many different things, I need to discuss assemblies in some detail.
.net makes extensive use of a new packaging unit called an assembly.
l         Concept of an Assembly
An assembly is a logical collection of one or more EXE or DLL files containing an application’s code and resources. An assembly also contains a manifest, which is a metadata description of the code and resources “inside” the assembly. An assembly can be, and often is, a single file, either an EXE or a DLL, as shown in Figure 2-4 in the book.
l         An assembly can also be a logical collection of more than one files
You can view the manifest of an assembly using the IL Disassembler (ILDASM.exe)
You add and remove files from a multifile assembly using the command line SDK utility program AL.exe, the Microsoft Assembly Linker.
In the case of a shared assembly, of which more anon, the manifest also contains a public cryptographic key, which is used to ensure that the assembly can be distinguished from all other assemblies regardless of its filename.
You can place assemblies into the cache GAC view their properties ,and remove them from the GAC using a .net framework SDK command line utility called GACUTIL.exe, which works well when run from scripts and batch files.
You generate this file (a file containing the public/private key pair) with the SDK command line utility program SN.exe.
The public/private key algorithm also provides a check on the integrity of the assembly’s files.
The compatibility version number consists of a major and minor number, a build number, and a revision number.
The most common way to do this is with a publisher policy, which changes the versioning behavior for all clients of a GAC assembly. You set a publisher policy by making entries in the master configuration file machine.config, which holds the .net administrative settings for your entire machine.
You set a publisher policy using the .net framework Configuration utility mscorcfg.msc.
You enter one or more binding policies, each of which consists of a set of one or more old versions that the assembly loader will map to exactly one new version.
You can also enter a codebase for an assembly, which tells the loader from where to download a requested version if it isn’t already present on the machine.
An individual application can override a publisher policy’s version behavior with its own configuration file. The name of this file is the full name of the application that it configures, including the extension, with the additional extension “.config” tacked onto the end.
2..net memory management
You can force a garbage collection manually by calling the function System.GC.Collect.
By calling the function System.GC.SupperessFinalize, you tell the garbage collector not to bother calling your finalizer even though you have one.
3.Interoperation with COM
A .net client accesses a COM object through a runtime callable wrapper (RCW). Select the COM object for which you want to generate the RCW, and VS.net will split it out for you. If you’re not using VS.net, the .net SDK contains a command line tool called Tlblmp.exe (类型库导入工具), the type library importer that performs the same task.
If you would like to blow away one particular COM object without affecting the others, you can do so via the function System.Runtime.InteropService.Marshal.ReleaseComObject.
A COM client accesses a .net object through a COM callable wrapper (CCW)
4.ASP.net
l         Managing and configuring web application project: the web.config file
The master file specifying the defaults for the entire ASP.NET system has the name machine.config and lives in the directory [system, e.g. WINNT]/Microsoft.NET/Framework/[version]/Config.
l         Input controls can maintain their contents and selection from one round- trip to another. Display controls can also maintain their state from one round-trip to another.
Another great feature of the input controls in the Web controls package (list box, text box, check box, radio button, and so on) is that they can automatically remember the state in which the user left them when the page they are on makes a round-trip to the server. Microsoft calls this feature postback data. Observe the behavior of the check box control in the simplest example that I showed in the previous section. The check box correctly maintains its checked or unchecked state when the page is posted to the server for event handling and returned to the client browser afterward. I didn’t have to write any code to get this behavior, as I would have had to in classic HTML. Input Web controls automatically remember their state. During the postback operation, the data actually resides in a dedicated field in the HTTP header of the page. This is an automatic feature that you can’t turn off.
The display controls in the Web controls package, such as label, data list, data grid, and repeater, support their own version of property retention, called view state. Even though the user doesn’t set them to specific values, they still remember the state in which the program left them in the previous round trip. The .ASPX page environment automatically places a hidden input control called __VIEWSTATE on each of its pages. Web controls automatically serialize their state into this hidden control when the page is being destroyed and then retrieve their state when the page is next created, which is what you want most of the time. If you don’t want this, you easily can turn it off by setting the control’s MaintainState property to False.
l         A setting made in a web.config file overrides the settings made in directories above it.
Not all sections of the web.config file are configurable on all levels of subdirectory. The <sessionstate> section, for example, may not appear below an application’s root directory. You’ll have to examine the details of individual sections to find out the granularity of each.
l         ASP.NET provides a set of data tied to a specific user. Called a Session, programmers can use this object to store data for a specific user.
Original ASP provided a simple mechanism for managing session state, which ASP.NET supports and extends. Every time a new user accesses an .ASPX page, ASP.NET creates an internal object called a Session. This object is an indexed collection of data living on the server and tied to a particular active user. You can access items in the collection by means of a numerical index or a string name that you specify. The Session object can hold any number of items for each user, but since they all use server memory, I advise you to limit the amount of data you store in session state to the minimum necessary.
You can also dump the session yourself by calling the method Session.Abandon.
l         Session state can be stored in a separate process for robustness
While the session mechanism in original ASP was easy to use, it had a number of drawbacks that hampered its expansion to large-scale systems. First, it stored session state in the worker processes that actually ran page scripts and called code living in programmer-written custom objects. A badly behaved object could and often did crash this process, thereby killing the session state of every user it was currently serving, not just the one whose call caused the crash. ASP.NET fixes this problem by providing the ability to store session state in a separate process, one that runs as a system service (see Figure 3-13), so badly behaved user code can’t kill it. This means that worker processes can come and go without losing their session state. It slows down the access to the session state somewhat, as applications now need to cross process boundaries to get to their session state, but most developers figure that’s worth it for reliability. To turn this feature on, set the mode attribute in the web.config file to stateserver and ensure that the state server process is running on the server machine.
l         ASP.NET contains good prefabricated support for forms-based authentication
If the server accepts the incoming user, a simple function call (to System.Web.Security.FormsAuthentication.RedirectFromLoginPage) brings the user back to the page that he was trying to access when the authentication scheme shunted him to the login screen. If you’d like to send him somewhere else instead, you can use the function Response.Redirect.
l         You can administratively specify which users are allowed to view various pages by making entries in the web.config files for specific pages or directories.
Although web.config files apply to an entire directory (and its subdirectories unless overridden lower down), you can administratively restrict access to a single file by using the <location> element (not shown).
Note that when using Windows authentication on a domain, you must prepend the domain name onto the role or user name in order for ASP.NET to recognize it,
This object contains a method called IsInRole, which you can call to see if the currently authenticated user is a member of the specified role. The page is shown in Figure 3-18, and the code in Listing 3-14. If you’d like finer granularity, you can access the object Page.User.Identity, which contains the user’s name.
l         A Web server can impersonate a client programmatically, if using Windows authentication.
The Web server impersonates a client in one of two ways. Original ASP automatically impersonated the client every time a request was made. ASP.NET does not do this by default, but you can turn on this functionality in the web.config file by setting the impersonation attribute shown in Listing 3-16 to True. Alternatively, you can impersonate the client programmatically by calling the function System.Security.Principal.WindowsIdentity.GetCurrent().Impersonate.
l         ASP.NET supports process recycling to continue working robustly in the face of imperfect user code.
Original ASP kept a user process running indefinitely. Any bugs or memory leaks in any of the user code would accumulate and eventually cause crashes. ASP.NET recognizes that user code probably isn’t going to be perfect. It therefore allows an administrator to configure the server to periodically shut down and restart worker processes.
You configure process recycling using the <processModel> element of the machine-level machine.config file, as shown in Listing 3-17. I wish Microsoft had allowed process model configuration on a per-application basis, but they haven’t at the time of this writing. You can tell ASP.NET to shut down your worker process and launch a new one after a specified amount of time (timeout attribute), a specified number of page requests (requestLimit attribute), or if the percentage of system memory it consumes grows too large (memoryLimit attribute). While not removing the need to make your code as robust as possible, this will allow you to run with a few memory leaks without rebooting the server every day or two. You can see that the process model contains other configurable capabilities as well. IIS 6.0, which is scheduled to ship with Windows Server 2003, contains a mode called worker process isolation. When this mode is active, ASP.NET’s process model settings are ignored in favor of IIS’s own internal settings that provide the same sorts of functionality.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值