Alternative to service reference

别来无恙 提交于 2020-01-13 03:44:09

问题


I am trying to assist one project team to streamline their work by fixing some of the pain points.

One of the pain points they have in their code is that, they are using WCF service via service references (proxy) [i.e. "Add Service Reference" in Visua Studio 2008. This creates a lot of problem including deployment overhead, Souce Control get latest related problems of updating proxy etc.

In order to handle these and other related issues with service reference, I am looking for a good alternative to service references. I have already seen ChannelFactory and I am leaning towards that most. That seems to be a good solution altogether.

However the problem is that, there is a lot of code consuming these services like this

BatchClient client = new BatchClient(); //Batchclient is  a proxy
batchData = client.GetBatchData(batchNumber)

So if I go the ChannelFactory path, I would need to update all code piece like the above throughout the project. Because of amount of changes, the team is not very comfortable with this option.

Question I have is that, is there any other better alternative to "Add service reference" which can be used with minimal code changes? Or is there any way I could use the ChannelFactory without affecting exsting code pieces?


回答1:


Firstly, I believe you could easily fix your 'have to replace a lot of code' problem by creating your own proxy BatchClient which would instantiate the channel. It would work essentially the same way as the currently-used generated code does.

However before moving to the proxy-less, contract-sharing model I'd really consider why you want to move. I don't use the generated code option anymore, primarily because with familiarity of WCF it's not necessary. However I believe that the 'add service reference...' pattern is easier for WCF beginners, and it really doesn't cost you at all.

Ask yourself the following questions (or rather, explain to me):

  • What is the deployment overhead? I don't see any. The proxy that you've generated will work after deployment. You change the service address & behaviours in the configuration file.
  • Problems with source control / get latest? This should not be a problem, either. Just get latest, and use the latest. If the service has changed - and you want to take advantage of the changes - you will need to get latest version of the service contract, if you're using a shared contract file and instantiating your own channel.
  • Updating the proxy? Lack of familiarity with how the proxy works, what it is and what it does might cause this, but again it's no easier without the proxy. Really, you just right click up select 'update service reference', right?

The model you are considering is cleaner, and perhaps allows for more flexibility, but it is no simpler. If your team are having problems with maintaining the generated service proxy then I'd address those problems rather than throwing them into the deep end of WCF by removing the proxy.




回答2:


There are some article that suggest to use class ClientBase to achive the same purpose.

See a) https://aturcarablog.wordpress.com/2016/08/07/alternative-way-to-consume-wcf-service/

b) http://www.codeproject.com/Articles/412363/How-to-Use-a-WCF-Service-without-Adding-a-Service

Thus, your code can be rewritten as:

using (var batchClient = new ServiceWrapper<IYourWcfService>("YourEndpointConfigurationName"))
{
    batchData = batchClient.Proxy.GetBatchData(batchNumber);
}


来源:https://stackoverflow.com/questions/5787727/alternative-to-service-reference

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