Create WCF Client without auto generated proxy

后端 未结 3 1781
暖寄归人
暖寄归人 2020-12-13 05:22

looking at

WCF ChannelFactory vs generating proxy

appears that the best practice in creating a WCF client is to create a proxy (Not autogenerated).

3条回答
  •  离开以前
    2020-12-13 05:42

    This is how I do it.

    Get service contracts and data contracts

    If I have access to the service code, I have all the contracts. If not, I can use svcutil or Add Service Reference to generate them.

    Make config

    I use Add Service Reference just to get the app.config file. I then delete everything else it generates. Edit the app.config as necessary.

    Define factory

    Say I have a service contract IFooService:

    interface IFooServiceChannel : IFooService, IClientChannel { }
    

    That is literally it. No members.

    Create factory

    fooServiceFactory = new ChannelFactory(
                            "NetTcpBinding_IFooService");
    

    The string "NetTcpBinding_IFooService" is the name attribute of the binding element in app.config.

    Create channel

    fooService = fooServiceFactory.CreateChannel();
    

    Use it

    fooService.DoSomething();
    

    The trickiest part is getting app.config right. You need to learn about bindings and endpoints. It's a bit of a learning curve, but nothing drastic.

提交回复
热议问题