dynamic tools for .net developers™  LaMarvin Home
home » constable » faq » resources

Embedding a XML-based policy as a resource

In order to embed a XML-based authorization policy into an assembly's manifest, you have to add the XML file (typically with the .caze extension) to the application's project and mark it as "Embedded Resource" (in the file's property window). After compiling the project, you can use code like this to load the policy into memory:
[Visual Basic]
Dim resourceName As String = "LaMarvin.DocumentApproval._doc.caze"
Dim policy As AuthorizationPolicy = _
  AuthorizationPolicyLoader.LoadFromXmlResource(resourceName)
  
[C#]
string resourceName = "LaMarvin.DocumentApproval._doc.caze";
AuthorizationPolicy policy =
  AuthorizationPolicyLoader.LoadFromXmlResource(resourceName);
We've used the AuthorizationPolicyLoader class to load the model from an embedded XML resource.

It is important to note that the assembly calling the LoadFromXmlResource method contains the specified XML resource, otherwise the method will throw an exception. If the calling assembly doesn't contain the XML resource (perhaps it is in another resource-only assembly), you can load the resource yourself and then pass the resource stream to the LoadFromStream method as the following pseudo-code illustrates:
[Visual Basic]
Dim resAssembly As System.Reflection.Assembly = <load the resource assembly>

Dim s As System.IO.Stream = _ 
  resAssembly.GetManifestResourceStream(resourceName))
Try
  policy = AuthorizationPolicyLoader.LoadFromStream(s)
Finally
  s.Close()
End Try

[C#]
System.Reflection.Assembly resAssembly = <load the resource assembly>; 
using (
  System.IO.Stream s = resAssembly.GetManifestResourceStream(resourceName))
{
  policy = AuthorizationPolicyLoader.LoadFromStream(s);
}




© 2002-2007 LaMarvin. All Rights Reserved.     [Terms of use]     [Privacy] This site doesn't open new browser windows.