获取 WCF 服务内的调用程序集名称

发布于 2024-09-07 00:46:35 字数 1080 浏览 4 评论 0原文

我正在努力实现以下目标: 每当执行服务调用时,我希望能够识别客户端。 我考虑过通过迭代堆栈跟踪来获取调用程序集名称 但我无法获取客户端程序集名称。 示例代码:

private List<System.Reflection.Assembly> GetCallingAssemblies()
        {

            List<System.Reflection.Assembly> assemblies = new List<System.Reflection.Assembly>();

            StackTrace stackTrace = new StackTrace(0, true);

            for (int i = 0; i < stackTrace.FrameCount; i++)
            {

                StackFrame stackFrame = stackTrace.GetFrame(i);

                System.Reflection.MethodBase methodBase = stackFrame.GetMethod();

                Type type = methodBase.ReflectedType;
                System.Reflection.Assembly assembly;
                if (type != null)
                {
                    assembly = System.Reflection.Assembly.GetAssembly(type);
                    if (assemblies.Contains(assembly) == false)
                    {
                        assemblies.Add(assembly);
                    }   
                }                 
            }
            return assemblies;
        }

I'm trying to achieve the following:
whenever a call to service is performed I want to be able to identify the client.
I thought about getting the calling assembly name by iterating over stack trace
but I failed to get the client assembly name.
Sample code:

private List<System.Reflection.Assembly> GetCallingAssemblies()
        {

            List<System.Reflection.Assembly> assemblies = new List<System.Reflection.Assembly>();

            StackTrace stackTrace = new StackTrace(0, true);

            for (int i = 0; i < stackTrace.FrameCount; i++)
            {

                StackFrame stackFrame = stackTrace.GetFrame(i);

                System.Reflection.MethodBase methodBase = stackFrame.GetMethod();

                Type type = methodBase.ReflectedType;
                System.Reflection.Assembly assembly;
                if (type != null)
                {
                    assembly = System.Reflection.Assembly.GetAssembly(type);
                    if (assemblies.Contains(assembly) == false)
                    {
                        assemblies.Add(assembly);
                    }   
                }                 
            }
            return assemblies;
        }

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

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

发布评论

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

评论(2

桃气十足 2024-09-14 00:46:35

我一定错过了一些东西:您试图通过程序集识别客户端?为什么不使用身份验证?

此外,谁说客户端甚至有程序集?它可能是 Java 客户端,或其他一些平台。

I must be missing something: you're trying to identify the client through assemblies? Why not use authentication?

Besides, who says the client even has assemblies? It may be a Java client, or some other platform.

情栀口红 2024-09-14 00:46:35

当您的客户端调用 WCF 服务时,两者之间传递的只是序列化消息 - 要调用的方法和要传入的所有参数。

在运行时,服务器和客户端之间没有其他连接。服务器无法“返回”并查看客户端 - 没有连接

您的服务可以查看的只是序列化消息和任何消息标头。因此,如果您确实需要这个(您需要它做什么??),那么您需要确保客户端将某种标记/标识作为消息头放入调用中。

When your client calls a WCF service, all that goes between the two is the serialized message - the method to call and all the parameters to pass in.

There is no other connection at runtime between server and client. The server cannot "reach back" and look at the client - there is no connection.

All your service can look at is the serialized message, and any message headers. So if you really really need this (what do you need it for??) then you need to make sure the client puts a marker / identification of some sort as a message header into the call.

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