.NET Framework Developer Center > .NET Development Forums > MSBuild > Single-page assembly compilation not working as expected with CodeBehind page model
Ask a questionAsk a question
 

AnswerSingle-page assembly compilation not working as expected with CodeBehind page model

  • Thursday, August 09, 2007 12:45 PMNxbber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    We have a large project in VS2005 which was based on the Web Site Project model, we have just converted to the Web Application Project model and have found large differences when compiling with page-level assemblies.

     

    To explain:

      - Create a new Web Application Project in VS2005 called 'TestApp';

      - Create a new Web Deployment Project

      - Add this node into the WDP, before the </Project> tag at the end of the file:

     

    <Target Name="Build">

    <AspNetCompiler FixedNames="true" TargetPath="C:\temp\output" Clean="true" Force="true" Updateable="false" PhysicalPath="$(SourceWebPhysicalPath)" VirtualPath="/TestApp.csproj_deploy" />

    </Target>

     

      - Build the Web Deployment Project which will create the files in 'C:\temp\output\bin'

     

    You will see two dlls in there, one called TestApp and one for the default page. Using Reflector look at these dlls for the Page_Load code, its not in the page level assembly which is where I would expect to see it but in the main TestApp.dll.

     

    Now, change the default.aspx page directive from CodeBehind="default.aspx.cs" to CodeFile="default.aspx.cs" and rebuild the Web Deployment Project. Again, check the output in 'C:\temp\output\bin', notice the TestApp.dll hasnt been rebuilt and using Reflector you will see that the page level assembly now has the Page_Load event exactly where you would expect it to be. Obviously, this is a hack and won't work in the real world but it demonstrates that the compilation for page-level assemblies is definately not the same between the Web Site Project model and the Web Application Project model.

     

    Can anyone shed further light on this and advise on how we can generate page-level assemblies for our application that match the structure from the old CodeFile model or explain what we are missing here.

     

    We need to deploy incremental updates using page-level assemblies having been doing so for years using the Web Site Project model.

     

    Thanks

     

    Rich

Answers

  • Thursday, August 16, 2007 11:22 AMNxbber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

     

    The solution to this is that there is no solution. Single-page assembly compilation between the Web Application Project and Web Site Project models are fundamentally different and incompatible.

     

    The advice is don't change from a web site projec to web application project model in VS2005 when you are using single-page assemblies unless you can redeploy the entire project after the change. There are some questions concerning if single-page assemblies are truly single-page in the new web application project model. I haven't had time to clarify this.

     

    Hope this helps.

     

    Rich

  • Wednesday, August 22, 2007 8:21 PMDaniel Portella Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi rich had a look at the page directive help on msdn and it looks like codebehind is only there to be backwards compatible with previous vesions of asp net preior to 2.0. codefile should be used instead, see snippet below from the page and the link to it.

     

    ----- snippet

    CodeBehind

    Specifies the name of the compiled file that contains the class associated with the page. This attribute is not used at run time.

    Note

    This attribute is included for compatibility with previous versions of ASP.NET, to implement the code-behind feature. In ASP.NET version 2.0, you should instead use the CodeFile attribute to specify the name of the source file, along with the Inherits attribute to specify the fully qualified name of the class.

    CodeFile

    Specifies a path to the referenced code-behind file for the page. This attribute is used together with the Inherits attribute to associate a code-behind source file with a Web page. The attribute is valid only for compiled pages.

    ----

    URL http://msdn2.microsoft.com/en-us/library/ydy4x04a.aspx

     

    But before you build the web application you must switch the build action for all the cs files (pages and controls) to none, so when the web application gets compiled you dont end up with the pages in two places.

     

    after you do that you get your web application dll, with any classes that you created on your web application and all the web user controls and page into their individual assemblies.

     

    I hope this helps anyone out there, I have also changed all visual studio templates to use the codefile attribute instead of codebehind, now all i need to check if i can make the build action part of the template.

     

    UPDATED

    Ahah looking on the web i found that you can change the build action on item templates. please have a look at this thread, it contains the answer and a link to a blog with examples.

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=159967&SiteID=1

     

    So we got a full solution to the problem now.

     

    I will be creating a new web application project package to address this problem in full. I will find some where to post it so everyone in the community can see and have access to it.

     

    regards

     

    dan

