Common WCF parameters ClientMessageInspector, DispatchMessageInspector or alternative?

I use WCF only for data services (i.e. internal application and very bad, without session state, etc.) to maintain scalability of our web application.

We need to provide some common properties for every service call that we currently go through all the time. The presence of single request objects for each call is not ideal, because in addition to these common properties, the others will remain very diverse and change quite often during development.

I'm currently considering using custom headers and a clientmessageinspector to set values. Is this the easiest recommended approach for this scenario or is there a better approach?

Read more ..

The red dots below are where I’m not sure about the right approach (or how to do it).

enter image description here

What is sent

The transmitted data is a simple set of identifiers (3 or 4 for userid, clientid, etc.). All of these identifiers affect security and performance (in some cases, it determines which database to go to).

We will also expand this to have more complex permissions - not needed for working windows.

The caller will either be a web application in which they exit the session object, or a Windows service employee, where they are manually populated.

Modern thinking

Ideally, getinstance in the caller’s workflow will automatically populate these properties with a session object or more manually using Windows service calls (different constructors?).

Then we would guarantee that these parameters are always available without any thoughts or without permanent links in the entire code in order to build a contract for each function that calls it. We currently have a lot of service calls (due to the size / complexity of the application, and not due to poor engineering :)), since this applies to complex permissions, it becomes a little difficult to enforce the rules in a self-documenting way.

Conceptually, a session is where you take care of this in the application, but services are just a level of access to data (with display mapping, page browsing and last call checking from repository calls), so we don’t need the kind of repetition or complexity, Only key identifiers and permission fields to include in queries.

Problem

This is very similar to what we should do with the call headers, since we always need these fields, but I'm a little unsure where the set and get should sit on the life cycle of the endpoint and the client interface. I am also glad to make a mistake in this.

+8
c # soa wcf wcf-4
source share
3 answers

I applied a similar architecture; basically, each client call should have some information about which database should be selected, identifier, etc. And on the server side, these parameters should be processed automatically and stored in the dictionary.

I created a generic proxy class for migrating client proxies to add related headers to each service call. Every developer who needs to call a service has used this common proxy class in their calls.

On the service side, I implemented DispatchMessageInspector as an endpoint behavior where data is extracted from request headers and stored in a dictionary. The dictionary is initialized inside the OperationContext extension ( IExtension<OperationContext> ) and is available during request processing.

Note that the context mode of services is an instance of PerCall .

+1
source share

In my experience, using message inspectors can be quite complicated for initial setup and configuration, and in my research there was no site that covered everything, I had to select fragments from several different places and combine all this together.

You need to ask a question about what you put in headings. Is this information that should be available for the called method? If this is the case, then putting it in the headers is the wrong option, since each method will have to analyze the information back.

The type of information that is ideal for this is the custom authentication and / or user metadata associated with the WCF call. In my case, I had a WCF call initiated by an automatic service that was transferred to other WCF endpoints, this was an ideal scenario for using message inspectors, since I managed to add metadata to the headers at the relay points for consumption by the subsequent endpoint.

If you just want to pack some data common to each call, I would enable the creation of a basic data object with the appropriate properties, and then just expand it for more specialized calls (the endpoint can ensure that the shared data is either present or accept some default values if it is not). For the common data required for each endpoint using message inspectors, redundant and potentially non-viable.

+3
source share

This may be an older approach, but you can easily use CallContext from System.Runtime.Remoting.Messaging . The client can use the implementation of IClientMessageInspector to set the call context when invoking the operation and implement the IMessageInspector on the server to obtain common data from CallContext .

+1
source share

All Articles