Why does WCF generated proxy wrap contract interface methods with new methods with different signatures?
Asked Answered
I

1

6

I'm subcsribing to the SQL Server 2008 SSRS web service ( .../reportserver/ReportService2005.asmx?wsdl) using WCF, with default WCF config options as far as I can tell.

It does something weird when it generates the local proxy classes though.

I'll use the ListChildren method as an example:

On the client side, WCF generates an interface like this, as you would expect:

public interface ReportingService2005Soap {

    ListChildrenResponse ListChildren(ListChildrenRequest request);

}

It also generates a 'client' proxy that implements that interface:

public partial class ReportingService2005SoapClient :
    System.ServiceModel.ClientBase<ReportingService2005Soap>, ReportingService2005Soap 
{

    [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
    ListChildrenResponse ReportingService2005Soap.ListChildren(ListChildrenRequest request) 
    {
        return base.Channel.ListChildren(request);
    }

    public ServerInfoHeader ListChildren(string Item, bool Recursive, out CatalogItem[] CatalogItems) {
        ListChildrenRequest inValue = new ListChildrenRequest();
        inValue.Item = Item;
        inValue.Recursive = Recursive;
        ListChildrenResponse retVal = ((ReportingService2005Soap)(this)).ListChildren(inValue);
        CatalogItems = retVal.CatalogItems;
        return retVal.ServerInfoHeader;
    }

}

As you can see, the client proxy implements the interface and then 'hides' it from being used by explicitly implementing the interface (so you have to cast to get to the interface method) and additionally with a EditorBrowsableState.Advanced attribute.

It then adds an extra wrapper method that uses 'out' parameters.

Is there a way to stop if from doing that, and just have it implement the interface directly?

What its doing here leads you down the path of using the wrapper methods with 'out' parameters, and then you find you can't mock the service very easily because the wrapper methods aren't virtual, and aren't defined in any interface.

NB: I'm using the SSRS web service as an example here but I've seen WCF do this on other services as well.

Inocenciainoculable answered 26/8, 2011 at 11:9 Comment(3)
What happens if you go to advanced settings of Add service reference and check Always generate message contracts?Lexicon
@Ladislav : ah yes, that seems to fix it. If you post an answer I can vote you up, thanks.Inocenciainoculable
My question got 'popular question' badge (1000 views) but not one single upvote. I should get an anti-badge for that : SInocenciainoculable
L
7

This probably happens if your service is using MessageContracts. Proxy creation by default tries to unwrap these message contracts so that exposed operations accept their content directly. If you want to use message contracts on the client as well you need to configure it in advanced settings of Add service reference by checking Always generate message contracts.

Lexicon answered 26/8, 2011 at 13:55 Comment(1)
Thanks, I had overlooked that option until now. If it was called 'Never generate rubbish wrapper methods' I might have tried it : )Inocenciainoculable

© 2022 - 2024 — McMap. All rights reserved.