All Replies

  • Thursday, August 16, 2007 11:22 AMNxbber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

     

    The solution to this is that there is no solution. Single-page assembly compilation between the Web Application Project and Web Site Project models are fundamentally different and incompatible.

     

    The advice is don't change from a web site projec to web application project model in VS2005 when you are using single-page assemblies unless you can redeploy the entire project after the change. There are some questions concerning if single-page assemblies are truly single-page in the new web application project model. I haven't had time to clarify this.

     

    Hope this helps.

     

    Rich

  • Wednesday, August 22, 2007 10:17 AMDaniel Portella Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     Nxbber wrote:

     

    The solution to this is that there is no solution. Single-page assembly compilation between the Web Application Project and Web Site Project models are fundamentally different and incompatible.

     

    The advice is don't change from a web site projec to web application project model in VS2005 when you are using single-page assemblies unless you can redeploy the entire project after the change. There are some questions concerning if single-page assemblies are truly single-page in the new web application project model. I haven't had time to clarify this.

     

    Hope this helps.

     

    Rich

     

    If any one is able to answer this it would be really helpful to understand why it was done this way. I shall put a link to this thread on asp .net forums and news groups.

  • Wednesday, August 22, 2007 8:21 PMDaniel Portella Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi rich had a look at the page directive help on msdn and it looks like codebehind is only there to be backwards compatible with previous vesions of asp net preior to 2.0. codefile should be used instead, see snippet below from the page and the link to it.

     

    ----- snippet

    CodeBehind

    Specifies the name of the compiled file that contains the class associated with the page. This attribute is not used at run time.

    Note

    This attribute is included for compatibility with previous versions of ASP.NET, to implement the code-behind feature. In ASP.NET version 2.0, you should instead use the CodeFile attribute to specify the name of the source file, along with the Inherits attribute to specify the fully qualified name of the class.

    CodeFile

    Specifies a path to the referenced code-behind file for the page. This attribute is used together with the Inherits attribute to associate a code-behind source file with a Web page. The attribute is valid only for compiled pages.

    ----

    URL http://msdn2.microsoft.com/en-us/library/ydy4x04a.aspx

     

    But before you build the web application you must switch the build action for all the cs files (pages and controls) to none, so when the web application gets compiled you dont end up with the pages in two places.

     

    after you do that you get your web application dll, with any classes that you created on your web application and all the web user controls and page into their individual assemblies.

     

    I hope this helps anyone out there, I have also changed all visual studio templates to use the codefile attribute instead of codebehind, now all i need to check if i can make the build action part of the template.

     

    UPDATED

    Ahah looking on the web i found that you can change the build action on item templates. please have a look at this thread, it contains the answer and a link to a blog with examples.

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=159967&SiteID=1

     

    So we got a full solution to the problem now.

     

    I will be creating a new web application project package to address this problem in full. I will find some where to post it so everyone in the community can see and have access to it.

     

    regards

     

    dan

  • Monday, November 12, 2007 11:15 AMDaniel Portella Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    UPDATE

     

    I have createda  blog with a step by step on how to get the web application project working correctly. Have alook at http://undocnet.blogspot.com/ or for the specific post go here http://undocnet.blogspot.com/2007/11/web-application-project.html

     

    Regards to all of you from

     

    Dancoe

  • Friday, January 30, 2009 4:22 PMDaniel Portella Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    UPDATE

    Hehe it has been a while... the problem still persists. But i closer to making the new project type.

    Daniel Portella - http://undocnet.blogspot.com - This posting is provided "AS IS" with no warranties, and confers no rights. If this post is answered your question please mark as the answer and if it is helpful do like wise.