如何识别代码是否在Web服务内部运行?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您的具体要求,但似乎 plain dll 是一个标准业务逻辑库。根据我的经验,我建议让业务逻辑尽可能与实现无关(当然在合理范围内),因为无论如何您可能会以不同的方式处理异常。通过根据实现抛出不同的异常,您将把业务逻辑的职责与实现者的职责混合在一起。
我的建议是从业务逻辑库中抛出一组常见的异常,并针对每个实现以不同的方式捕获/处理它们。例如,控制台应用程序可能只是再次请求输入,而 WCF 应用程序可能会抛出错误异常。
以下面的代码为例:
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:
很公平。在这种情况下,您可以检查库中的各种上下文。
世界碳纤维
网
Fair enough. In that case you could check various contexts in the library.
WCF
Web