Using post sharp with web projects?

Questions about integration in Visual Studio, MSBuild or other IDEs or build tools.

Using post sharp with web projects?

Postby dawginlife on Wed Jun 06, 2007 1:36 pm


I can't seem to get PostSharp working on my c# asp.net web service project.  Are there additional configuration needed to get Laos to work with web projects? 

Thanks
dawginlife
 
Posts: 6
Joined: Sat Jan 12, 2008 4:55 pm

Re: Using post sharp with web projects?

Postby gfraiteur on Wed Jun 06, 2007 2:23 pm

Hi wanginlife,

Actually I've to admit I've never tried to use PostSharp with web projects. But it should work, and if it does not, it is a bug.

The best is that you send me your zipped test application (if possible). One of the options is to upload it on [url=ftp://ftp.postsharp.org/upload]ftp://ftp.postsharp.org/upload[/url] or to attach it to a forum post.

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Using post sharp with web projects?

Postby dawginlife on Wed Jun 06, 2007 3:14 pm


having problem uploading the solution.  but it only consist of two projects with a file in each.  i'll paste the content for you to look over.
*******WEB SERVICE CODE BEHIND********


using
System;
using
System.Web;
using
System.Collections;
using
System.Collections.Generic;
using
System.Web.Services;
using
System.Web.Services.Protocols;
using
System.Threading;
///
<summary> Summary description for PermService
///
</summary>
[
WebService(Namespace = "PostSharpTest")]WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public
class AdminService : System.Web.Services.WebService
{

public AdminService()//Uncomment the following line if using designed components

//InitializeComponent();
}
[
WebMethod]Required]public string Test()return new PsTester().ReturnString();
public
class PsTester
{
[
Required]public string ReturnString()return "PsTester.ReturnString()"; System; System.Collections.Generic; System.Text; System.Reflection; PostSharp.Laos; PostSharp.Extensibility;Serializable] sealed class RequiredAttribute : OnMethodBoundaryAspect/// <summary>/// Initializes the current object. Called at compile time by PostSharp./// </summary>/// <param name="field">Field to which the current instance is/// associated.</param>public override void CompileTimeInitialize(MethodBase method)/// <summary>/// Method executed <b>before</b> the body of methods to which this aspect is applied./// We just trace and increment indentation./// </summary>/// <param name="eventArgs">Event arguments specifying which method/// is being executed and which are its arguments.</param>public override void OnEntry(MethodExecutionEventArgs context)//context.FlowBehavior = FlowBehavior.Return;/// <summary>/// Method executed <b>after</b> the body of methods /// to which this aspect is applied, when the method succeeds./// </summary>/// <param name="eventArgs">Event arguments specifying which method/// is being executed and which are its arguments.</param>public override void OnSuccess(MethodExecutionEventArgs eventArgs)/// <summary>/// Method executed <b>after</b> the body of methods /// to which this aspect is applied, when the method ends with an exception./// </summary>/// <param name="eventArgs">Event arguments specifying which method/// is being executed and which are its arguments.</param>public override void OnException(MethodExecutionEventArgs eventArgs)
dawginlife
 
Posts: 6
Joined: Sat Jan 12, 2008 4:55 pm

Re: Using post sharp with web projects?

Postby dawginlife on Wed Jun 06, 2007 3:16 pm


Second Try.

having problem uploading the solution.  but it only consist of two projects with a file in each.  i'll paste the content for you to look over.
*******WEB SERVICE CODE BEHIND********

