How can I mark the methods in a WCF client proxy g

2019-06-20 19:56发布

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.

1条回答
时光不老,我们不散
2楼-- · 2019-06-20 20:50

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
{
}
查看更多
登录 后发表回答