Tuesday, September 22, 2009 7:22 PM Sean Feldman

Dynamic WCF Proxy

Our new system is entirely based of services (SOA solution). From the day one we had an issue with Visual Studio auto-magically generated proxies and management of those as system grew. Solution at that time was to create clients of the services dynamically, but the knowledge of WCF we had was a minimal. Now, 6+ months later, we finally getting to the point where I am comfortable and pleased with the solution. The interesting part is that WCF had that option all the time, we were not just educated enough to see it. Now we are.

The solution is to leverage ChannelFactory provided by WCF and create a client proxy from an end point defined in configuration file. Let me show the process from the client perspective:

var channel = new ChannelFactory<IWcfAppenderService>("WcfAppenderServiceEP").CreateChannel();

channel is our proxy. Reading carefully Programming WCF Services book from Yuval Lowy (page 48), it is clear that a channel has to be closed, regardless of the state it’s found in after invocation (faulted or not).

using(channel as IDisposable)
          channel.Append(loggingEventDataDto);

.NET using statement does the work.

 

This is it. To simplify the whole thing, we could intercept method calls on channel with a transparent proxy of ours and wrap with using statements – that way a user does not have to remember to do it each time.

Configuration file for a client would contain the minimal required information:

<system.serviceModel>
  <client>
    <endpoint address="http://localhost:4268/WcfAppenderService.svc"
        binding="wsHttpBinding" 
        contract="WcfAppender.Contracts.IWcfAppenderService" 
        name="WcfAppenderServiceEP">
    </endpoint>
  </client>
</system.serviceModel>

And the last missing part – contracts. A shared assembly with contracts would define all the service and data contracts. This is the only coupling between client and the server, which is logical, because contracts are coupling. image

I am not done with my WCF adventures, but I can definitely point to a great book by Yuval Lowy as a reference for WCF. 

Filed under:

Comments

# Twitter Trackbacks for Dynamic WCF Proxy - sfeldman.NET [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Dynamic WCF Proxy - sfeldman.NET         [asp.net]        on Topsy.com

# re: Dynamic WCF Proxy

Thursday, September 24, 2009 4:32 AM by Gino

In a 'closed system' (e.g. you own both sides) this is indeed the best way to do it. You could even drop the name in the config and use an empty string to create the channel.

You need to watch out though, simply disposing the channel can throw unexpected exceptions, just google for it.

I don't know if you're using any IoC/DI container, but Windsor has facilities for this built-in (including proper disposal). If you're into LinFu you might want to check out this: typesafe.be/.../creating-wcf-proxies-with-linfu.

# re: Dynamic WCF Proxy

Thursday, September 24, 2009 8:37 AM by Sean Feldman

@Gino,

you are absolutely right, both ends are a part of our system. I am not sure how different the interceptor that you showed in the linked example from what I was talking about, since using "using" solution does the same thing (closes on normal behaviour or aborts  on exception).

# Consuming ASMX Web Service With WCF

Friday, October 02, 2009 2:23 AM by sfeldman.NET

ASMX web services were a breakthrough when appeared on .NET platform. A lot of services were created

# Consuming ASMX Web Service With WCF | I love .NET!

Friday, October 02, 2009 3:16 AM by Consuming ASMX Web Service With WCF | I love .NET!

Pingback from  Consuming ASMX Web Service With WCF | I love .NET!