Welcome to Windows Communication Foundation (WCF)
Top Tasks :

WCF Dynamic Proxy (Microsoft)

File Details
Downloads: 4320 File Size: 17.3kB
Posted By: vipulm Views: 30553
Date Added: Wed, Oct 04 2006

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();

To run the example: Compile the solution, run the CalculatorService.exe and then run the CalculatorDynamicClient.exe

Comments
 

Raghu said:

Wow! This tools works great. Thank you for the sample.
Thu, Oct 05 2006 15:26
 

Daniel Carbajal said:

Works great! Do you have any article/documentation about your DynamicProxyLibrary?
Fri, Dec 01 2006 13:09
 

VipulM said:

Thanks for your comments. No there is no documentation. What were you looking for?
Thu, Dec 14 2006 13:31
 

Monique said:

It's simple and works! Is it possible to call operations asynchronously?
Mon, Jan 22 2007 16:57
 

Mark Mullin said:

Very handy - one question though - how do I find information on [DataContract] entities - a lot of my services use DataContract to define params I found the contract name in Endpoints/Operations/<service>/Messages/Body/parts[0].Basetype, but I was hopelessly confused by that time
Mon, Feb 19 2007 15:54
 

Fasil Hayat said:

What about casting issues? My return values are not primitive types but custom objects. I only know what to cast to in run time. Casting results to the type 'Object' is not the plan.
Thu, Mar 15 2007 12:48
 

admin guy said:

Hi Fasil, Check out this article, this might be what you are looking for: http://www.netfxfactory.org/blogs/papers/archive/2007/01/24/An-effective-way-to-access-wcf-services.aspx
Tue, Apr 24 2007 10:56
 

me said:

ne 1 no a site which un blocks bebo n which aint which aint filtered at school ??? wb_xXx
Mon, May 21 2007 9:54
 

Vincent Parisot said:

What about net.tcp WCF services (like "net.tcp://localhost:8090/WcfSamples/DynamicProxy") ?
Fri, Jun 15 2007 10:49
 

Mark Richman said:

Hi Vipul! I was just trying your code against ebay's WSDL and got 281 import errors: "There was an error in importing the metadata." Here is the WSDL: http://developer.ebay.com/webservices/latest/eBaySvc.wsdl Am I doing something wrong? Thanks, Mark
Wed, Jun 27 2007 14:16
 

Jeff Jackson said:

How to handle going through firewall/proxy?
Mon, Jul 16 2007 13:11
 

Serge calderara said:

Does it means that with this sample with dynamic proxy I can extend at any time my WCF service without recompiling my application ? My idea is that I have basic services through WCF whcih are actullay available for a client application. Then the buisness ask me to extend service to provide. IT means interface update, proxy generation, client update, redeployement of the whole thing. Is there a way to deliver for instance just the new service methode like "PrintReport" , update config file and thats it ??? Check my initial post to a better view at : http://forums.microsoft.com/MSDN/showpost.aspx?postid=1916248&siteid=1
Thu, Jul 26 2007 13:02
 

Lee said:

Any idea how to handle net.tcp services?
Wed, Oct 03 2007 1:20
 

Darpan Dewan said:

I am trying to connect one of the service using dynamic proxy,but while load test we found that the CPU utilization becomes 100%,when ever the proxy is created using dynamic proxy code.I feel that is beacuse the proxy is created everytime.To avoid this I am making the dynamic proxy and keeping in Cache so that I dont have to create the proxy again and again,but with this I am getting the proxy time out. "The request channel timed out while waiting for a reply after 00:00:57.9844137. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout." 1) How can the timeout of the proxy (dynamic) be increased? 2) Is there any other alternate way to keep the proxy alive for a while so that the CPU usage is not 100%?
Tue, Nov 06 2007 5:50
 

Constantin said:

Greetings! My English is bad enough, but I hope you me will understand. =) at creation of a object of class DinamicProxyFactory appears exception with the message: " There was an error in importing the metadata ". Help, please!
Thu, Nov 08 2007 5:36
 

Harvey said:

Hi, Thanks for your great tool - can it be used to access generic xml web services (i.e. may not be a WCF service endpoint), such as on a J2EE server or whatever. Thanks, Harvey
Wed, Nov 14 2007 21:31
 

Jermaine Wright said:

FUCC ALL OF YA"LL
Mon, Nov 26 2007 13:03
 

did you know Ur moh said:

jdfhasjkdfhsjfs
Mon, Dec 17 2007 11:46
 

DF said:

Can anyone tell me how to handle basic authentication.
Fri, Dec 21 2007 13:54
 

DF said:

