WCF Note8(Client)

http://stackoverflow.com/questions/1698275/wcf-channelfactory-vs-generating-proxy

There are 3 basic ways to create a WCF client:

  1. Let Visual Studio generate your proxy. This auto generates code that connects to the service by reading the WSDL. If the service changes for any reason you have to regenerate it. 

    The big advantage of this is that it is easy to set up - VS has a wizard and it's all automatic.

     The disadvantage is that you're relying on VS to do all the hard work for you, and so you lose control.

  2. Use ChannelFactory with a known interface. This relies on you having local interfaces that describe the service (the service contract).

    The big advantage is that can manage change much more easily - you still have to recompile and fix changes, but

    now you're not regenerating code, you're referencing the new interfaces. Commonly this is used when you control both server and client as both can be much more easily mocked for unit testing. However the interfaces can be written for any service, even REST ones - take a look at this Twitter API.

  3. Write your own proxy - this is fairly easy to do, especially for REST services, using theHttpClient or WebClient. This gives you the most fine grain control, but at the cost of lots of service API being in strings. For instance: var content = new HttpClient().Get("http://yoursite.com/resource/id").Content; - if the details of the API change you won't encounter an error until runtime.

Personally I've never liked option 1 - relying on the auto generated code is messy and loses too much control. Plus it often creates serialisation issues - I end up with two identical classes (one in the server code, one auto generated) which can be tided up but is a pain.

Option 2 should be perfect, but Channels are a little too limiting - for instance they completely lose the content of HTTP errors. That said having interfaces that describe the service is much easier to code with and maintain.

Using ChannelFactory Vs. Proxies in WCF

http://blogs.msdn.com/b/juveriak/archive/2008/02/03/using-channels-vs-proxies-in-wcf.aspx

 When to use a proxy?
We use proxy in WCF to be able to share the service contract and/or entities with the client. If the client to your service is external to the system, such as API, it makes sense to use a proxy, because it makes sharing the contract easier by giving a code file rather than a DLL. 

When to use a ChannelFactory i.e. a shared DLL?

A DLL is helpful if the client code is under you control and you'd like to: share more than just the service contract with the client -- such as some utility methods associated with entities and make the client & the service code more tightly bound. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值