Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mark the methods in a WCF client proxy generated class as virtual

In VS 2010 I'm creating a service reference which code generates the WCF client proxy class Refernce.cs. I need the methods in this class to have the Virtual modifier so they can be used in Mock.

Of course I can hand edit the generated code, but every time I update the reference the code is going to be regenerated and wipe out my changes.

Do I have more control of how the WCF client proxy class is generated? Is there any way to have the code generator always add the Virtual modifier? I would like this to be more automated so that when other developers need to update the reference, they don't have to know or remember to hand edit the generated code and add the the virtual modifier.

like image 629
Dude0001 Avatar asked Nov 05 '12 15:11

Dude0001


People also ask

How will you specify a method is available to access by client in WCF?

With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button. The dialog will display a list of services available at the address you specify.

What is the use of proxy class in WCF?

Actually Proxy is a class in WCF that is used to communicate with client application. We can simply get the entire configuration through the proxy class. There is no need to do extra effort to generate the configuration setting for the client. Proxy class used when you think that your service must be loosely coupled.

What is required by the client to generate proxy?

A metadata exchange endpoint is required to support the dynamic generation of proxy and configuration for client applications. You must explicitly enable metadata exchange by adding the endpoint and enabling the metadata exchange behavior.


1 Answers

An alternative is to create an interface. The proxy classes are generated as partial, which means you can create another partial file for that class, and specify that the class implements your interface, even though the actual implementation is in the generated class. You can then mock the interface, and regenerate your proxy to your hearts content.

For instance, your generated class might be:

public partial class SomeService
{
    public string GetSomething()
    {
        return ... 
    }
}

You can create an interface for this:

public interface ISomeService
{
    string GetSomething();
}

And then add an empty partial file for the generated class:

public partial class SomeService : ISomeService
{
}
like image 163
Seth Flowers Avatar answered Oct 01 '22 01:10

Seth Flowers