Professional C# Third Edition phần 4 ppt

140 372 0
Professional C# Third Edition phần 4 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Figure 13-35 Figure 13-36 shows the manifest of the client application where the client references version 1.0.1318.27763 of the assembly SharedDemo. Figure 13-36 Now an application configuration file is needed. It is not necessary to work directly with XML; the .NET Framework Configuration tool can create application and machine configuration files. Figure 13-37 shows the .NET Framework Configuration tool that is an MMC Snap-in. You can start this tool from the Administrative Tools in the Control Panel. When you select Applications on the left side, and then select Action➪Add, you’ll get a list that shows all .NET applications that have been previously started on this computer. Select the application Client.exe to create an application configuration file for this application. After adding the client applica- tion to the .NET Admin tool, the assembly dependencies can be listed as is shown in Figure 13-38. 380 Chapter 13 16 557599 Ch13.qxd 4/29/04 11:31 AM Page 380 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 13-37 Figure 13-38 Selecting Configured Assemblies and the menu Action | Add configure the dependency of the assembly SharedDemo from the dependency list as is shown in Figure 13-39. For the Requested Version, specify the version that’s referenced in the manifest of the client assembly. New Version specifies the new version of the shared assembly. In Figure 13-39 it is defined that the ver- sion 1.1.1330.27636 should be used instead of any version in the range of 1.0.0.0 to 1.0.999.99999. 381 Assemblies 16 557599 Ch13.qxd 4/29/04 11:31 AM Page 381 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 13-39 Now you can find the application configuration file Client.exe.config in the directory of the Client.exe application that includes this XML code: <?xml version=”1.0”?> <configuration> <runtime> <assemblyBinding xmlns=”urn:schemas-microsoft-com:asm.v1”> <dependentAssembly> <assemblyIdentity name=”SharedDemo” publicKeyToken=”be9f9ce7b9a0a62f” /> <bindingRedirect oldVersion=”1.0.0.0-1.0.999.99999” newVersion=”1.1.1330.27636” /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> With the <runtime> element, runtime settings can be configured. The subelement of <runtime> is <assemblyBinding>, which in turn has a subelement <dependentAssembly>. <dependentAssembly> has a required subelement <assemblyIdentity>. We specify the name of the referenced assembly with <assemblyIdentity>. name is the only mandatory attribute for <assemblyIdentity>. The optional attributes are publicKeyToken and culture. The other subelement of <dependentAssembly> that’s needed for version redirection is <bindingRedirect>. With this element the old and the new version of the dependent assembly is specified. Starting the client with this configuration file, you will get the new version of the referenced shared assembly. 382 Chapter 13 16 557599 Ch13.qxd 4/29/04 11:31 AM Page 382 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Publisher policy files Using assemblies that are shared in the global assembly cache you can also use publisher policies to override versioning issues. Let’s assume that we have a shared assembly that is used by some applica- tions. What if a bug is found in the shared assembly? We have seen that it is not necessary to rebuild all the applications that use this shared assembly as we can use configuration files to redirect to the new version of this shared assembly. Maybe we don’t know all the applications that use this shared assembly, but we want to get the bug fix to all of them. In that case we can create publisher policy files to redirect all applications to the new version of the shared assembly. To set up publisher policies we have to: ❑ Create a publisher policy file. ❑ Create a publisher policy assembly. ❑ Add the publisher policy assembly to the global assembly cache. Create a Publisher Policy File A publisher policy file is an XML file that redirects an existing version or version range to a new version. The syntax used is the same as for application configuration files, so we can use the same file we created earlier to redirect the old versions 1.0.0.0 through 1.0.999.99999 to the new version 1.1.1330.27636. Rename the previously created file to mypolicy.config to use it as a publisher policy file. <?xml version=”1.0”?> <configuration> <runtime> <assemblyBinding xmlns=”urn:schemas-microsoft-com:asm.v1”> <dependentAssembly> <assemblyIdentity name=”SharedDemo” publicKeyToken=”be9f9ce7b9a0a62f” /> <bindingRedirect oldVersion=”1.0.0.0-1.0.999.99999” newVersion=”1.1.1330.27636” /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> Create a Publisher Policy Assembly To associate the publisher policy file with the shared assembly it is necessary to create a publisher policy assembly, and put it into the global assembly cache. The tool that can be used to create such files is the assembly linker al. The option /linkresource adds the publisher policy file to the generated assembly. The name of the generated assembly must start with policy, followed by the major and minor version number of the assembly that should be redirected, and the filename of the shared assembly. In our case Publisher policy files only apply to shared assemblies installed into the global assembly cache. 383 Assemblies 16 557599 Ch13.qxd 4/29/04 11:31 AM Page 383 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com the publisher policy assembly must be named policy.1.0.SharedDemo.dll to redirect the assemblies SharedDemo with the major version 1, and minor version 0. The key that must be added to this pub- lisher key with the option /keyfile is the same key that was used to sign the shared assembly SharedDemo to guarantee that the version redirection is from the same publisher. al /linkresource:mypolicy.config /out:policy.1.0.SharedDemo.dll /keyfile: \ \mykey.snk Add the Publisher Policy Assembly to the Global Assembly Cache The publisher policy assembly can now be added to the global assembly cache with the utility gacutil. gacutil –i policy.1.0.SharedDemo.dll Now remove the application configuration file that was placed in the directory of the client application, and start the client application. Although the client assembly references 1.0.1318.24054 we use the new version 1.1.1330.27636 of the shared assembly because of the publisher policy. Overriding Publisher Policies With a publisher policy, the publisher of the shared assembly guarantees that a new version of the assembly is compatible with the old version. As we know from changes of traditional DLLs, such guar- antees don’t always hold. Maybe all but one application is working with the new shared assembly. To fix the one application that has a problem with the new release, the publisher policy can be overridden by using an application configuration file. With the .NET Framework configuration tool you can override the publisher policy by deselecting the check box Enable Publisher Policy as shown in Figure 13-40. Figure 13-40 384 Chapter 13 16 557599 Ch13.qxd 4/29/04 11:31 AM Page 384 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Disabling the publisher policy with the .NET Framework Configuration results in a configuration file with the XML element <publisherPolicy> and the attribute apply=”no”. <?xml version=”1.0”?> <configuration> <runtime> <assemblyBinding xmlns=”urn:schemas-microsoft-com:asm.v1”> <dependentAssembly> <assemblyIdentity name=”SharedDemo” publicKeyToken=”be9f9ce7b9a0a62f” /> <publisherPolicy apply=”no” /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> Disabling the publisher policy we can configure different version redirection in the application configu- ration file. Fixing an application If an application doesn’t run because a configuration is wrong or because newly installed assemblies let it fail, the .NET Framework Configuration has an option to fix .NET applications. Clicking the Fix an Application hyperlink lists all .NET applications that were running previously and it allows you to restore the last version of the application configuration file (see Figure 13-41), or select any previous con- figuration. Selecting the Application SafeMode disables publisher policies. Figure 13-41 Runtime version Installing and using multiple versions is not only possible with assemblies, but also with the .NET run- time. Both versions 1.0 and 1.1 (and future versions) of the .NET runtime can be installed on the same operating system side by side. Visual Studio .NET 2003 by default targets applications running on .NET 1.1. However, this can be changed by modifying the values in the application configuration file. 385 Assemblies 16 557599 Ch13.qxd 4/29/04 11:31 AM Page 385 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com An application that was built using .NET 1.0 may run without changes on .NET 1.1. If an operating sys- tem has both versions of the runtime installed, the application will use the version with which it was built. However, if only version 1.1 is installed with the operating system, and the application was built with version 1.0, it tries to run with the newer version. The registry key HKEY_LOCAL_MACHINE \Software\Microsoft\.NETFramework\policy lists the ranges of the versions that will be used for a specific runtime. If an application was built using .NET 1.1, it may run without changes on .NET 1.0, in case no classes or methods are used that are only available with .NET 1.1. To make this possible, an application configura- tion file is needed. In an application configuration file, it’s not only possible to redirect versions of referenced assemblies; we can also define the required version of the runtime. Different .NET runtime versions can be installed on a single machine. We can specify the version that’s required for the application in an application con- figuration file. The element <supportedVersion> marks the runtime versions that are supported by the application. <?xml version=”1.0”?> <configuration> <startup> <supportedRuntime version=”v1.1.4322” /> <supportedRuntime version=”v1.0.3512” /> </startup> </configuration> However, with .NET 1.0 instead of the element <supportedVersion> the element <requiredRuntime> was used to specify the needed runtime. Configuring the .NET Framework ver- sions in the project properties (see Figure 13-42) adds both <supportedRuntime> and <requiredRuntime> elements as well as assembly binding information we discussed earlier. <?xml version=”1.0”?> <configuration> <startup> <supportedRuntime version=”v1.1.4322”/> <supportedRuntime version=”v1.0.3705”/> <requiredRuntime version=”v1.0.3512” safeMode=”true” /> </startup> </configuration> <requiredRuntime> does not overrule the configuration for <supportedRuntime> as it may look like, because <requiredRuntime> is used only with .NET 1.0, while <supportedRuntime> is used by .NET 1.1 and later versions. 386 Chapter 13 16 557599 Ch13.qxd 4/29/04 11:31 AM Page 386 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 13-42 Configuring Directories We’ve already seen how to redirect referenced assemblies to a different version so that we can locate our assemblies, but there are more options to configure! For example, it’s not necessary to install a shared assembly in the global assembly cache. It’s also possible that shared assemblies can be found with the help of specific directory settings in configuration files. This feature can be used if you want to make the shared components available on a server. Another possible scenario is if you want to share an assembly between your applications, but you don’t want to make it publicly available in the global assembly cache, so you put it into a shared directory instead. There are two ways to find the correct directory for an assembly: the codeBase element in an XML con- figuration file, or through probing. The codeBase configuration is only available for shared assemblies, and probing is done for private assemblies. <codeBase> The <codeBase> can also be configured using the .NET Admin Tool. Codebases can be configured by selecting the properties of the configured application, SimpleShared, inside the Configured Assemblies in the Applications tree. Similar to the Binding Policy, we can configure lists of versions with the Codebases tab. In the following screen we have configured that the version 1.0 should be loaded from the Web server http://www.christiannagel.com/WroxUtils: 387 Assemblies 16 557599 Ch13.qxd 4/29/04 11:31 AM Page 387 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 13-43 The .NET Admin tool creates this application configuration file: <?xml version=”1.0”?> <configuration> <runtime> <assemblyBinding xmlns=”urn:schemas-microsoft-com:asm.v1”> <dependentAssembly xmlns=””> <assemblyIdentity name=”SimpleShared” publicKeyToken=”6ca9587197f6f8c2” /> <codeBase version=”1.0” href=”http://www.christiannagel.com/WroxUtils” /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> The <dependentAssembly> element is the same used previously for the version redirection. The <codeBase> element has the attributes version and href. With version, the original referenced ver- sion of the assembly must be specified. With href, we can define the directory from where the assembly should be loaded. In our example, a path using the HTTP protocol is used. A directory on a local system or a share is specified using href=”file:C:/WroxUtils”. When using that assembly loaded from the network a System.Security.Permissions exception occurs. You must configure the required permissions for assemblies loaded from the network. In Chapter 14 we show how to configure security for assemblies. 388 Chapter 13 16 557599 Ch13.qxd 4/29/04 11:31 AM Page 388 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com <probing> When the <codeBase> is not configured and the assembly is not stored in the global assembly cache, the runtime tries to find an assembly with probing. The .NET runtime tries to find an assembly with either a .dll or an .exe file extension in the application directory, or in one of its subdirectories, that has the same name as the assembly searched for. If the assembly is not found here, the search continues. You can con- figure search directories with the <probing> element in the <runtime> section of application configura- tion files. This XML configuration can also be done easily by selecting the properties of the application with the .NET Framework Configuration tool. You can configure the directories where the probing should occur by using the search path in the .NET Framework configuration (see Figure 13-44). Figure 13-44 The XML file produced has these entries: <?xml version=”1.0”?> <configuration> <runtime> <gcConcurrent enabled=”enabled” /> <assemblyBinding xmlns=”urn:schemas-microsoft-com:asm.v1”> <probing privatePath=”bin;utils;” xmlns=”” /> </assemblyBinding> </runtime> </configuration> The <probing> element has just a single required attribute: privatePath. This application configura- tion file tells the runtime that assemblies should be searched for in the base directory of the application, followed by the bin and the util directory. Both directories are subdirectories of the application base 389 Assemblies 16 557599 Ch13.qxd 4/29/04 11:31 AM Page 389 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... StrongName - 00 240 000 048 00000 940 000000602000000 240 000525 341 3100 040 0000100010007D1FA57C4AED9F0A32E84AA0FAEFD0DE9E8FD6AEC8F87FB03766C834C99921EB23BE 79AD9D5DCC1DD9AD236132102900B723CF980957FC4E177108FC607774F29E8320E92EA05ECE4E82 1C0A5EFE8F1 645 C4C0C93C1AB99285D622CAA652C1DFAD63D 745 D6F2DE5F17E5EAF0FC4963D261C8 A1 243 6518206DC093 344 D5AD293: FullTrust 1.1.2 StrongName - 0000000000000000 040 0000000000000: FullTrust... resembles the one in Figure 14- 5 40 6 .NET Security Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 14- 5 However, if you copy the executable to a network share and run it again, you are operating within the LocalIntranet permission sets, which blocks access to local storage, and the button will be dimmed as is shown in Figure 14- 6 Figure 14- 6 If the functionality to make... PublicKeyToken=b77a5c561934e089” version=”1” Access=”Open”/> ... the All Code membership condition at the root (see Figure 14- 1) You can see that each code group has a single membership condition and specifies the permissions that the code group has been granted Note that if an assembly does not match the membership condition in a code group, the CLR does not attempt to match code groups below it 393 Chapter 14 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com... an entire organization, and that is why NET provides not one, but three levels of code groups: ❑ Machine ❑ Enterprise ❑ User The code group levels are independently managed and exist in parallel: 40 3 Chapter 14 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com User Level Code Group: All Code Permission: Nothing Membership Condition: All Code Enterprise Level Code Group: All Code . 00 240 000 048 00000 940 000000602000000 240 000525 341 3100 040 0000100010007D1FA57C4AED9F0A32E84AA0FAEFD0DE9E8FD6AEC8F87FB03766C834C99921EB23BE 79AD9D5DCC1DD9AD236132102900B723CF980957FC4E177108FC607774F29E8320E92EA05ECE4E82 1C0A5EFE8F1 645 C4C0C93C1AB99285D622CAA652C1DFAD63D 745 D6F2DE5F17E5EAF0FC4963D261C8 A1 243 6518206DC093 344 D5AD293:. 00 240 000 048 00000 940 000000602000000 240 000525 341 3100 040 0000100010007D1FA57C4AED9F0A32E84AA0FAEFD0DE9E8FD6AEC8F87FB03766C834C99921EB23BE 79AD9D5DCC1DD9AD236132102900B723CF980957FC4E177108FC607774F29E8320E92EA05ECE4E82 1C0A5EFE8F1 645 C4C0C93C1AB99285D622CAA652C1DFAD63D 745 D6F2DE5F17E5EAF0FC4963D261C8 A1 243 6518206DC093 344 D5AD293: FullTrust 1.1.2. StrongName - 0000000000000000 040 0000000000000:. the check box Enable Publisher Policy as shown in Figure 13 -40 . Figure 13 -40 3 84 Chapter 13 16 557599 Ch13.qxd 4/ 29/ 04 11:31 AM Page 3 84 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Disabling

Ngày đăng: 13/08/2014, 15:21

Mục lục

  • The Common Language Runtime

    • Advantages of Managed Code

    • A Closer Look at Intermediate Language

      • Support for Object Orientation and Interfaces

      • Distinct Value and Reference Types

      • Error Handling with Exceptions

      • Chapter 2: C# Basics

        • Before We Start

        • Our First C# Program

          • The Code

          • Compiling and Running the Program

          • Predefined Data Types

            • Value Types and Reference Types

            • The Main() Method

              • Multiple Main() Methods

              • Passing Arguments to Main()

              • More on Compiling C# Files

              • Using Comments

                • Internal Comments Within the Source Files

                • The C# Preprocessor Directives

                  • #define and #undef

                  • #if, #elif, #else, and #endif

                  • C# Programming Guidelines

                    • Rules for Identifiers

                    • Chapter 3: Objects and Types

                      • Classes and Structs

                      • Structs

                        • Structs Are Value Types

                        • Chapter 4: Inheritance

                          • Types of Inheritance

                            • Implementation Versus Interface Inheritance

                            • Calling Base Versions of Functions

                            • Abstract Classes and Functions

Tài liệu cùng người dùng

Tài liệu liên quan