Dynamic applying of client’s configuration: Part II

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

By importing of endpoints from WsdlImporter, the resulting enpoint can not be used to dicertly create the instance of the proxy class at the client. To ilustrate this take a look on the following example:

MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress);

MetadataSet metadataSet = mexClient.GetMetadata();

WsdlImporter importer = new WsdlImporter(metadataSet);

ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();

ChannelFactory<TChannel> factory = new ChannelFactory<TChannel>(endpoints[0]);

ITestService proxy = factory.CreateChannel();

 

If the example is executed as it is, the last line will fail with following exception:

"Instance of MessagePartDescription Name='n1' Namespace='' cannot be used in this context: required 'Type' property was not set". The service used in this example is well known Calculator service.

This obviosly indicates that there is some failure in the imported contact, oltought no any problem could have been noticed. To create the working proxy following example can be used:

MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress);

MetadataSet metadataSet = mexClient.GetMetadata();

WsdlImporter importer = new WsdlImporter(metadataSet);

ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();

ChannelFactory<TChannel> factory = new ChannelFactory<TChannel>(endpoints[0].Binding, endpoints[0].Address );

ITestService proxy = factory.CreateChannel();

 

In this example, the proxy instance is created from binding and address, and not directly from the contract as in previous example. The good thing is that this workaround works fine. Unfortinately, the contract in this case which is used behind the proxy instance (factory.Endpoint.Contract) does not contain any custom policies if some used. Custom policies could be imported as shown following example:

MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress);

MetadataSet metadataSet = mexClient.GetMetadata();

WsdlImporter importer = new WsdlImporter(metadataSet);

 

importer.PolicyImportExtensions.Insert(0, new MyPolicyImporter());

ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();

ChannelFactory<TChannel> factory = new ChannelFactory<TChannel>(endpoints[0].Binding, endpoints[0].Address);

 

ITestService proxy = factory.CreateChannel();

 

This is exactly the bad part of this approach. My custom policies are just lost forever in the endpoint's contract, which can not be used. To woraround this, I used follwing code:

MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress);

MetadataSet metadataSet = mexClient.GetMetadata();

WsdlImporter importer = new WsdlImporter(metadataSet);

 

importer.PolicyImportExtensions.Insert(0, new MyPolicyImporter());

ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();

ChannelFactory<TChannel> factory = new ChannelFactory<TChannel> (endpoints[0].Binding, endpoints[0].Address);

 

… Copy All Custom Behaviors From endpoints[0] to factory.Endpoint …

 

ITestService proxy = factory.CreateChannel();

 

Most important step in this example is just described without of code. The code at this point is very straight on. You just have to travers all custom behavior elements (ServiceBehavior, ContractBehaviors and OperationBehaviors) contained in the bad endpoint (endpoints[0]) and append them the to enpoint which has been automatically created by the factory object.

This works fine, bu personaly, I would declare this solution as workoraund. I would be very glad if somebody could provide a real solution.


Posted Jan 11 2007, 03:42 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.