如何识别代码是否在Web服务内部运行?

发布于 2024-12-10 01:57:22 字数 103 浏览 0 评论 0原文

我有一个 WCF 服务契约实现,它可以用作普通 dll 或 Web 服务。有什么方法可以(从其代码中)识别它是如何使用的。 更具体地说,我需要在这些情况下抛出不同的异常。

谢谢

I have a WCF service contract implementation which can be either used as a plain dll or as a web service. Is there any way to identify (from within its code) how is it used.
More specifically, I need to throw different exceptions in those cases.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

開玄 2024-12-17 01:57:22

我不确定您的具体要求,但似乎 plain dll 是一个标准业务逻辑库。根据我的经验,我建议让业务逻辑尽可能与实现无关(当然在合理范围内),因为无论如何您可能会以不同的方式处理异常。通过根据实现抛出不同的异常,您将把业务逻辑的职责与实现者的职责混合在一起。

我的建议是从业务逻辑库中抛出一组常见的异常,并针对每个实现以不同的方式捕获/处理它们。例如,控制台应用程序可能只是再次请求输入,而 WCF 应用程序可能会抛出错误异常。

以下面的代码为例:

// Simple business logic that throws common exceptions
namespace BusinessLogicLibrary
{
    public class Math
    {
        public static int Divide(int dividend, int divisor)
        {
            if (divisor == 0)
                throw new DivideByZeroException();

            return dividend / divisor;
        }
    }
}

// WCF calls to business logic and handles the exception differently
namespace WcfProject
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        int Divide(int dividend, int divisor);
    }

    public class Service : IService
    {
        public int Divide(int dividend, int divisor)
        {
            try
            {
                return BusinessLogicLibrary.Math.Divide(dividend, divisor);
            }
            catch (Exception ex)
            {
                throw new FaultException(
                    new FaultReason(ex.Message),
                    new FaultCode("Division Error"));
            }
        }
    }
}

// Console application calls library directly and handles the exception differently
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowDivide();
        }

        static void ShowDivide()
        {
            try
            {
                Console.WriteLine("Enter the dividend: ");
                int dividend = int.Parse(Console.ReadLine());

                Console.WriteLine("Enter the divisor: ");
                int divisor = int.Parse(Console.ReadLine());

                int result = BusinessLogicLibrary.Math.Divide(dividend, divisor);
                Console.WriteLine("Result: {0}", result);
            }
            catch (DivideByZeroException)
            {
                // error occurred but we can ask the user again
                Console.WriteLine("Cannot divide by zero. Please retry.");
                ShowDivide();
            }
        }
    }
}

I'm unsure of your specific requirements, but it seems like plain dll is a standard business logic library. Based on my experience, I'd suggest leaving the business logic as implementation agnostic as possible (within reason, of course) as you'll likely be handling the exceptions differently anyway. By throwing different exceptions based on the implementation, you'll be mixing the responsiblities of the business logic with those of the implementor.

My suggestion would be to throw a common set of exceptions from the business logic library, and catch/handle them differently for each implementation. E.g. a console application may just ask for the input again, where as a WCF application may throw a fault exception.

Take the following code as example:

// Simple business logic that throws common exceptions
namespace BusinessLogicLibrary
{
    public class Math
    {
        public static int Divide(int dividend, int divisor)
        {
            if (divisor == 0)
                throw new DivideByZeroException();

            return dividend / divisor;
        }
    }
}

// WCF calls to business logic and handles the exception differently
namespace WcfProject
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        int Divide(int dividend, int divisor);
    }

    public class Service : IService
    {
        public int Divide(int dividend, int divisor)
        {
            try
            {
                return BusinessLogicLibrary.Math.Divide(dividend, divisor);
            }
            catch (Exception ex)
            {
                throw new FaultException(
                    new FaultReason(ex.Message),
                    new FaultCode("Division Error"));
            }
        }
    }
}

// Console application calls library directly and handles the exception differently
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowDivide();
        }

        static void ShowDivide()
        {
            try
            {
                Console.WriteLine("Enter the dividend: ");
                int dividend = int.Parse(Console.ReadLine());

                Console.WriteLine("Enter the divisor: ");
                int divisor = int.Parse(Console.ReadLine());

                int result = BusinessLogicLibrary.Math.Divide(dividend, divisor);
                Console.WriteLine("Result: {0}", result);
            }
            catch (DivideByZeroException)
            {
                // error occurred but we can ask the user again
                Console.WriteLine("Cannot divide by zero. Please retry.");
                ShowDivide();
            }
        }
    }
}
小红帽 2024-12-17 01:57:22

很公平。在这种情况下,您可以检查库中的各种上下文。

世界碳纤维

bool isWcf = OperationContext.Current != null;

bool isWeb = System.Web.HttpContext.Current != null;

Fair enough. In that case you could check various contexts in the library.

WCF

bool isWcf = OperationContext.Current != null;

Web

bool isWeb = System.Web.HttpContext.Current != null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文