简体   繁体   English

有没有可能知道which.dll调用了C#服务? 即使调用先经过一个COM层?

[英]Is it possible to know which .dll called a C# service? Even if the call passes through a COM layer first?

We have layered architecture that has a native C++ client, and a managed client C++/C# wrapper around it.我们有一个分层架构,它有一个本地 C++ 客户端,以及一个围绕它的托管客户端 C++/C# 包装器。 (2 dll files, one native one managed) (2 个 dll 个文件,一个本地一个管理)

These two clients go through a COM layer to a C# service, there are many different versions of these clients and the service is designed to be backward compatible.这两个客户端 go 通过一个 COM 层到一个 C# 服务,这些客户端有很多不同的版本并且服务被设计成向后兼容。

Recently we realized we might need to know the version of the client calling into the service.最近我们意识到我们可能需要知道调用服务的客户端的版本。 (the dll files) (dll 个文件)
Is there a way to go through the call stack and grab the dll that called in and its version?有没有办法通过调用堆栈到 go 并获取调用的 dll 及其版本? Does going through the COM layer make this an impossible task?遍历 COM 层会使这成为一项不可能完成的任务吗?

Other products consume and deliver these clients, so the location or path of the dlls is not always known.其他产品使用并交付这些客户端,因此并不总是知道 dll 的位置或路径。 Getting the location through the call stack might be an alternative route to the solution, but I might be thinking about this entirely incorrectly.通过调用堆栈获取位置可能是解决方案的替代途径,但我可能完全错误地考虑了这一点。

We can't edit existing released client dlls, so we have to be able to determine the version of the dlls from the service layer at runtime.我们无法编辑现有已发布的客户端 dll,因此我们必须能够在运行时从服务层确定 dll 的版本。

It is possible to capture a stacktrace , step thru the various stack frames, get the methodinfo for each, get the declaring type, and find the assembly for that type.可以捕获堆栈跟踪,遍历各种堆栈帧,获取每个堆栈帧的方法信息,获取声明类型,并找到该类型的程序集。 This however is rather cumbersome, and would be slow since it would use a lot of reflection.然而,这相当麻烦,而且会很慢,因为它会使用大量反射。 I'm not sure if COM would affect this, but I suspect it should not, as long as the call does not jump between threads.我不确定 COM 是否会影响这个,但我怀疑它不应该,只要调用不在线程之间跳转。

This would also be rather poor practice since the method call depend on information that is not obvious for the caller.这也是一种相当糟糕的做法,因为方法调用依赖于对调用者来说并不明显的信息。 As such it can be very confusing for new developers, and may be fragile.因此,对于新开发人员来说,它可能会非常混乱,并且可能很脆弱。

Perhaps it would be sufficient to check what dlls are loaded, rather than what version made the call?也许检查加载了哪些 dll 就足够了,而不是调用哪个版本?

I would recommend making your service work correctly regardless of client if at all possible, possibly adding information to the call if needed.我建议尽可能让您的服务正常工作,而不管客户端如何,如果需要,可能会向呼叫添加信息。 Another approach is to specify multiple versions of the service, and let the client call the version it has been developed with.另一种方法是指定服务的多个版本,让客户端调用它开发时使用的版本。 Third approach would be to explicitly inform the service about the client version, and if this is not done, assume some specific client version.第三种方法是明确通知服务有关客户端版本的信息,如果未这样做,则假定某个特定的客户端版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM