Freigeben über


Dynamic Programming with WCF

Update: Please check out the post on memory footprint and dynamic proxy (https://blogs.msdn.com/vipulmodi/archive/2008/10/16/dynamic-proxy-and-memory-footprint.aspx)

-----

Ever wonder what it would like to go from WSDL to code at runtime? Check out my WCF Dynamic Proxy tool that I posted on the wcf.nefx3.com community site. The WCF Dynamic Proxy downloads the WSDL, generate the code, compile the code and then allow you to invoke the web service operations using reflection, all at runtime. Here is the readme from the tool: https://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=netfxsamples&DownloadId=3939

The DynamicProxy allows you to create the dynamic WCF client at runtime by specifying the WSDL URI of the service. The DynamicProxy does not depend on the precompiled proxy or configuration. The DynamicProxy uses the MetadataResolver to download the metadata from the service and WsdlImporter to create the contract and binding at runtime. The compiled dynamic proxy can be used to invoke the operations on the service using reflection.

The example shows how you can the dynamic proxy to invoke operations that use simple types and complex types. The flow of usage is as following.

1. Create the ProxyFactory specifying the WSDL URI of the service.

    DynamicProxyFactory factory = new DynamicProxyFactory("https://localhost:8080/WcfSamples/DynamicProxy?wsdl");

2. Browse the endpoints, metadata, contracts etc.
factory.Endpoints
factory.Metadata
factory.Contracts
factory.Bindings

3. Create DynamicProxy to an endpoint by specifying either the endpoint or
contract name.
DynamicProxy proxy = factory.CreateProxy("ISimpleCalculator");

    OR

    DynamicProxy proxy = factory.CreateProxy(endpoint);

4. Invoke operations on the DynamicProxy
dobule result = (dobule)proxy.CallMethod("Add", 1d ,2d);

5. Close the DynamicProxy
proxy.Close();

Comments

  • Anonymous
    February 21, 2007
    Hi, We had to face similar problematics, the result is two libraries. The first one uses only .NET 2.0 and offers similar programmatic experience for Asmx, Remoting and Wse 3.0. You can have a look at http://www.codeplex.com/ProxyFactory. The second uses WCF and it introduces the concept of leveraging a service metadata in order to retrieve at runtime its endpoint configuration therefore avoiding duplicating the configuration information on both service and all its clients. It is also leveraging ChannelFactory<T> for strongly typed proxy generation. Article, video and sample code are available at this url : http://netfxfactory.org/blogs/papers/archive/2007/01/24/An-effective-way-to-access-wcf-services.aspx We would be interested by your feedback.

  • Anonymous
    February 21, 2007
    Is there sample for "net.tcp://www.codeplex.com/ProxyFactory."? Thanks a lot.

  • Anonymous
    March 16, 2007
    A remonting sample (tcp://localhost:9000/AccountManager.rem) is provided in the getting started of the ProxyFactory project. And the SmartChannelFactory project based on WCF technology supports, of course, the NetTcpBinding.

  • Anonymous
    May 23, 2007
    I tried to implement the same factory concept in my apps. , in which i want to dynamically invoke different third party webservices and is giving issues regarding the contracts. I will not be able to implement the contract info into these third aprty webservices, but i want to invoke them on fly. How can i go about resolving it... tips welcome..

  • Anonymous
    July 13, 2007
    When I try to read WSDL using the dynamic proxy class in this blog, for certain web services I get the an error message similar to the following: "Cannot import wsdl:bindingrnDetail: The required WSDL extension element 'binding' from namespace 'http://schemas.xmlsoap.org/wsdl/http/' was not handled.rnXPath to Error Source: //wsdl:definitions[@targetNamespace='http://someurl.com']/wsdl:binding[@name='StockQuotesHttpGet']" I've noticed that the error always occurs on web services with HttpPost and HttpGet bindings.  I can read these web services successfully with webservicestudio, so I know the WSDL is valid.  Could this be a WCF/WSE limitation?  Any ideas on how to resolve it?

  • Anonymous
    August 16, 2007
    Will it work with any kind of binding, including netMsmqBinding (with metadata through WS HTTP binding)?

  • Anonymous
    September 27, 2007
    Hi Vipul Can I use DynamicProxy with NetDataContractSerializer. I am trying to refer the NetDataContractSerializer (Aaron Skonnard's implementation) with Dynamic Proxy, and I am getting the dynamicproxy dll not found error while invoking the service. Any help is highly appreciated Rama

  • Anonymous
    January 30, 2008
    How to do Exception handling with Dynamic proxy

  • Anonymous
    February 28, 2008
    The comment has been removed

  • Anonymous
    March 12, 2008
    The comment has been removed

  • Anonymous
    March 13, 2008
    i just want to get on at school

  • Anonymous
    April 07, 2008
    Thanks for the excellent work.  I was able to incorporate this into a client console application in my environment, which works fine, but when using a WinForms client, the call to serviceFinder.Probe() always results in a timeout.  I suspect this may be related to SynchronizationContext, but have been unable to overcome the issue.  Any suggestions would be appreciated.

  • Anonymous
    May 19, 2008
    Thanks for using the sample and thanks for the comments. Let me try to answer some them here.

  1. Can I use Dynamic Proxy with other bindings?   Yes you can.
  2. I get an error while creating the factory.   Run SVCUTIL on the URL you are trying to create the proxy for. If SVCUTIL gives an error the dynamic proxy will. Try to debug the issue with SVCUTIL.
  3. What about downloading the metadata through MEX endpoint instead of ?wsdl.   Yes, you can modify the sample to download metadata using System.ServiceModel.Description.MetadataExchangeClient.
  4. What about complex types in my service contact, how do I use them?    If you already have the service contract and types pregenerated, you can simply create a channel factory using known service contract and binding and address given to you by the dynamic proxy factory.    If you do not have the service contract before hand, you are left with using Reflection. The sample provide an example of Complex type (DynamicComplexNumber.cs).
  • Anonymous
    October 16, 2008
    A while back I published a post about dynamic programming with WCF using the dynamic proxy library that

  • Anonymous
    October 17, 2008
    First, i must thank u for this great work!!! I have a question: How can i call my service's Methods asynchronously? i want to use asynchronous operations by using this Library(Dynamic Proxy) but i haven't found any way. Could u help me .... thanks Rahman

  • Anonymous
    October 19, 2008
    Hi, I Solved My Problem!! I changed the Library's Code and by that,   asynchronous Operations generate Dynamically! i will published the changed library in the CodeProject Site very Soon. I Hope it will be useful ... R. Khanipour

  • Anonymous
    October 31, 2008
    Good morning, I have tested your client with my application and have gotten data back from the database. the issue... my configuration files from app.config are not being read. the one which is complaining is maxreceivedmessagesize. unfortunately it is set to default of 65536. How can I get more data than this on a call to the database. thank you for any assistance. rick. You may respond also to rwatson@ogleveeltd.com

  • Anonymous
    February 20, 2009
    Hi Vipul, How can I get my Properties in a Complex Type reconstructed in the Proxy code. Using the current code, the property names are replaced by their corresponding private variables in the proxy code. Thanks, Vikas

  • Anonymous
    March 09, 2009
    I have a similar issue. I have xyz WCF Service that inherits from xyzBase. xyzBase has a property that I want to access. When I create a proxy class no metadata is generated for the property that is defined in the base class. In other words, when I try:   PropertyInfo fi = webService.GetType().GetProperty("ServiceHeaderValue"); it cannot find the ServiceHeaderValue propery. When I use traditional ASP.NET service this works fine. I can see using Lutz Reflector that the property is being exposed .. it just doesn;t seem to make it into the proxy class genertaed by svcutil.exe. Any ideas would be greatly appreciated.

  • Anonymous
    March 12, 2009
    Hi Vipul Is it possible to pass a cookie to the dynamic proxy before calling the invokeMember function.  I am having trouble accessing an endpoint that needs to have authentication (We are implementing SSO).  Many thanks for your help. Kind Regards

  • Anonymous
    March 16, 2009
    Hi Vipul, I was able to extract the the input parameters to "MyMethod" function by doing the following: DynamicProxy _dp = _dpf.CreateProxy(endpoint); Type proxyType = _dp.ProxyType; MethodInfo method = proxyType.GetMethod("MyMethod"); ParameterInfo[] inputParameters = method.GetParameters(); My output parameter is complex structure represented in the WSDL by the xml bellow. I am not sure how I can retrieve the type and name of each output parameter within the complex one! <s:complexType name="ProcessStats">  <s:sequence>   <s:element minOccurs="1" maxOccurs="1" name="accessDenied" type="s:boolean"/>   <s:element minOccurs="1" maxOccurs="1" name="isRunning" type="s:boolean"/>   <s:element minOccurs="1" maxOccurs="1" name="processID" type="s:int"/>   <s:element minOccurs="0" maxOccurs="1" name="processName" type="s:string"/>   <s:element minOccurs="1" maxOccurs="1" name="totalRunTime" type="s:double"/>   <s:element minOccurs="0" maxOccurs="1" name="errorDescription" type="s:string"/>  </s:sequence> </s:complexType> The same is true for complex input parameters!  Do you know if there is a clear way to do this? Thank you very much.

  • Anonymous
    March 19, 2009
    Hi Vipul; Thanks For your Post. can you please help me to solve one issue. I am using dynamicproxy dll in my client application, when I run my client application and try to invoke some services(WCF) its showing error saying that "The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element." I google for this error and get a fix,When we add a service in our client Application it created binding configuration there we need to change MaxReceivedMessageSize. my question is how we change MaxReceivedMessageSize value in DynamicProxy  

  • Anonymous
    April 03, 2009
    WSHttpBinding b = new WSHttpBinding(SecurityMode.None); b.MessageEncoding = WSMessageEncoding.Mtom;            b.TransferMode = TransferMode.Streamed;            b.ReaderQuotas.MaxStringContentLength = 99999; still i get error when i pass large data. Help needed

  • Anonymous
    July 12, 2009
    Can you provide example on how to create an instance of a custom DataMember class and use it within the Proxy?

  • Anonymous
    August 28, 2009
    Works great, but I need to customize binding because my service host is Java based. I tried to change factory.Bindings and add extra behaviors to factory.Endpoints, but they don't seem to be changing anything in the Proxy?

  • Anonymous
    October 28, 2009
    How to connect to WCF web services and by passing complex data object?

  • Anonymous
    February 26, 2010
    Hi, I tried using the dynamicproxy in my code, i am currently facing an issue, when i try to pass a DataContract object i am getting Missing Method Exception thrown from the CallMethod function, tried googling but couldn't find a solution to the same, it seems that the invokemember of reflection class is throwing the error, but i couldn't get a concrete solution. Any helps appretiated

  • Anonymous
    March 04, 2010
    Hi Vipul, I have similar question to others concerning setting MaxReceivedMessageSize value in DynamicProxy.  I tried different things but they don't seem to make any difference.  Your help is appreciated.  If you can give us a code snippet that will be great.

  • Anonymous
    March 11, 2010
    The comment has been removed

  • Anonymous
    March 16, 2010
    I have made a simple addition to the Dynamic Proxy project, added a new class DynamicProxyClass which inherits from Dynamic Object, modified the GetType function in the class to public static Type GetType(Assembly assembly, string TypeName)        {            return assembly.GetType(TypeName, true, true);        } sending the type from outside rather than hard-coding it. so for creating an object doing something like this DynamicProxyClass requestProxy = new DynamicProxyClass(factory.ProxyAssembly, CLASS.GetType().ToString()); more info on this @ http://dvij.blogspot.com/2010/03/implementing-dynamic-proxy-in-wcf.html

  • Anonymous
    April 12, 2010
    How do you build  the WSDL string("http://localhost:8080/WcfSamples/DynamicProxy?wsdl"); what does WcfSamples signify, Can I choose any name?   Then do I need to specify "DynamicProx?wsdl"?

  • Anonymous
    August 10, 2010
    @vikas make sure you call your method using the correct params (count and type).

  • Anonymous
    September 15, 2010
    hi, vipul. i am using your tool. it is very good. But i am in very big problem that is "how can i pass UserName  and password by using your tool. i am using wsHttpBinding". please help me. my email ID is "naresh_soni44@yahoo.com"

  • Anonymous
    October 17, 2010
    Hi Vipul, how to pass ref and out parameters to/from WCF using Dynamic Proxy Class Library

  • Anonymous
    October 31, 2010
    This daynamic proxy code is awesome!  I've modified the DownloadMetadata function like this: EndpointAddress epr = new EndpointAddress(this.wsdlUri); MetadataExchangeClient mexC = new MetadataExchangeClient(new Uri(this.wsdlUri), MetadataExchangeClientMode.MetadataExchange); MetadataSet metaDataset = mexC.GetMetadata(); this.metadataCollection = new Collection<MetadataSection>(); foreach (MetadataSection mds in metaDataset.MetadataSections) {     this.metadataCollection.Add(mds); } to allow downloading the metadata over a NetTCP MEX endpoint. What I'm trying to do now is allow generating a proxy with a callback fuction (to support a tcp duplex channel.)  The code currently throws a DynamicProxyException("The constructor matching the specified parameter types is not found.")  Ideally the code would be generated with a callback event that could be handled in the client application.   Please let me know if anyone has already done this, or has a good approach to look into.

  • Anonymous
    February 08, 2011
    This is an interesting idea. Is there a Silverlight version? You can achieve part of this functionality in Silverlight with a sample that I created here (obviously not as comprehensive as this solution though) christianfindlay.blogspot.com/.../dynamic-wcf-services-silverlight.html

  • Anonymous
    March 29, 2011
    Thanks for that great work Vipul! How can I pass UserName  and password by using your tool?

  • Anonymous
    March 30, 2011
    I solved the credentials problem. DiscoveryClientProtocol disco = new DiscoveryClientProtocol(); disco.UseDefaultCredentials = false; disco.Credentials = new NetworkCredentials("userName", "password");

  • Anonymous
    May 29, 2011
    Hey I am not able to build you project! How do i start off :/

  • Anonymous
    June 23, 2011
    This stuff is cool... but i am not able to run it yet. When i try to expose methods in my Service it si showing error method name not found. I am working on WCF Workflow service which is hosted in IIs. Do your code support xamlx?

  • Anonymous
    July 23, 2011
    Hi Vipul, Is there a way to invoke methods on a asmx service dynamically using your library

  • Anonymous
    October 12, 2011
    have you try it on WCF 4 Workflow Services ? its got error Method not found, I'm sure the method exist.

  • Anonymous
    November 02, 2011
    Great little tool. I would like to be able to use it with a service over SSL. Where in your code should I load the client X509 cert? I tried disco.ClientCertificates.Add(cert); but that didn't work.

  • Anonymous
    March 21, 2012
    How to specify the collection Type I need Collection type as List not as array

  • Anonymous
    July 26, 2012
    Hello sir,Nice post.i am novice in WCF programming.when i call my service using Dynamic Proxy class,it gives me an error on call method."Method Not found".despite i am passing correct method name

  • Anonymous
    October 30, 2012
    Vipul your work is just awesome but how can we get the types of method parameters and handle complex types using wsdl can you show us a sample example it would be really helpful.

  • Anonymous
    November 14, 2012
    Modification made to the DynamicProxy.Close() method.  Before the modification some exceptions were hidden and replaced with. The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state public class DynamicProxy {    public void Close()    {        var state = GetProperty("State") as CommunicationState?;        if (state.HasValue &&            state.Value == CommunicationState.Opened)        {            CallMethod("Close");        }    }

  • Anonymous
    January 24, 2013
    Anyone here solved the "Method not found" issue? I'm sure the method exists. However why do I get that issue?

  • Anonymous
    April 24, 2013
    Hi, Thanks for this amazing code!! I get an error when I use the DynamicProxy.SetProperty Method. Any idea how to resolve it?

  • Anonymous
    April 25, 2013
    The comment has been removed

  • Anonymous
    May 28, 2013
    Hi Vipul, I am creating a Dynamic wcf test client. when I browsed in net I found most of the people talking about you and your code. It is very important project for me. can you share your code with me. I need some guidance because I am new to wcf  but I am trying my level best to do. So please guide me dude.

  • Anonymous
    May 30, 2013
    Firstly: Amazing work :) Secondly: It seems to only return the first message body member instead of returning the response message (as I would prefer). How do I change this (I don't mind getting my hands dirty in the source)? Also, how do I perform authorisation? Thanks

  • Anonymous
    July 18, 2013
    When we pass Custom object as parameter, it is giving error method not found. Please help us.

  • Anonymous
    September 05, 2013
    Hi, i´m also facing missing method exception. Anyone knows how to fix it? TKS

  • Anonymous
    September 16, 2013
    Hello Vijay / luiz For that ObjectInstance to be used like.. proxy.CallMethod("methodName", DynamicObj.ObjectInstance);

  • Anonymous
    November 12, 2013
    The comment has been removed

  • Anonymous
    January 13, 2014
    Hi, great work! How can i peform authentication? Tks!

  • Anonymous
    February 27, 2014
    The comment has been removed

  • Anonymous
    May 11, 2014
    HI, Its working Great but when I pass a custom object as a parameter, its throwing exception can any body place an example how to call a Service method with CustomClass as a Parameter, Its urgent Thanks in Advance

  • Anonymous
    May 28, 2014
    Hi, It looks like all the links stoped working. Is there any other place where I can download library from ?

  • Anonymous
    November 15, 2014
    Hi Vipul, Thanks for this excellent post. I have one problem though. If my wsdl is such that I have ReplyAction = * in OperatonContract, then while calling any of the method, it throws me the error "Method not found". If I add that service reference to my client, everything works fine, I also added a code to show the method names inside the proxy, I am able to find the function to be called. I further researched this problem and found that there is some issue with ReplyAction = *. The WSDL to be called is a third party wsdl and I have no control to change the corresponding ReplyAction property. Are you aware of any such problem. Any help in this regard would be appreciated. Thanks in advance Naresh

  • Anonymous
    December 16, 2015
    Hi Vipul,   Is it possible to get the project sources? The download page seems to be inaccessible.

  • Anonymous
    December 18, 2015
    Is the code still available. Download link seems to be broken

  • Anonymous
    March 04, 2016
    The download page seems to be inaccessible. Is there another link avaiable? Thank