views:

22

answers:

2

Hi all. 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;
        }
+1  A: 

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.

HTH,
Kent

Kent Boogaart
OK, I'll explain this.There are several clients (they all have assemblies for sure).The service currently has methods for ALL clients. I want to be able to compartmentalize the service by identifying the client which requests the service. There's no possibility to split the service into several little ones, although that might be the better choice. For instance, let's say Method A should only be available to clients B and C. How do I ensure that client D will be unable to use it?
Gena Verdel
That's what authentication and authorization is used for. For example, you can demarcate services as only accessible to authenticated clients with a given role or claim.
Kent Boogaart
Can you please provide me with any sample?
Gena Verdel
A: 

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.

marc_s