From Email FWs
A proxy is a class by which a service client can interact with the service. By the use of proxy in the client application we are able to call the different methods exposed by the service
The proxy is a CLR class that exposes a single CLR interface representing the service contract. The proxy provides the same operations as service’s contract, but also has additional methods for managing the proxy life cycle and the connection to the service. The proxy completely encapsulates every aspect of the service: its location, its implementation technology and runtime platform, and the communication transport.
The proxy can be generated using Visual Studio by right clicking Reference and clicking on Add Service Reference. This brings up the Add Service Reference dialog box, where you need to supply the base address of the service (or a base address and a MEX URI) and the namespace to contain the proxy.
Proxy can also be generated by using SvcUtil.exe command-line utility. We need to provide SvcUtil with the HTTP-GET address or the metadata exchange endpoint address and, optionally, with a proxy filename. The default proxy filename is output.cs but you can also use the /out switch to indicate a different name.
SvcUtil http://localhost/MyService/MyService.svc /out:Proxy.cs
When we are hosting in IIS and selecting a port other than port 80 (such as port 88), we must provide that port number as part of the base address:
SvcUtil http://localhost:88/MyService/MyService.svc /out:Proxy.cs
Both svcutil.exe & ‘Add Service reference’ will use the same proxy generation code underneath. Think of ‘Add service reference’ as a UI way to generate proxy where it pre-populates a set of switches that you will have to do in svcutil command line. For example when you add service reference in VS the UI lets you to reuse types from referenced assemblies, specify the kind of proxy to be generated (sync-async / task based etc). svcutil will also be useful if you want to automate service reference generation.
Use WCF service by adding Proxies to Client App -> http://www.devproconnections.com/article/net-framework2/wcf-and-client-proxies-122510
Use WCF witout addin proxies -> http://www.codeproject.com/Articles/412363/How-to-Use-a-WCF-Service-without-Adding-a-Service
Client Proxy wrappers are coded improve performance, error handling etc. Following are some of the scenarios that we opt for WCF client proxies.
Reuse the same proxy
In many cases, you would want to reuse the same proxy. This has the best performance. It is especially true when you use security features since the initial security negotiation can have high cost.
Don’t forget to open the proxy explicitly before using it as I mentioned that in my previous blog entry.
Use proxy that enables caching
As mentioned above, you can either use ChannelFactory<T>.CreateChannel to create your proxy or you can use auto-generated proxies. If you use the latter, you need to be aware of the following in order to get ChannelFactory cached:
- Don’t use the constructors of the proxy that takes Binding as an argument.
- Don’t access the public properties ChannelFactory, Endpoint, and ClientCredentials of the proxy.
Disabling caching
If you really want to disable caching, you can simply do the reverse of above. For example, you can access the ChannelFactory property of the proxy before it is first used.
Proxy/Channel pooling
The last resort of achieving high performance is through proxy caching. In some scenarios, you may not want all threads to use the same proxy due to the following two possible reasons:
- The context of the channels does not allow multiple threads to access.
- There is some bottleneck in the Channel stack that hurts performance when a single proxy is used.
The logic of pooling can be from very simple to very complicated. Some ideas to keep in mind are as following:
- You need to implement the right synchronization logic for managing the proxies.
- You need to make sure the proxies are used equally. Sometimes, you may want to implement a round-robin pattern for the proxies.
- You need to handle exceptions and retries for the pool.
- The pool size needs to be limited and configurable.
- You may need to be able to create proxies even if when no proxy is available from the pool.