Flickr WCF Part II - Hosting WCF Components In-Process

February 16th, 2009

This is Part Two in an on-going series of articles about WCF components, all centered around a simple component I created to talk to the Flickr api. You can find Part I here.

Now that we’ve hosted the Flickr WCF component in IIS, let’s take the same service definition and implementation and do something more interesting with them than just spitting out a list of URLs. It would be cool if we could see each of the pictures and cycle through them. And, so that I can point out a potential WCF gotcha, let’s do this in a WinForms application.

Let’s also host the component in process. In process components should always use named pipes. I’ve found that the easiest possible way to host a component in process is by using Juval Lowy’s ServiceModelEx library. You can download the library at this link. We’ll use his InProcFactory class which he describes in some detail on pages 60-63 of his WCF Bible, “Programming WCF Services.”
With InProcFactory all you need to do is to add references to the class libraries which contain your component’s contracts and implementation.
Read more…

Tad .net, servicemodelex, wcf , , ,

StackOverflow update

February 11th, 2009

I have recently had two of my WCF related answers to questions on StackOverflow rated as top answers!

Are Enterprise Level DataContracts a Good Practice?

Logic first, WCF security later?

I enjoy going through the StackOverflow questions that are tagged as WCF related on a near daily basis. I have learned an awful lot just reading other people’s answers and sometimes the questions inspire me to go learn something I hadn’t thought about.

Also, tonight I asked:

What are your favorite resources concerning RESTful WCF?

If you have a good answer, I’d love to hear it!

Tad .net, stackoverflow, wcf , ,

Flickr WCF Part I - Basic Manual WCF Component

February 10th, 2009

This is the first part in a series of blog posts where I see how many different aspects of WCF I can exercise with the same basic component definition. In this article we’re going to define our contracts, create a service, create a IIS host, hand-code a proxy and have a Console app spit out URLs to the 100 most recent photos uploaded to Flickr.

This and subsequent articles assume some basic working knowledge of WCF. If you’ve worked your way through a couple of tutorials, you’ll be fine.

The basic logic of the calling out to Flickr and the extension method I use are described in the previous blog post which is a prelude to this series. Please take a look at that article if you have trouble following along.

For simple cases like the one we’re working on here, I find that hand coding all of the WCF logic is the easiest and most straightforward way to make my code work. Using the svcutil.exe or the Add Service Reference feature in VS 2008 works, but both methods add unnecessary cruft that add to the complexity and understanding of the code. So, here we go!

Read more…

Tad .net, wcf

Calling the Flickr API getRecent method with c# and Linq to XML

February 7th, 2009

UPDATE: I have updated this article to now use the flickr.interestingness.getList method instead of getRecent. I love getRecent, but out of every 3 to 400 pictures there’s almost always one or two pictures that most audiences would consider offensive. All of the photos returned by flickr.interestingness.getList are artistic and very few of them would be considered offensive by most people.

One of my favorite features of Flickr is browsing through photos that have just been uploaded.  You can get a glimpse of this on Flickr’s home page, but I decided I’d write some code so I could look at a bunch of them all at once.  I also like making calls against other web API’s (like from Twitter, FriendFeed or delicious).

Read more…

Tad .net, extensions, linq , , , , , , ,

Best of WCF, January 25, 2009

January 25th, 2009

Two way client server network communication

A question on StackOverflow about 2 way WCF communication.  The answers provide some excellent links to resources concerning WCF duplex messaging.  Chapter 5 of Juval Lowy’s excellent Programming WCF Services has a lengthy discussion on Duplex messenges including Server/Client event models - really cool stuff!

Async Operations in WCF: Event Based Model

Along the same lines, I found this gem of a 5 part series on two-way messaging in WCF the other day.  Excellent article - I hope to have a screencast and sample solution of my own concerning Events in WCF before long.

Tad wcf

Manual WCF - An extension

January 18th, 2009

While going through the WCF questions on StackOverflow a few days ago, I found a link to an excellent article on setting up WCF manually from bottom to top written by Miguel A. Castro for Code Magazine.  You can find the article here.  Please go and read it before proceeding.

The thrust of the article is to show how to create a WCF contract, service, host, proxy and client without relying on Visual Studio or other external tools to create anything for you.  In particular Castro prefers to create his own .config files and proxies.

In the article, Castro shows two different ways to set up a client proxy for his service.  The author of the question that lead me to the article had trouble with one of the proxies, shown below:

    <br />using System;    <br />using System.ServiceModel;</p>  <p>namespace CoDeMagazine.ServiceArticle   <br />{    <br />public class AdminClient : IProductAdmin    <br />{    <br />public AdminClient()    <br />{    <br />IProductAdmin productAdminChannel =    <br />new ChannelFactory<iproductadmin>().    <br />CreateChannel();    <br />}</p>  <p>IProductAdmin productAdminChannel = null;</p>  <p>#region IProductAdmin Members</p>  <p>public void UpdateProduct(   <br />ProductData product)    <br />{    <br />productAdminChannel.UpdateProduct(    <br />product);    <br />}</p>  <p>public void DeleteProduct(Guid productID)   <br />{    <br />productAdminChannel.DeleteProduct(    <br />productID);    <br />}</p>  <p>#endregion   <br />}</p>  <p>}   <br />

The problem here is that as far as I can see, I can only get this to work by referencing the endpoint name pointed to by the client. If I change constructor to look like this:

</p>  <p>public AdminClient()   <br />{    <br />IProductAdmin productAdminChannel =    <br />new ChannelFactory<iproductadmin>("endpointName").    <br />CreateChannel();    <br />}    <br />

given an endpoint configuration in the client like:

</p>  <p><endpoint address="<br" />http://localhost:9224/ProductService.svc   <br />binding="wsHttpBinding"    <br />name="endpointName"    <br />contract="CoDeMagazine.ServiceArticle.    <br />IProductBrowser" /></p>  <p>

it works! I’m not sure if Mr Castro is just wrong or if there’s something that I and the other folks on StackOverflow missed.

Either way, I created a sample project to share and a very brief screen cast to go along with it. My example is simpler, but fully working - you shouldn’t have to do any extra work to get it up and running and you can apply the concepts in your own work.

ManualWCFExample

View the screencast here.

I may well have made a goof or error in this post, the code linked, or in the screencast.  If you find something you’d like to correct, feel free to leave a comment below.

Thanks for reading!

Tad screencast, wcf

Clicky Web Analytics