2

I'm currently trying to integrate postsharp with my asmx web service for the purpose of logging exceptions.

Here's my code for the Aspect perspective:

[Serializable]
public class LogPerformance : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
        string test = "test";
        base.OnEntry(args);
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        string test = "test";
        base.OnExit(args);
    }

    public override void OnException(MethodExecutionArgs args)
    {
        string test = "test";
        base.OnException(args);
    }
}

while in my Service.cs class, i've the following web method:

[WebMethod(EnableSession = true)]
[SoapHeader("authentication")]
[LogPerformance]
public DataTable loginUser(string userName, string password)
{
    doStuff();
}

Coming straight to the point:

  • Does postsharp support implementation with web methods? As in my case, postSharp methods does not get called whenever the web method receives a hit. (Yes i've added postsharp reference using Nuget and/or/plus manually added its dll as well) This does suggest a step towards the mentioned subject but i could not make anything out of it.

It is important to note that the same LogPerformance Class runs smoothly when integrated with:

  • Web API
  • ASP.Net Web Application (MVC)
  • Console Application

The problem is when i use it with .asmx web service. A little nudge towards the right direction would be appreciated.

Aimal Khan
  • 1,009
  • 1
  • 12
  • 25
  • First of all you need to make sure that PostSharp compiler executes when you build your project. Your project must have a *.csproj project file and this project file must contain an Import line with PostSharp.targets. In your build output you should see at least one message from PostSharp. – AlexD Sep 10 '17 at 21:34
  • @alexD Thank you for your response, i just checked and its verified that my solution does not have .csproj file. Secondly in my build output, i do not see any message from post sharp. Can you please confirm of your product's support for .asmx services? – Aimal Khan Sep 11 '17 at 15:05
  • The .asmx are supported, but only in web application projects with *.csproj file.You need to convert your web site to web application, look for example at this blog post (https://blogs.msdn.microsoft.com/webdev/2009/10/29/converting-a-web-site-project-to-a-web-application-project/). After conversion you need to install PostSharp NuGet package into your project. – AlexD Sep 11 '17 at 15:11
  • @AlexD Thank you again, your method worked in my case. – Aimal Khan Sep 11 '17 at 16:52
  • Great! I've added the complete answer for the reference. – AlexD Sep 12 '17 at 08:51

1 Answers1

1

The *.asmx web-services are supported by PostSharp. However, you need to pay attention whether your ASP.NET project is a Web Site or a Web Application (ASP.NET Web Site or ASP.NET Web Application?). Only Web Application projects are supported by PostSharp. For more information on compatibility you can also check Requirements and Compatibility.

You can convert your Web Site project to Web Application project by following the guidelines from the blog post Converting a Web Site Project to a Web Application Project. After the conversion you need to install PostSharp NuGet package into your project.

AlexD
  • 5,011
  • 2
  • 23
  • 34