I got it Change DynamicProxyFactory class public DynamicProxy CreateProxy(string contractName, string contractNamespace) { ServiceEndpoint endpoint = GetEndpoint(contractName, contractNamespace); //Add these lines BasicHttpBinding b = (BasicHttpBinding)endpoint.Binding; b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; /////////// return CreateProxy(endpoint); }
Thu, Dec 27 2007 18:39
 

DF said:

You also need to set up username and password on client side ClientCredentials ccd = (ClientCredentials)simpleCalculatorProxy.GetProperty("ClientCredentials"); ccd.UserName.UserName = "yourusername"; ccd.UserName.Password = "yourpassword";
Thu, Dec 27 2007 18:48
 

Craig said:

Hi I get errors with the ImportAllBindings() when the WSDL contains HttpGet and HttpPost. Whats the best work-around resolve this? In DynamicProxyFactory.ImportMetadata() I invoked TurnOnGetAndPost() method in WebServiceSection, that didn't make a difference either.
Sat, Jan 05 2008 6:08
 

Anonymous said:

How does it handle duplex contract? E.g., those using WSDualHttp, NetTcp bindings. Can client supply a callback object that implements the callback interface, and dynamic proxy registers it with the service?
Mon, Jan 28 2008 0:38
 

Fei said:

How does it handle duplex contract? E.g., those using WSDualHttp, NetTcp bindings. Can client supply a callback object that implements the callback interface, and dynamic proxy registers it with the service?
Mon, Jan 28 2008 0:38
 

John said:

Hi, I’ve gone through your Dynamic Proxy. It’s really interesting and challenging stuff. It is perfectly creating proxy for any WSDL URI. I could call any service method with standard type parameter like integer, string … How can I call a WCF Service Method that having Composite Data Type Parameters. For Example: My WCF Servcie having following Contracts [ServiceContract] public interface IService1 { [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); } [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello"; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } } Please let me know how should I call above my method(GetDataUsingDataContract) using your Dynamic proxy CallMethod(string method, params object[] parameters). It would be very much helpful if you could explain with a sample code. Thanks in advance, John
Wed, Jan 30 2008 5:19
 

Greg Olmstead said:

Possibly the most brilliant contribution to this community EVER. This has enabled me to generalize my web projects in ways I could not fathom. Thank you.
Thu, Feb 14 2008 15:28
 

Marco said:

Is it possible in any way insert a pathname for a wsdl document instead of its uri?
Mon, Feb 25 2008 18:24
 

marco said:

I have a problem when i use this tool. The error is "There was an error in importing the metadata". What does it mean?
Thu, Feb 28 2008 11:54
 

shilpa said:

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?
Thu, Mar 13 2008 7:51
 

SaruMax said:

Thanks you un-help me! This proyect only work with http endpoints. If you want it to work with all kind of endpoints you have to change the DynamicProxyFactoryOptions.DownloadMetadata() method, coment everything this guy wrote there and put the following code: MetadataExchangeClient metaTransfer = new MetadataExchangeClient(new Uri(this.wsdlUri), MetadataExchangeClientMode.MetadataExchange); metaTransfer.ResolveMetadataReferences = true; MetadataSet otherDocs = metaTransfer.GetMetadata(); Collection<MetadataSection> results = new Collection<MetadataSection>(); foreach (MetadataSection doc in otherDocs.MetadataSections) { AddDocumentToResults(doc, results); } this.metadataCollection = results; Then edit the AddDocumentToResults() method with the following code: MetadataSection wsdl = (MetadataSection)document; results.Add(wsdl); Look&Feel BABY!!!! Belive it or now now it will WORK!! PD: use it with framework 3.5
Thu, Mar 13 2008 14:57
 

Joe said:

Hi, My team is wondering if there is a way to do this without recompiling on-the-fly? Apparently recompiling on-the-fly is a big security issue for us. Please advise. Thanks! Joe
Tue, Mar 18 2008 12:55
 

Fallon Massey said:

Does anyone know if this will work with ASMX WebServices as well?
Thu, May 15 2008 13:41
 

Anonymous said:

Thanks for using the sample and thanks for the comments. I apologize for not monitoring this regularly. 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 for the WSDL 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? Reflection. The sample provide an example of Complex type (DynamicComplexNumber.cs).
Mon, May 19 2008 12:35
 

vipulm said:

Thanks for using the sample and thanks for the comments. I apologize for not monitoring this regularly. 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 for the WSDL 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? Reflection. The sample provide an example of Complex type (DynamicComplexNumber.cs).
Mon, May 19 2008 12:36
 

suharjono said:

is it work for mobile client.. .NET CF 3.5?? If yes... How?
Wed, May 28 2008 2:43
 

vir said:

When I run it I get error - {"There was an error downloading 'http://localhost:8080/WcfSamples/DynamicProxy?wsdl'."}. How can I fix this?
Sun, Jul 06 2008 16:29

Add Comment

Name (required)
Web Site (optional)
Comment (required)
Add

Copyright © 2006 Microsoft Corporation. All Rights Reserved. | Terms of Use | Privacy Statement | Contact Us