Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wcf

wcf-proxy

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.

like image 345
codeulike Avatar asked Aug 26 '11 11:08

codeulike


1 Answers

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.

like image 131
Ladislav Mrnka Avatar answered Nov 04 '22 06:11

Ladislav Mrnka