获取 WCF 服务内的调用程序集名称
我正在努力实现以下目标: 每当执行服务调用时,我希望能够识别客户端。 我考虑过通过迭代堆栈跟踪来获取调用程序集名称 但我无法获取客户端程序集名称。 示例代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一定错过了一些东西:您试图通过程序集识别客户端?为什么不使用身份验证?
此外,谁说客户端甚至有程序集?它可能是 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.
当您的客户端调用 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.