Is there a way to know whether the currently executing WCF method is a OneWay method?
I'm using httpBinding and the question relates to the server side.
I searched in properties for OperationContext on MSDN and couldn't find it.
EDIT:
I used the following check:
HttpContext.Current.Response.StatusCode !=
(int)System.Net.HttpStatusCode.Accepted;
In case of OneWay calls the status code will be 202, but it's not a good way.
Are there any better ways?
The WCF way to solve this is to:
This requires plugging into multiple WCF extension points. Its not hard once you get the hang of it, but it is a lot of typing because of all the interfaces you need to implement (even when the method implementations are empty). Here's an example.
First define a Simple Service:
It uses a custom context object to store the information you need. In this case a bool that is true if the operation IsOneWay. Notice that since you are wrapping the WCF InstanceContext, unit testing without actually hosting the service is possible. Method to create a custom context taken from this blog:
Create an OperationInvoker to add the custom context to the OperationContext. Note that inserting a WCF OperationInvoker means putting it in the call stack. So all calls that it doesn't handle, it needs to pass on to the framework's "inner" OperationInvoker.
Now apply the new behavior to the contract. The [OneWayContract] attribute applies a WCF Contract behavior, which applies an operation behavior. You could also apply the behavior at the operation level.
Note that the OperationDescription provides all the information that you need with WCF structures that are already populated. No resorting to reflection. No dependency on binding.
Now run a quick test.
Output should be:
Notice that all our abstractions are maintained. The service only depends on the context object provided by a behavior, which is clearly marked as a dependency on the service by the attribute.
The example puts
[OneWayContract]
on the service class. But you should also be able to apply it to the[ServiceContract]
.For completeness sake this is a copy of the entire code sample as one console app that you can paste and run.
as Tim suggested, use reflection. The following snippet should work for you
You could also use stackframe to get the current method name, but I used reflection all along.