Consuming a WCF service in ASPX

I’m developing a WCF web service to support an application that is being developed by my current employer. In essence the web service will be taking requests from a Java client and this has gone smoothly by simply using the NetBeans IDE to discover the service and create the appropriate proxy. All good and it all works.

However, during our closed beta phases there are elements of the application that are not yet implemented in the client, namely a registration process. The short answer appeared to be to create a little website that purely acts as a front end for the web service (in particular allowing the user to register their instance of the software with the web service – the details are of no significance). As I say this was only scoped at a small scale public beta so was acceptable in our situation.

I had already been testing the service via a windows client purely to allow me to send requests to the service and to see the responses in a reasonably friendly fashion. All I did in this case was publish the service to a local IIS7 virtual directory and then add a web reference in the usual way (ie pointing it to http://localhost/myvirtualdirectory/myservice.svc?wsdl). This worked fine but for some reason I was getting errors when using the same approach in the aspx web site. The really cool (add sarcasm symbol here) bit was that the service was performing the action but the web site was choking on the response. The service uses MTOM to carry large binary data (ie packets of up to 1 MB) to and from the service and the error tended to indicate that the client was getting  the ‘attachment’ style payload that MTOM uses but was expecting text/xml. Straight away I thought that this had to do with a bad configuration setting but that turns out to be only half the story.

It turns out that I really should have paid attention to the documentation as it is crucial in this case.

So here’s how you do it.

Fire up a Visual Studio Command Prompt and locate your self in the directory where you want the output of this process to go. In short this process is going to create two files, one that contains a client proxy for the service that you can then code against in the service and one that will contain a config section to be merged into web.config.

Using the above URL as an example you use the command:

svcutil.exe http://localhost/myvirtualurl/myservice.svc?wsdl

svcutil will then create a file of the same name as the service (ie myservice.cs) which will contain a class called MyServiceClient. Simply add this file to your web application and its ready to be used by creating instances of MyServiceClass and calling its methods. The contents of output.config are to be merged into the web.config file. I simply copied everything between and pasted it before the closing tag in my web.config file.

Not only does this provide you with the correct configuration for the web.config file but the proxy that is generated also features a set of constructors which at this stage I assume are allowing the correct endpoints and bindings to be used (I’ve only had a quick glance at this stage – It works – but I don’t exactly know why yet – however I think its got to do with correct binding and endpoint configurations).

It all works perfectly. Sorry this post is rushed, but its another one of my ‘get it down before you forget’ posts. I may return to this to flesh out the detail soon, but for now if you have any questions or think that this ‘smells’ a little familiar but need more detail, please feel free to comment and I will endeavour to help where I can.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s