On demand proxies in WCF
-
Thursday, January 26, 2012 12:12 PMHi, can anybody explain what does an creating on demand proxies in WCF means?
All Replies
-
Friday, January 27, 2012 5:20 AMModerator
Hello, can you be more specific? If you mean you want to create an instance of a client proxy only when needed, then you can create it right before you want to call the service. And after the service call completes, you close the proxy. If you mean you don't want to use add service reference to create a proxy, and want to invoke a service dynamically, it can be difficult. At a minimum, you need to know the request format expected by the service. Then you can create a Message dynamically, as described in http://blogs.msdn.com/b/kaevans/archive/2009/01/20/dynamically-invoking-web-services-with-wcf-this-time.aspx. If you don't even know what message format the service expects, you will have to manually invoke svcutil to generate a client proxy class, use CodeDOM to dynamically compile the code, and finally use reflection to invoke the service.
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.- Proposed As Answer by Peter Borremans Friday, January 27, 2012 6:34 AM
-
Friday, January 27, 2012 6:31 AM
Hello....
Follow this example...
On demand proxy generation means, the service proxy will be generated dynamically when you are going to call your service.
In add service ref and using svcutil the proxy will be generated when you do add reference and you can find it in your app it self.
But in on demand proxy generation, the proxy will be generated at runtime on the wire so the benefit of on demand proxy is, you do not need to do update service reference every time when you do changes in your service.
Service Declaration
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using DemoSolution.Common.Entities; namespace DemoSolution.Business.Interfaces { [ServiceContract] public interface ICalculatorManager { [OperationContract] [FaultContract(typeof(Exception))] string AddToNumber(Number number); } }Service Implementation
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DemoSolution.Business.Interfaces; using DemoSolution.Data.DataAccess; using DemoSolution.Common.Entities; namespace DemoSolution.Business.Managers { public class CalculatorManager : ICalculatorManager { CalculatorDb _calculatorDb = null; public CalculatorManager() { _calculatorDb = new CalculatorDb(); } public string AddToNumber(Number number) { try { if (!string.IsNullOrEmpty(number.Error)) throw new Exception(number.Error); int addition = _calculatorDb.AddTwoNumbers(number.Number1, number.Number2); string result = string.Format("The result of {0} + {1} = {2}", number.Number1, number.Number2, addition); return result; } catch (Exception ex) { throw ex; } } } }Calling service from client using ChannelFactory<T> Class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DemoSolution.Business.Interfaces; using System.ServiceModel; using DemoSolution.Common.Entities; namespace DemoSolution.Business.ServiceAgent { public class CalculatorServiceAgent { public string AddToNumber(Number number) { string result = string.Empty; ChannelFactory<ICalculatorManager> cf = null; try { cf = new ChannelFactory<ICalculatorManager>("BasicHttpBinding_ICalculatorManager"); result = cf.CreateChannel().AddToNumber(number); } catch (Exception ex) { throw ex; } finally { cf.Close(); } return result; } } }BasicHttpBinding_ICalculatorManager is the name of endpointConfiguration name which you have specified in app.config file(client side)
App.config file (client)
<?xml version="1.0"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_ICalculatorManager" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:6001/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICalculatorManager" contract="DemoSolution.Business.Interfaces.ICalculatorManager" name="BasicHttpBinding_ICalculatorManager" /> </client> </system.serviceModel> </configuration>Web.config file (service)
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service name="DemoSolution.Business.Managers.CalculatorManager"> <endpoint address="http://localhost:6001/Calculator.svc" binding="basicHttpBinding" bindingConfiguration="" contract="DemoSolution.Business.Interfaces.ICalculatorManager" /> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>By using above example when you call wcf service (using ChannelFactory<T>), the proxy will be created on demand and then be closed after the execution of your service call.
for more information about ChannelFactory<T> class see the below link:
http://msdn.microsoft.com/en-us/library/ms576132.aspx
I Hope, this may fix your requirement
Regards, Hiren Bharadwa
- Edited by Hiren Bharadwa Friday, January 27, 2012 6:38 AM
- Edited by Hiren Bharadwa Friday, January 27, 2012 6:41 AM
- Edited by Hiren Bharadwa Friday, January 27, 2012 6:43 AM
- Edited by Hiren Bharadwa Friday, January 27, 2012 6:44 AM
- Edited by Hiren Bharadwa Friday, January 27, 2012 6:47 AM
- Edited by Hiren Bharadwa Friday, January 27, 2012 6:48 AM
- Edited by Hiren Bharadwa Friday, January 27, 2012 11:39 AM
-
Friday, January 27, 2012 5:17 PM
more pointers here:
http://social.msdn.microsoft.com/forums/en-US/wcf/thread/b74f700e-3a20-4327-9e81-f0dd587c7d81/
hth, Allan
-
Tuesday, January 31, 2012 9:05 AMThanks for the reply. I wanted to create dynamic proxy without any binding restriction.
-
Wednesday, February 01, 2012 9:41 AM
Hello....
Just follow my example, that will guide you to create Dynamic proxy to call your service at runtime.
and which type of binding restriction you are talking about??
Regards, Hiren Bharadwa -
Saturday, February 04, 2012 1:09 PMIn your given example, while creating dynamic proxy you are making the use of ChannelFactory specifying the service contractor. Is there any way to create dynamic proxy by just specifying just service URL.
-
Saturday, February 04, 2012 7:55 PM
hi _Sanika,
i see the link I provied does not work anymore(sorry), have a look at Vipul's dynamic proxy, used this my self and it works like charme.
http://blogs.msdn.com/b/vipulmodi/archive/2008/10/16/dynamic-proxy-and-memory-footprint.aspx
hth Allan
- Proposed As Answer by Allan-Nielsen Saturday, February 04, 2012 7:55 PM
-
Friday, February 17, 2012 6:36 AMI have created one console application, given DynamicProxyLibrary reference and Model projects reference which contains all project Entities. When I Call GetEntity() using CallMethod() it throws exception like can not convert object of type 'TestApp.Entities.Entity' to the type 'TestApp.Entities.Entity'. why so?
-
Wednesday, February 29, 2012 6:46 AM
hi,
this is good .but what if incase of wcf service is not web and i have created wcf service using wcf service library ???