using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Threading;
/// <summary>
/// Summary description for PermService
/// </summary>
[WebService(Namespace = "PostSharpTest")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AdminService : System.Web.Services.WebService
{
public AdminService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[Required]
public string Test()
{
return new PsTester().ReturnString();
}
}
public class PsTester
{
[Required]
public string ReturnString()
{
return "PsTester.ReturnString()";
}
}



********CUSTOM ATTRIBUTE*******


using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using PostSharp.Laos;
using PostSharp.Extensibility;
 
[Serializable]
public sealed class RequiredAttribute : OnMethodBoundaryAspect
{
/// <summary>
/// Initializes the current object. Called at compile time by PostSharp.
/// </summary>
/// <param name="field">Field to which the current instance is
/// associated.</param>
public override void CompileTimeInitialize(MethodBase method)
{
}
/// <summary>
/// Method executed <b>before</b> the body of methods to which this aspect is applied.
/// We just trace and increment indentation.
/// </summary>
/// <param name="eventArgs">Event arguments specifying which method
/// is being executed and which are its arguments.</param>
public override void OnEntry(MethodExecutionEventArgs context)
{
//context.FlowBehavior = FlowBehavior.Return;
}
/// <summary>
/// Method executed <b>after</b> the body of methods
/// to which this aspect is applied, when the method succeeds.
/// </summary>
/// <param name="eventArgs">Event arguments specifying which method
/// is being executed and which are its arguments.</param>
public override void OnSuccess(MethodExecutionEventArgs eventArgs)
{
}
/// <summary>
/// Method executed <b>after</b> the body of methods
/// to which this aspect is applied, when the method ends with an exception.
/// </summary>
/// <param name="eventArgs">Event arguments specifying which method
/// is being executed and which are its arguments.</param>
public override void OnException(MethodExecutionEventArgs eventArgs)
{
}
 
}
dawginlife
 
Posts: 6
Joined: Sat Jan 12, 2008 4:55 pm

Re: Using post sharp with web projects?

Postby gfraiteur on Wed Jun 06, 2007 6:37 pm

Sorry, the address was [url=ftp://www.postsharp.org/upload/.]ftp://www.postsharp.org/upload/.[/url]

Really, please upload the projects and describe the problem because it takes a stupid time to recompose the projects and guess what is wrong.

Thank you.
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Using post sharp with web projects?

Postby dawginlife on Wed Jun 06, 2007 7:19 pm

no problem, i understand.

The problem seems to be that the PostSharp does not get invoked when building an asp.net project.

I've uploaded the file is called TestPostSharp.zip.

when you start the web project, the web service will provide you with two method: HelloWorld and SaySomethingSmart.  Both will return the greetings "Hello World".

The problem is the HelloWorld method does not invoke the PostSharp attribute that was created in the asp.net project.  When running in debug mode, the breakpoints in this attribute "PermRequired" does not get triggered.

Method SaySomethingSmart invokes a method from a none asp.net project  decorated with  an attribute, "Smart" that does get triggered.

dawginlife
 
Posts: 6
Joined: Sat Jan 12, 2008 4:55 pm

Re: Using post sharp with web projects?

Postby gfraiteur on Wed Jun 06, 2007 9:05 pm

Excellent point.
The reason is that the build process bypasses the normal MSBuild process, where PostSharp is inserted.
I will have to look at this in details, it can take a while.

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Using post sharp with web projects?

Postby dawginlife on Wed Jun 06, 2007 10:03 pm

It's not a pressing issue.  I just wanted to know if i needed to reorganize my project to use PostSharp or wait for the fix.  Knowing that it could take sometime, I'll just go on to plan B.  FYI, PostSharp/Laos rocks; it's a lot better than the other .NET AOP frameworks i've looked at.  Thanks for creating this great tool.
dawginlife
 
Posts: 6
Joined: Sat Jan 12, 2008 4:55 pm

Re: Using post sharp with web projects?

Postby gfraiteur on Thu Jun 07, 2007 7:04 am

It seems that ASP.NET compiles using CodeDom. For instance, C# files are built using Microsoft.CSharp.CSharpCodeProvider. A solution would be to "subclass" these providers to execute PostSharp at the end. I think it's feasible, but

1) It's too big to put this in scope of the version 1.0.
2) This would quite significantly slow down the process of starting up the web server, since PostSharp is many times slower than the C# compiler itself.

But if you are brave go for it :)

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Using post sharp with web projects?

Postby dawginlife on Thu Jun 07, 2007 12:53 pm

This does not sound like a simple task; and i've not done any work with IL.  (Are there any good resources for getting up to speed with IL?) as such, i'm unlikely to tackle this unless i'm provided with a good supply of painkillers.

Thanks for the update.
dawginlife
 
Posts: 6
Joined: Sat Jan 12, 2008 4:55 pm

Re: Using post sharp with web projects?

Postby gfraiteur on Thu Jun 07, 2007 2:04 pm

Don't be afraid, there is not a single drop of IL in this task. It's just about adding a PostSharp step after CSharpCodeProvider (develop what is called a 'PostSharp Host').

