Need help with BuildProvider
- Hello, I have two questions:
1.) How do I debug a BuildProvider? I tried opening two instances of VS, set a breakpoint, attach one instance to the other - but that does not work.
2.) I apparently have a problem with my code. The compiler says "Could not load type CafeModel.PermissionsEnumGenerator". My goal is to generate the equivalent of this:Later I will want to read an xml file but for now I'll be happy with the above.namespace CafeModel { enum Permissions { Permission0 } }
Here is my code:using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.CodeDom; using System.CodeDom.Compiler; using System.Web.Compilation; using System.IO; using System.Text; /// <summary> /// Summary description for PermissionsEnumGenerator /// </summary> /// namespace CafeModel { public class PermissionsEnumGenerator : BuildProvider { public override void GenerateCode(AssemblyBuilder ab) { CodeCompileUnit codeUnit = CreateEnums(); ab.AddCodeCompileUnit(this, codeUnit); base.GenerateCode(ab); } public CodeCompileUnit CreateEnums() { // Create codeUnit CodeCompileUnit codeUnit = new CodeCompileUnit(); // Create namespace CodeNamespace newnewNamespace = new CodeNamespace { Name = "CafeModel" }; // Add namespace to codeUnit codeUnit.Namespaces.Add(newNamespace); // Create enum declaration CodeTypeDeclaration newnewEnum = new CodeTypeDeclaration { Name = "Permissions", IsEnum = true }; // Add enum to namespace newNamespace.Types.Add(newEnum); // Create enum member CodeMemberField memberField = new CodeMemberField { Name = "Permission0" }; memberField.InitExpression = new CodePrimitiveExpression(0); // Add member to enum newEnum.Members.Add(memberField); return codeUnit; } } }
The relevent section of my web.config looks like this:
<buildProviders>
<add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider"/>
<add extension=".sam" type="CafeModel.PermissionsEnumGenerator"/>
</buildProviders>
And finally I have a blank file called plug.sam to prod the whole thing along.
Thanks for any help.
Sam
Answers
- David, you gave me enough of a hint that I figgured it out. Thanks very much.
For the benefit of anyone else who tries this:
1.) Create a new class library project. Create your buildprovider class there. The code above works fine for a simple test. Create a .dll.
2.) In the project where you want to use the output of the buildprovider, create a reference to the .dll you made in the step above.
3) Add a line to your web config like this: <add extension=".sam" type="CafeModel.PermissionsEnumGenerator"/>- Marked As Answer bySam2 Wednesday, February 25, 2009 5:52 AM
All Replies
- Please assist me I'm really stuck.
Thanks. - Still waiting
- The issue may be with your type declaration.
1. Have you referenced the file?
2. If you have, specify the assembly name for the type. For example:
<add extension=".sam" type="CafeModel.PermissionsEnumGenerator, MyAssemblyNameHere"/>
If there's an inner exception, please post it. Also, post any stack trace you're getting also.
David Morton - http://blog.davemorton.net/ - Hi David, thank you for your reply.
I did add the "add extension..." line to web config (see original post)
The only error message I get is "Could not load type CafeModel.PermissionsEnumGenerator"
What is so frustrating is that I cannot debug this. Perhaps if you can help me do that I can solve my own problem. - There's a difference between:
<add extension=".sam" type="CafeModel.PermissionsEnumGenerator, MyAssemblyNameHere"/>
and
<add extension=".sam" type="CafeModel.PermissionsEnumGenerator"/>
Does that make sense? Reread my last post. Pay specific attention to number 2.
And again, if there's an inner exception, post it. Also, post the stack trace.
David Morton - http://blog.davemorton.net/ - Sorry, I missed the assembly reference.
Also I'm still uncertain as to how to get an inner exception/stack trace since I cant debug. Visual studio displays the error I posted and stops.
- Is this an ASP.NET Application?
If so, you're probably better off posting at the ASP.NET forum.
Forums - ASP.NET Forums
Where are you getting the error that is saying "Could not load type CafeModel.PermissionsEnumGenerator"?
Is this actually showing up in VS under compiler errors? Or is this showing up at runtime when you try to start the application? If it is, you may want to set Debug to true to show the actual stack trace for the problem.
David Morton - http://blog.davemorton.net/ - Yes this is an ASP.NET application.
Yes it is showing up under complier errors at the time I try to build the app. No file or line number info. If you are referring to debug=true in web config, that is set, however the app never runs so I never get the pretty yellow page.
I'm on the road at the moment so I cant give you step by step details.
- David, you gave me enough of a hint that I figgured it out. Thanks very much.
For the benefit of anyone else who tries this:
1.) Create a new class library project. Create your buildprovider class there. The code above works fine for a simple test. Create a .dll.
2.) In the project where you want to use the output of the buildprovider, create a reference to the .dll you made in the step above.
3) Add a line to your web config like this: <add extension=".sam" type="CafeModel.PermissionsEnumGenerator"/>- Marked As Answer bySam2 Wednesday, February 25, 2009 5:52 AM

