WCF Service Extreme Make-over - Part 3
IMPORTANT: It's really important that you read part 1 and part 2 before reading Part 3. I'll assume you did, so a lot might not make any sense unless you've read it.
Introduction
So in the previous two posts, we've established how we wanted to use and host our WCF Services, and how we actually hosted them.
Just some things to remember:
- Our Service should be usable by it's URL alone, which is configured using an AppSettings value with Key of "IMathServiceUrl".
- We want to access this service using var mathService = ServiceResolver.Resolve<IMathService>();
So, without further ado, let's have our service working. Oh, wait... Almost forgot I work for ThoughtWorks now. So let's have a failing test.
The very beginning - Our Failing Test
Let's have a test that calls on our Math Service, like this one:
[TestFixture]
public class TestServiceCall
{ protected IMathService mathService;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{ mathService = ServiceResolver.Resolve<IMathService>();
}
[Test]
public void ShouldCallAdd()
{ Assert.That(mathService.Add(1, 2), Is.EqualTo(3));
}
}
As you can see we initialize an instance of IMathService in our Fixture SetUp, and then just use it whenever we want it. Let's check the config file for the Test Project, just to make sure we are not using any WCF sorcery:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="IMathServiceUrl" value="http://localhost/MathService/MathService.svc" />
</appSettings>
</configuration>
As promised we are just setting the Url for that service's endpoint. Let's run our test and check that it fails.
The actual Service Resolver
Cool, it doesn't even build, since it doesn't know any ServiceResolver classes. So let's change that by adding a ServiceResolver class to the ServiceInfrastructure project, like this one:
using System.ServiceModel;
namespace Stormwind.EasyWcf.ServiceInfrastructure
{ public class ServiceResolver
{ public static T Resolve<T>()
{ return ChannelFactory<T>.CreateChannel(
new BasicHttpBinding(),
new EndpointAddress(UrlHelper.GetUrlFor<T>()));
}
}
}
What? That's it? I'm sure you must be mistaken!
Not at all my dear friend, and you can thank the WCF Team for that. They made it very easy to create a client-side proxy using code, instead of a Service Reference.
Now, let's run our test again, and who could foresee it! GREEN!
Conclusion
That's all there is to it, though. Even though I would like to keep rambling on and on about how hard it is, it wasn't even hard. I just had a hard time finding resources that explained the whole process. That's why I decided on writing a comprehensive post on it.
I really hope that these three posts have helped you grasp how to use WCF in a more developer-friendly way. I'll post next on how to do secure bindings and stuff like that using this approach, which is quite good to ENFORCE security, since the client and host cannot mess with the security settings. Once again, it has drawbacks on flexibility.
The full code for Stormwind.EasyWcf is available through our Subversion server at: http://svn.stormwindproject.org/svn/Personal/Heynemann/Stormwind.EasyWcf.
Cheers.