Compiling and Deploying a Custom Provider
After you create a custom provider, you must compile it and deploy the assembly to the appropriate location. If you created the provider within the Enterprise Library solution (on your Start menu) in Visual Studio, it will be compiled by default into the assembly Microsoft.Practices.EnterpriseLibrary.Caching.dll
that contains the Caching Application Block. The easiest way to achieve this is to use the two utilities available from the Enterprise Library section of your Start menu: "Build Enterprise Library" and "Copy Assemblies to bin Folder." These actions incorporate the provider into Enterprise Library, and it will appear in the list of installable providers in the Configuration Console.
Alternatively, if you created your custom provider in a separate project, you can compile it into a separate assembly, and then copy the assembly into the correct location for use in your applications. The best choice for the provider is %Program Files%\Microsoft Enterprise Library January 2006\bin
although you can place it into the bin
folder of your application if you wish, where it will be private to and only available within that application. In the Configuration Console, you can load the assembly and select the provider when configuring the Caching Application Block.
Using the Custom File Cache Provider
|Figure 4. Custom Cache Storage: The figure shows the process for adding a Custom Cache Storage item to the application configuration.|
To use this custom provider in your application, you must first configure the Caching Application Block to treat it as a custom backing store provider, and specify the configuration information the provider requires.
Configuring the Custom File Cache Provider
The example provider you have created inherits from the class CustomCacheStorageData, which is one of the types supported by the Enterprise Library Configuration Console for the Caching Application Block. After adding the Caching Application Block to your application's configuration, right-click the Cache Manager node and select New, then select Custom Cache Storage as shown in Figure 4
The Configuration Console adds the Cache Storage
node, and the right-hand window displays the properties of the node. You can edit the name, and you must specify the actual object type that implements your custom cache backing store provider. Select the Type
property entry and click the "(...)" button that appears. This opens the Type Selector dialog, where you can select a class that follows the rules of implementing the IBackingStore interface and has a configuration attribute that indicates it is of type CustomCacheStorageData.
|Figure 5. Selecting a Custom Backing Store Provider: Select your custom backing store provider from the Type Selector dialog in the Configuration Console.|
If you compiled the provider into the Caching Application Block, using the Visual Studio Enterprise Library solution, your custom provider will show in the Type Selector dialog and you can simply select it and click OK (see Figure 5
). If you compiled it into a separate assembly, click the "Load" button, navigate to the folder containing the assembly, and select it. The Type Selector dialog will then show your custom provider so that you can select it and click OK.
The final step is to configure the remaining properties of the provider. Recall from the earlier discussion (in the section "Creating the Class Constructor
") that the Enterprise Library configuration system will expose the attributes from the configuration file as name/value pairs within a NameValueCollection instance passed to the constructor. In the Configuration Console, you specify these name/value pairs as the Attributes
Select the "Attributes" entry in the right-hand window of the Configuration Console and click the "(...)" button that appears. This opens the EditableKeyValue Collection Editor dialog. In this dialog, click the Add button and enter the key (name) and value for the path attribute the custom provider requires (see Figure 6
). Specify a folder that ASP.NET can write to if you are using the provider in an ASP.NET application. Then click OK to save the configuration.
|Figure 6. Custom Cache Backing Store Configuration: In the EditableKeyValue Collection editor, specify the name/value pairs for the custom cache backing store provider.|
|Author's Note: For an ASP.NET application, you should save the file as Web.config. In a Windows Forms or Console application, you should save the file as App.config. If your ASP.NET application already has a Web.config file, as is the case when you use Visual Studio or Visual Web Developer to create a new Web site, you can open that file into the Enterprise Library Configuration Console, edit it to add and configure the blocks you need, then save it. This does not change any other settings in the file.
If you then open the configuration file in a text editor, you'll see the settings for the custom provider:
<cachingConfiguration defaultCacheManager="Cache Manager">
<add path="C:\Temp\" encryptionProviderName=""
Version=220.127.116.11, Culture=neutral, PublicKeyToken=null"
name="Custom Cache Storage" />
Notice the four attributes of the custom provider <add>
element, which indicate the property settings for the provider. The type
attributes correspond to the Type
properties, and the NameValueCollection exposes the path attribute and its value. You can configure an encryption provider for a custom cache backing store provider, but as the example provider does not use one, this attribute is empty in this example.
|Author's Note: To implement encryption in your custom provider, you can use the Configuration Console to add a new Symmetric Storage Encryption node to the Cache Storage node configuration, and configure an encryption provider from the Cryptography Application Block for this node. The name of the encryption provider then appears as the StorageEncryption property of the custom provider, and your code can encrypt and decrypt the data in the same way as the example application shown in earlier related articles.