To use your WCF service with a WCF client you will need to get access to the service. WCF does this by supplying a transparent proxy of the service to your client. This can then be used to invoke remote methods on the service.

So in this post we will look into the ways to generate such proxy.

What does this post cover

  • General overview over proxy call forwarding
  • Generating a proxy with Visual Studio
  • Generating a proxy with SvcUtil.exe
  • Parts of programmatic and administrative client configuration
  • Use Channel factory to invoke service methods directly

What does this post not cover

  • How to create a service
  • Configuration of WCF services
  • WCF architecture

Alternatives

I’ll show you three different possibilities to create a client proxy, others would be to consume the WCF generated WSDL. But this only makes sense if you are not using WCF on the client side. Else you would gain pretty much nothing but more overhead and complexity. If you want to fine tune the proxy generation better use the extensibility and interception capabilities of WCF.

How does a service call work in general

In general WCF needs a proxy to let a client request a method of the given WCF service. The client then uses this proxy to forward a method call of the specified service contract.
This forwarded call goes throught the appropriate Channels to create the SOAP message, applies the configured behaviors, is then forwarded to the instance context, in which the service instance is hosted, and finally is executed there. If the call returns at the service, it returns in the same way through all the appropriate channels.

To invoke a proxy, the client needs a way to import the service description first.
The imported proxy equalsCLR interface with the same representation as the service contract. For each servicetype per endpoint that the client should use you will need to supply a specific proxy to the client.

Use of proxy and generating proxies

Besides the operations of the service, the proxy has lifecycle related methods and connection related methods.
To make the programming interface as convenient as possible for application programmers the proxy encapsulates every aspect of the service. Which includes the location, the implementation, the technology, runtime platform, transport scheme, and servicebehavior.

To utilize a proxy on the client side in a performant manner, a best practice is to always Close the client if you are finished with the given call.

We are now going to look into the first way to generate a proxy which is via Visual studio and a self-hosted WCF service.

Generate a Proxy with Visual Studio

As described above you ‘ll need to discover the service first to utilize it with a proxy at client side. There are two industry standards to infer the service information. The first one is the use of http-Get for your metadata. For this you simply add a service endpoint that exposes your service with HttpBinding. The other is the notion of a MetadataExchange (MEX) Endpoint. (To run your process with an http endpoint make sure to run VS as an admdinistrator.)

For the upcoming examples we are using the following Servicecontract and implementation:

 

Then add MEX endpoint like the following snippet. Use either programmatic or adiminstrative configuration (but recall I personally prefer programmatic)

If you have set up your WCF service and are hosting it in your self-hosting process you will need to take the following steps:

  1. Start your service self-hosting process without debugging (as administrator for MEX http rights)
  2. Open your Solution with the client
  3. Rigth click References of the assembly
  4. choose AddService Reference from context menu
  5. follow the instructions in the dialog (see below)

The following dialog lets you either put in your MEX endpoint URL or discover it.

Notice that the discovered Name is the same as my defined Name in the Service Contract Attribute constructor argument for the Name. Here you can also configure advanced settings with the Advanced… Button. But we are ignoring the configuration possibilities for the proxy for now.

If you confirm the dialog with OK, the solution gets new part with ServiceReferences/ConnectedServices, where you will find your Proxy. To finally generate the proxy you need to use the UpdateServiceReference in the ContextMenu of the generated ServiceReference.

You can now proceed to use the proxy to invoke methods on the service like this:

And this is how the auto generated proxy would look like:

 

Generate a Proxy with SVCUtil.exe

The next way to generate a proxy is to use the command line utility called SvcUtil.exe. This gives you the main advantage of using it in pre build steps of your build machine. The .exe can be found in C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools. Your path may deviate a little, just look for Microsoft SDKs in the program files

The SVCUtil.Exe employs numerous switches, like the /out switch to determine how you want to name your Proxy file. Most of these correspond to the configuration options that you can see in the dialog of Visual studio to generate a proxy.

In general you can use SvcUtil like so:

 

Summary and Outlook to second page

We saw how you can generate a proxy and why you would do this. We explored how one can generate a proxy with Visual Studio and how one can go about creating a proxy with SvcUtil.exe. Also we looked a little bit at how WCF handles the remote calls to the service.

Leave a Reply