Welcome to MSDN Blogs Sign in | Join | Help

Dynamic Programming with WCF

Update: Please check out the post on memory footprint and dynamic proxy (http://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: http://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("http://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();

Published Thursday, November 16, 2006 1:42 PM by vipulm

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Dynamic Programming with WCF

Wednesday, February 21, 2007 9:50 AM by Pascal

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.

# re: Dynamic Programming with WCF

Wednesday, February 21, 2007 6:30 PM by DJ Yang

Is there sample for "net.tcp://www.codeplex.com/ProxyFactory."?

Thanks a lot.

# re: Dynamic Programming with WCF

Saturday, March 17, 2007 5:58 AM by Pascal

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.

# re: Dynamic Programming with WCF

Wednesday, May 23, 2007 5:37 PM by DashHunter

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..

# re: Dynamic Programming with WCF

Friday, July 13, 2007 1:46 PM by JeffAtAon

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:binding\r\nDetail: The required WSDL extension element 'binding' from namespace 'http://schemas.xmlsoap.org/wsdl/http/' was not handled.\r\nXPath 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?

# re: Dynamic Programming with WCF

Thursday, August 16, 2007 9:30 AM by Gustavo

Will it work with any kind of binding, including netMsmqBinding (with metadata through WS HTTP binding)?

# re: Dynamic Programming with WCF

Thursday, September 27, 2007 11:06 AM by Rama

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

# re: Dynamic Programming with WCF

Thursday, January 31, 2008 1:41 AM by shikha

How to do Exception handling with Dynamic proxy

# re: Dynamic Programming with WCF

Thursday, February 28, 2008 8:41 AM by Marco

Hi,

i'm testing this component but i have a problem. For certain wsdl document i receive this exception

"THERE WAS AN ERROR IN IMPORTING THE METADATA"

What does it mean?

# re: Dynamic Programming with WCF

Thursday, March 13, 2008 7:50 AM by shilpa

Hi,

i'm testing this component but i have a problem. For certain wsdl document i receive this exception

"THERE WAS AN ERROR IN IMPORTING THE METADATA"

What does it mean?

# re: Dynamic Programming with WCF

Thursday, March 13, 2008 10:30 AM by Jack

i just want to get on at school

# re: Dynamic Programming with WCF

Monday, April 07, 2008 2:30 PM by James

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.

# re: Dynamic Programming with WCF

Monday, May 19, 2008 12:31 PM by vipulm

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).

# Dynamic Proxy and Memory Footprint

Friday, October 17, 2008 2:04 AM by Vipul Modi's Blog

A while back I published a post about dynamic programming with WCF using the dynamic proxy library that

# re: Dynamic Programming with WCF

Saturday, October 18, 2008 7:44 AM by Rahman

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

# re: Dynamic Programming with WCF

Sunday, October 19, 2008 8:46 AM by Rahman Khanipour

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

# re: Dynamic Programming with WCF

Friday, October 31, 2008 9:11 AM by rick watson

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

# Properties with WCF

Friday, February 20, 2009 3:34 PM by Vikas Jain

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

# re: Dynamic Programming with WCF

Monday, March 09, 2009 5:37 PM by ranger63

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.

# re: Dynamic Programming with WCF

Thursday, March 12, 2009 5:17 PM by Anil Koripelly

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

# re: Dynamic Programming with WCF

Monday, March 16, 2009 6:50 PM by Kris

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.

# re: Dynamic Programming with WCF

Friday, March 20, 2009 4:48 AM by Pradyot Ranjan

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  

# re: Dynamic Programming with WCF

Saturday, April 04, 2009 6:21 AM by Srinivas

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

# re: Dynamic Programming with WCF

Monday, July 13, 2009 5:56 AM by Harry Pooni

Can you provide example on how to create an instance of a custom DataMember class and use it within the Proxy?

# re: Dynamic Programming with WCF

Friday, August 28, 2009 5:20 PM by Boris

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?

# re: Dynamic Programming with WCF

Wednesday, October 28, 2009 11:30 PM by Jin Khai (sovietmah@gmail.com)

How to connect to WCF web services and by passing complex data object?

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker