views:

307

answers:

4

I have an interface similar to this:

[ServiceContract]
public interface IBaseService<T>
{
    [OperationContract]
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    List<T> LoadById(string value);

    [OperationContract]
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    List<T> Load(string field, string value);
}

These methods are going to be implemented in several services. An example of implementation would be like this:

[ServiceContract]
[ServiceKnownType(typeof(ObjectDTO))]
public interface IObjectService : IBaseService<ObjectDTO>
{
}

My question is, is it possible to setup RESTful services using this architecture using UriTemplates on the OperationContracts in the base service interface? I tried searching around, but didn't see anyone else attempting to setup their RESTful services this way.

+3  A: 

This is purely my HO, but I strongly recommend you to try out OpenRasta instead of added-as-an-afterthought-to-a-RPC-based-framework REST support in WCF. This is surely possible in OpenRasta.

Anton Gogolev
+1 @Anton: Hey, tell us what you *really* think!
Ruben Bartelink
@Ruben Ummm... What do you mean?
Anton Gogolev
@Anton: I'm applauding that, in the spirit of an opinionated framework, you're stating your answer in such unequivocal terms. (i.e., translates as "dont hold back man - you're trying too hard to be PC right now") (And wanted to commend a good answer at the same time)
Ruben Bartelink
A: 

Inheritance with web services is something that I've always found very problematic to achieve, as such I strongly advise against it. Instead consider building your web services as stubs and then call into shared logic with similar structure to what you have done.

If you want a way to easily create web services that are configuration-free you should check out servicestack.net. It's an open source web service framework that lets your create REST-full JSON and XML webservices without any endpoint configuration, Service or Operation contracts - using only POCO DataContracts. Here is a live example showing the client and server code needed for creating simple REST web services called by Ajax and Silverlight clients:

mythz
+1  A: 

Given the way that REST services are consumed I'm not sure that there's any point using generics. You lose all the benefits of strong typing as you switch to what is a dynamic transport.

JSON serialisation is dynamic and not type aware, so why do you need to strongly type your output?

[ServiceContract]
public interface IBaseService
{
    [OperationContract]
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    List<object> LoadById(string value);

    [OperationContract]
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    List<object> Load(string field, string value);
}

Will produce the same JSON output.

Given that I think your best bet is just to not use generics in REST.

Keith
I am using generics and defining a ServiceKnownType due to the fact that the services need to be able to be consumed by a .NET client and by a Flex client. Using generics rather than an object was easier for the .NET client since I did not have to do any casting. Unfortunately, it appears that apart from using a 3rd party architecture to implement my service interfaces, I will not be able to use a base interface with generics.
Brandon
If you're always going .Net to .Net there's a lot less benefit using REST - you may be better off with plain old WCF.
Keith
We actually don't know who is going to consume our services. We do know that we are going .NET to .NET for sure and that our services will be hosted on different servers.
Brandon
A: 

WCF does not allow open generics in its service contracts. It does provide a KnownType and ServiceKnownType attributes that let you "inform" it of your polymorphisms. You can provide a static helper method to the ServiceKnownType attribute that return the collection of known types as well. I've used this to return the types that match my generic configuration.

frjames
I actually was able to get this to work. What I did was moved much of the logic that I wanted to expose via a base class in my example to WCF NET TCP services and then I setup a RESTful endpoint and treated it as a router service to my NET TCP services. This achieved my goal without having to replicate a lot of code.It seems to be a problem with Generics + RESTful Web Services in WCF.
Brandon