Use DomainServices as Service References

30 Nov

DomainServices was introduces as part of WCF RIA Services. WCF RIA Services offers a solution on how we can build a middle-tier having clients and servers.

Using a DomainService in a Silverlight client is very easy using WCF RIA Services – the WCF RIA Services link between the client and the server will automatically generate the client side code that we need to communicate with the server.

2010-11-30_161311

To avoid writing middle-tier functionality more than once we want to be able to reuse the DomainService and the logic inside of it to all of our projects. Most project types beside Silverlight do not understand WCF RIA Services yet. But as a DomainService ultimately just is a WCF Service we can just make a normal Service Reference (or ChannelFactory) to it and reuse the logic.

For that to work you need to make a few adjustments to the project hosting the service. When you add a DomainService to your project a DomainServiceHttpModule http module is added to your web.config and ASP.NET compatibility is turned on.

2010-11-30_163517

The DomainServiceHttpModule dynamically generates a WCF Service based on our DomainService. In this sample I have added a DomainService called DomainService1 in the root of my ASP.NET web project called WebApplication1. The web project is using port 6479. Based on this information I can point to my dynamically generated WCF Service from my browser:

http://localhost:6479/Services/WebApplication1-DomainService1.svc

2010-11-30_172225

If I try to make a Service Reference to the WCF Service I will get an error:

There was no endpoint listening at http://localhost:6479/Services/WebApplication1-DomainService1.svc that could accept the message.

Before I can make a Service Reference to the service I need to define an endpoint in the web.config. First I need to add a custom configuration section for DomainServices within System.ServiceModel.

2010-11-30_200119

Using the RIA Services Toolkit I can now define a SOAP endpoint (I need to add a reference to Microsoft.ServiceModel.DomainServices.Hosting found in the Microsoft SDKs/RIA Services/v1.0/Toolkit/Libraries folder).

2010-11-30_200643

Finally I can add a Service Reference to the service and use it as if it was a regular WCF Service.

2010-11-30_200816

2010-11-30_200917

You can download the full sample from http://bit.ly/dOVMVR.

by xamlgeek
@thomasmartinsen

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

%d bloggers like this: