WCF wrap proxy client

喜欢而已 提交于 2019-12-31 06:56:25

问题


I have many web method in my projects that used in client application. ]

I don't want write code something like this:

 using(ServiceClient sc = new ServiceClient())
    {
         //Invoke service methods        
         sc.Method1();
    }

instead, I want to write: ServiceClient.Method1(); in that case all common operation which reffered to the proxy initialization, invoking method, disposing, exception processing will be inside ServiceClient. Of couse, i can wrap any of my web method with similar code or use reflection for retriving method by name, but maybe any other ways are exist?


回答1:


I assume you are using Add Service Reference to generate ServiceClient...

First off, it's not safe to wrap ServiceClient (which derives from ClientBase) in a using block. Here is a stackoverflow thread that goes in depth.

Second, you can encapsulate the generated ServiceClient and create static methods that do what you are describing, but that's a lot of manual coding for very little benefit. I would instead encourage you to encapsulate the proxy creation, invocation, error handling, closing and disposing in a generic class. This is a good example.




回答2:


You can make your "Method1" a static member of ServiceClient and then call ServiceClient.Method1();



来源:https://stackoverflow.com/questions/5831193/wcf-wrap-proxy-client

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