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

给你一囗甜甜゛ 提交于 2020-01-03 11:46:08

问题


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:


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
{
}


来源:https://stackoverflow.com/questions/13235345/how-can-i-mark-the-methods-in-a-wcf-client-proxy-generated-class-as-virtual

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!