WCF Custom Service Behaviour - c#

Context: I am hosting a number of services using WCF. I'd like for each of them to support a Ping operation (heartbeat, keep-alive, whatever...). But, I'd rather not go implement IPingable on each one. Instead, I'd love to do something like the serviceMetadata behavior does and dynamically add a channel dispatcher. This would mean all I'd need to do is add a pingable behvaior to the services and this operation is supported.
Any ideas? I've looked through the the source code of serviceMetadata and there is all kinds of internal stuff being called I don't have access to. Also, there may be an easier way.
Thanks for your time.

If you do it through a behavior, will your metadata indicate that the Ping operation exists?

Just off the top of my head could you add a dispatch behaviour that would handle the call and spoof the return true/false?
I also found the following

Related

Invoking WCF IServiceBehavior and IOperationBehavior without calling a method

I'd like to be able to add a couple of behaviors without having to call a method forcing them to be to used. A typical example is via an [InvokeErrorSupport] attribute whose purpose would be to fire off a test e-mail when deploying a service to ensure the error e-mails are coming through okay. Thus saving magic strings in a request Parameter Object, one or more non-business logic related [OperationContract] methods etc dirtying up the contract (with single responsibility in mind). We are more than happy to potentially invoke a method in other case such as our [Heartbeat] behavior and similar.
I have no problem writing the behaviors. It's a great feature of WCF but right now its looking like I'm going to have to add some method to the contract which I call such as Initialize which I'd lock down after start-up. Since, in this instance and more, the services are often external facing so we wish to avoid DoS attacks etc.
I've poked about client-side and can't see anyway and to be honest it kinda makes a degree of sense this functionality doesn't exist.
Can anybody offer any advice?

Peek on MSMQIntegrationBinding

I am attempting to bind to a non-transactional MSMQ queue using MSMQIntegrationBinding. What I want to be able to do is to peek at the message, and then if conditions are right, go ahead and process it. But, if conditions are not right, I want to leave the message on the queue.
I came upon ReceiveContextEnabled=True, but have seen very little documentation or tutorials on how to actually use this. I am hosting the WCF service library through a Windows Service. However, when I open my host, I get an error like:
The contract IWMInTranslator_Service_MSMQ has at least one operation annotated with ReceiveContextEnabledAttribute, but the binding used for the contract endpoint at address msmq.formatname:DIRECT=OS:CCNU404CCH5%5Cprivate$%5Cwmintranslateque does not support required binding property 'IReceiveContextSettings'. Please ensure that the binding used for the contract supports the ReceiveContext capability.
If I change it to a transactional queue it seems to work, but I dont want to do this. Can anybody help? I create my endpoints and bindings via code (not through app.config). If there are some properties somewhere I can change that would be great!
Thanks,
:) David

How to encapsulate code for WCF services without using a base class?

All of our services take a ServiceCallContext object as a parameter. The service then creates a broker and tells the broker what connection string to use based on the ServiceCallContext.
In other words, some of our customers have their own databases so the service calls have to point the brokers to their databases.
I would like to take the code that looks at the ServiceCallContext and chooses the correct connection and put it in a base service class. My team lead doesn't like that idea because with services he feels that this would be 'hiding' behavior and that this would be a bad thing. He suggested that there may be better ways to accomplish the same thing through some sort of WCF extensions.
I honestly don't care how we implement the code so long as I can reuse it because I think it's absolutely silly for me to be rewriting it in every service I create. I began looking into some WCF videos on PluralSight and it looks like there's a lot of great stuff it can do but unfortunately I'm not quite sure where to start. Can anyone give me a little direction as to whether WCF can accomplish what I'm trying to do and if so what particular features of WCF am I looking for?
The functionality you need is a custom interceptor.
This allows you to tell the WCF stack to look at incoming messages and the do some action based on them. If you wrap the interceptor up into it's own assembly then you can reference it from multiple services.

How to use WCF fault contract globally without having to specify at operation level?

I need to return a fault of specific type from a WCF service so that the client can catch FaultException<MyClass>. In order to do this, as far as I know, only way out is to apply the FaultContract(typeof(MyClass)) at the operation level. Is there any other way to do this at a global level? One place where I can add this contract to all operations in all services?
Unfortunately you need to add it at every operation, however you can use tools like PostSharp that will re-write your code on compile so you could create an attribute that you put on a contract and have PostSharp add the correct FaultContract attribute on each OperationContract.
Yea...I wish we can just globally define the default FaultException for the service level, then override for specific messages. I'm on a WSDL first project and the WCF WSDLs come out looking FUGLY!

raising events from wcf service server

I have a very simple wcf server. When a client uses an "operation" contract, I want to log this in a log file.
I came up with two solutions for this but I doubt they are good solutions and need some hints from the experts here.
My first solution:
instead of creating the host with typeof(serverclass), I use an instance of serverclass.
serverclass has an event and I attach an event handler to it that writes into the log.
The problem with this solution is it requires extra care with mult-threading, re-entrant, multi-session, per call settings ... etc.
My second solution:
use a static delegate inside serverclass and still create the host with typeof(serverclass). This way I can assign the logging function to the static delegate.
I don't feel this is the best way and I really appreciate comments or hints.
Thank you.
You may use WCF behaviour extensions to intercept the call and then write the log file.
There are various ways to intercept it by implementing various extension interfaces but message inspector is one of them.
Have a look here.
You could also consider an AOP approach. PostSharp is a good platform for this. The free version supports entry, exit, and error aspects.

Categories