WCF RIA Services Binding Deep dive

Note: This is based on WCF RIA Services beta, and some stuffs can change in a future release.

As many of you may know, WCF RIA Services uses WCF. WCF uses EndPoints where we specify the ABC (Address, Binding and Contract). By default WCF RIA Services uses its own ServiceHost, call DomainServiceHost. The DomainServiceHost will add EndPoints programmatically and it will add three kind of EndPoints, WebHttpBinding (for REST), BasicHttpBinding and BinaryHttpBinding. All binding will have a MaxReceivedMessageSize set to ”2147483647”. The address of the three default bindings added are the following:

For WebHttpBinding: “baseAddress" (REST with JSON Endpoint)

For BasicHttpBinding: “baseAddress” + “/soap” (SOAP with XML Endpoint)

For BinaryHttpBinding: “baseAddress” + “/binary” (SOAP with Binary Endpoint)

If you want to change the binding used, you can pass the address with any of the above prefix after the baseAddress to the constructor when you create the DomainContext on the client-side:

 var myDomainContext = MyDomainContext(
     new Uri("SilverlightApplication1-Web-MyDomainService.svc/binary", UriKind.Relative)));


The security mode used on the bindings are “Transport” for https and the rest if a credential exists is “Transport credential only”. What's worry me is that the Metadata behavior’s HTTP get is enabled, so everyone can get information about the DomainService. Because of security reasons when using WCF, it’s advisable to turn it off in production.

WCR RIA Services uses a DomainServiceHostFactory which inherits from the SerivceHostFactory. The DomainServiceHostFactory has the following method:

protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new DomainServiceHost(serviceType, baseAddresses);
}


The serviceType is the type of the service and the baseAddresses are an array of addresses which will be added as addresses to EndPoints. We can create our own ServiceHost if we want, or just create our own SerivceHost factory to just add more EndPoints for specific addresses, or filter out some addresses we don’t want to allow. If we creates our own ServiceHost, we need to inherits the ServiceHost base class, and also create our own ServiceHostFactory. Here is an example of a ServiceHostFacotry which will return a custom ServiceHost:

public class MyDomainServiceHostFactory : ServiceHostFactory
{
public MyDomainServiceHostFactory() {}

protected override ServiceHost CreateServiceHost(
Type serviceType,
Uri[] baseAddresses)
{
return new MyDomainServiceHost(serviceType, baseAddresses);
}
}


The MyDomainServiceHost could for example look like this:

public class MyDomainServiceHost : DomainServiceHost
{
public MyDomainServiceHost(
                      Type domainServiceType,
                      params Uri[] baseAddresses) : base(
                                                     domainServiceType;
baseAddresses)
{
}



 

 


protected override void AddEndpoints()
{
//...

base.AddServiceEndPoint(...);
}
}


Note: The DomainServiceHost, is the WCF RIA’s own custom ServiceHost.

We can then use our custom ServiceHostFactory by adding a .svc file to for our DomainServices to the Services directory (If you don’t have a Services directory in the root folder of your web project, just add one) and specify a Factory by using the Factory attribute of the ServiceHost directive:

<%@ ServiceHost Service="SilverlightApplication1.Web.MyDomainService"
Factory="SilverlightApplication1.Web.MyDomainServiceHostFactory" %>


To get the name of the .svc file, you simply use the type of the DomainSerivce and replace “.” with a “-”. So if the type is as above “SilverlightApplication1.Web.MyDomainService”, the .svc file should have the following name: “SilverlightApplication1-Web-MyDomainService.svc”.

If you want to know when I post new blog posts, you can follow me on twitter: http://www.twitter.com/fredriknormen

1 Comment

  • Hi there,

    In my version of RIA Services (1.0) the DomainServiceHost has no AddEndpoints() method to override.
    What version is this based on?

Comments have been disabled for this content.