【问题标题】:Implementing PostSharp with Asmx Web Service使用 Asmx Web 服务实现 PostSharp
【发布时间】:2018-02-14 02:06:43
【问题描述】:

我目前正在尝试将 postsharp 与我的 asmx Web 服务集成以记录异常。

这是我的 Aspect 透视代码:

[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);
    }
}

在我的 Service.cs 类中,我有以下 Web 方法:

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

直奔主题:

  • postsharp 是否支持使用 Web 方法实现?和我的情况一样, 每当 web 方法 接收到 postSharp 方法时都不会被调用 一击。 (是的,我已经使用 Nuget 添加了 postsharp 参考和/或/加上手动添加了它的 dll)This 确实 建议朝着上述主题迈出一步,但我什么也做不了 出它。

需要注意的是,相同的 LogPerformance 类在与以下组件集成时可以顺利运行:

  • 网络 API
  • ASP.Net Web 应用程序 (MVC)
  • 控制台应用程序

问题是当我将它与 .asmx Web 服务一起使用时。向正确的方向轻推一点将不胜感激。

【问题讨论】:

  • 首先,您需要确保在构建项目时执行 PostSharp 编译器。您的项目必须有一个 *.csproj 项目文件,并且该项目文件必须包含带有 PostSharp.targets 的 Import 行。在您的构建输出中,您应该会看到至少一条来自 PostSharp 的消息。
  • @alexD 感谢您的回复,我刚刚检查并确认我的解决方案没有 .csproj 文件。其次,在我的构建输出中,我没有看到来自 post sharp 的任何消息。您能否确认您的产品支持 .asmx 服务?
  • 支持 .asmx,但仅限于带有 *.csproj 文件的 Web 应用程序项目。您需要将您的网站转换为 Web 应用程序,例如查看这篇博文 (blogs.msdn.microsoft.com/webdev/2009/10/29/…)。转换后,您需要将 PostSharp NuGet 包安装到您的项目中。
  • @AlexD 再次感谢您,您的方法对我有效。
  • 太棒了!我已经为参考添加了完整的答案。

标签: c# asp.net web-services asmx postsharp


【解决方案1】:

PostSharp 支持 *.asmx 网络服务。但是,您需要注意您的 ASP.NET 项目是 Web 站点 还是 Web 应用程序 (ASP.NET Web Site or ASP.NET Web Application?)。 PostSharp 仅支持 Web 应用程序 项目。有关兼容性的更多信息,您还可以查看Requirements and Compatibility

您可以按照博文Converting a Web Site Project to a Web Application Project 中的指南将您的网站 项目转换为Web 应用程序 项目。转换后,您需要将 PostSharp NuGet 包安装到您的项目中。

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 2013-06-03
    • 2013-03-27
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    相关资源
    最近更新 更多