tags:

views:

217

answers:

1

Does anyone know a way of listing the UriTemplate's of the various operation contracts in WCF? What I want to do is to somehow at IntegrationTesting spin up a selfhosted service and loop through all the operation contracts and print the UriTemplates if at all possible.

+3  A: 

Do you mean the Action? There is no UriTemplate property on OperationContract.

If yes, you can use reflection to get the Methods of the type and from each method get the OperationContractAttribute to get it's Action property.

var methods = typeof (IService1).GetMethods();
IEnumerable<string> actions = methods.Where(
    m => m.GetCustomAttributes(typeof (OperationContractAttribute), true).Count() > 0)
    .Select(m => 
        ((OperationContractAttribute)m.GetCustomAttributes(typeof (OperationContractAttribute), true).First()).Action);

Console.WriteLine(string.Join("\r\n",actions.ToArray()));

EDIT: as marc mentions, you may be after WebGet, so replace OperationContractAttribute with WebGetAttribute and Action with UriTemplate or whatever property you would like to see.

Sky Sanders
WCF REST method have both an [OperationContract] and a [WebGet] attribute on each method, basically. I think that's what the OP is after.
marc_s
Thanks, Marc, I haven't yet explicitly used REST much but now that you mention it...
Sky Sanders
Not exactly what I was looking for, but I changed the to WebGetAttribute and WebInvokeAttribute and was able to find two problematic Uri templates I would prefer to be able to get that for the running service but as long as I can test it somehow I am happy :)
mhenrixon
Can get it from the WSDL?
Sky Sanders
Unfortunately not because calling the WSDL fails because I have operation contracts that takes parameter stream and that makes the WSDL fail unless the stream parameter is the only parameter :) Stupid hey
mhenrixon
REST calls don't have a WSDL......
marc_s
[stuffs on more nugget in the noggin]->
Sky Sanders