I've learnt IL from the specs. Another good way to learn is to execute the sample PostSharp.Samples.Explorer and see how a .NET assembly looks like inside.
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Using post sharp with web projects?

Postby JonCubed on Wed Sep 26, 2007 7:05 am


Gael can you explain it a bit more? What would I be required to do to add it to a VB web program?
Jonathan Kuleff
JonCubed
 
Posts: 1
Joined: Sat Jan 12, 2008 4:55 pm
First Name: Jonathan
Last Name: Kuleff

Re: Using post sharp with web projects?

Postby gfraiteur on Wed Sep 26, 2007 9:35 am

It would be needed to create its own code provider that calls the VB code provider for most of the job, but then invokes PostSharp. For information about invoking PostSharp, see the projects PostSharp.Console or PostSharp.MSBuild.

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Using post sharp with web projects?

Postby louis210 on Tue Feb 26, 2008 10:09 pm

Gael,

I've read the various blogs, FAQs, and forums on postsharp.org and have spent most of the past two days scouring the internet trying to figure out how I can use PostSharp to integrate tracing calls into methods on .aspx pages.

Using your demonstration video as well as various tutorials, I've created a very nice tracing attribute to work with log4net that works perfectly. I understand that there may be no "automated" way right now for PostSharp to be part of the compilation process when an aspx page is changed, but is it at all possible to tell VS (or use MSBuild) to manually recompile the web pages using PostSharp?

Merci beaucoup,
Louis
louis210
 
Posts: 7
Joined: Tue Feb 26, 2008 9:55 pm
Last Name: Louis

Re: Using post sharp with web projects?

Postby gfraiteur on Tue Feb 26, 2008 10:12 pm

I guess you can pre-compile the website and PostSharp it using the command-line utility. See http://www.postsharp.org/blog/2008/01/u ... ation.html. I ignore if it is possible to automate this step in Visual Studio. Any feedback would be welcome.

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Using post sharp with web projects?

Postby louis210 on Tue Feb 26, 2008 11:30 pm

I've successfully precompiled the website such that I now have a bin directory with
  • App_Code.dll
  • App_global.asax.dll
  • a series of files that look like
    App_Web_[original aspx filename].aspx.[8-digit hex code].dll
  • (one for each original aspx page)
  • App_Web_site.master.[8-digit hex code].dll
So far, so good.

However, it looks like the command-line parameters for PostSharp.exe require a project name such as *.csproj. Of course, a web app doesn't have such a file; it's just a bunch of pages sitting in a directory.

So, let's say that I want to PostSharp one of the DLLs, App_Web_default.aspx.[8-digit hex code].dll. What would be the appropriate way to call PostSharp.exe without a project file?
louis210
 
Posts: 7
Joined: Tue Feb 26, 2008 9:55 pm
Last Name: Louis

Re: Using post sharp with web projects?

Postby gfraiteur on Wed Feb 27, 2008 8:33 am

You should use the default project file.

Please, read the blog item I referred to.

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Using post sharp with web projects?

Postby louis210 on Wed Feb 27, 2008 5:39 pm

My apologies...I misunderstood the purpose of the Default.psproj file. I should have caught that. I guess I needed more coffee!

I've got it working now and have written a couple batch files to speed up / automate the steps.

Where would be a good spot to post a generic walkthrough of how to process web pages with PostSharp as well as the text of the batch files?

Thank you again,
Louis
louis210
 
Posts: 7
Joined: Tue Feb 26, 2008 9:55 pm
Last Name: Louis

Re: Using post sharp with web projects?

Postby gfraiteur on Wed Feb 27, 2008 5:47 pm

I suggest you have a look at the interface IAssemblyPostProcessor. I will try to base a generic solution base on that.

It's quite work to make a generic and deployable solution, but it is pretty easy to make an ad-hoc workaround.

Let me know how it'll result.

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Using post sharp with web projects?

Postby gfraiteur on Wed Feb 27, 2008 10:30 pm

It was easier than expected.
You can download a first version of the code from http://postsharp-user-plugins.googlecod ... rp.AspNet/ (using an SVN client).

I will document and blog about this once the code will cleaner and more stable.

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 834
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Next

Return to Integration with IDE and Build Scripts