【问题标题】:How to identify if code runs inside the webservice?如何识别代码是否在 Web 服务内运行?
【发布时间】:2011-10-12 15:43:37
【问题描述】:

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

谢谢

【问题讨论】:

  • 需要更多信息。你有示例代码吗,如果这是一个闭源组件,你有示例客户端代码吗?
  • 能否使用 OperationContext 类找出调用 WCF 服务的机器的 IP 地址,看看是否是您的 Web 服务器的地址?
  • 嗯,很简单。我有一个公共类 Service:IService{},其中 IService 被定义为 ServiceContract。服务可以通过 web 服务中的端点公开,或者只是通过引用应用程序中的 dll 来访问。
  • DLL 本身是否也包含 WCF 宿主代码,或者 DLL 是否只是由 WCF 宿主加载,以防它作为 WebService 公开?这种情况下的主机是什么(IIS...)?

标签: .net dll service web


【解决方案1】:

我不确定您的具体要求,但似乎 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();
            }
        }
    }
}

【讨论】:

  • 是的,这是一种常见的方法,但不幸的是在当前情况下不是一个选项。
【解决方案2】:

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

WCF

bool isWcf = OperationContext.Current != null;

网络

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

【讨论】:

  • 这可能是答案,我会在尝试后回复。
猜你喜欢
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多