简体   繁体   English

使用泛型接口的WCF RESTful服务

[英]WCF RESTful Services Using Interface with Generics

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? 我的问题是,是否可以使用基本服务接口中OperationContracts上的UriTemplates使用此体系结构来设置RESTful服务? I tried searching around, but didn't see anyone else attempting to setup their RESTful services this way. 我尝试四处搜索,但没有看到其他人尝试以此方式设置其RESTful服务。

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. 这纯粹是我的工作方式,但是我强烈建议您尝试OpenRasta,而不要在WCF中添加对基于RPC的框架的REST支持。 This is surely possible in OpenRasta. 在OpenRasta中肯定可以做到这一点。

Inheritance with web services is something that I've always found very problematic to achieve, as such I strongly advise against it. Web服务的继承是我一直发现很难解决的问题,因此我强烈建议不要这样做。 Instead consider building your web services as stubs and then call into shared logic with similar structure to what you have done. 而是考虑将您的Web服务构建为存根,然后以与您所做的类似的结构调用共享逻辑。

If you want a way to easily create web services that are configuration-free you should check out servicestack.net . 如果您想要一种轻松创建无配置的Web服务的方法,则应查看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. 这是一个开放源代码的Web服务框架,使您无需使用任何端点配置,服务或操作合同即可创建REST-full JSON和XML Web服务,而仅使用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: 这是一个实时示例,显示了创建由Ajax和Silverlight客户端调用的简单REST Web服务所需的客户端和服务器代码:

Given the way that REST services are consumed I'm not sure that there's any point using generics. 考虑到使用REST服务的方式,我不确定使用泛型是否有用。 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? JSON序列化是动态的并且不支持类型识别,那么为什么需要强烈键入输出?

[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. 将产生相同的JSON输出。

Given that I think your best bet is just to not use generics in REST. 考虑到我认为您最好的选择就是不要在REST中使用泛型。

WCF does not allow open generics in its service contracts. WCF不允许在其服务合同中使用开放式仿制药。 It does provide a KnownType and ServiceKnownType attributes that let you "inform" it of your polymorphisms. 它确实提供了KnownType和ServiceKnownType属性,可让您“告知”您的多态性。 You can provide a static helper method to the ServiceKnownType attribute that return the collection of known types as well. 您可以向ServiceKnownType属性提供静态帮助程序方法,该方法也返回已知类型的集合。 I've used this to return the types that match my generic configuration. 我用它来返回与我的通用配置匹配的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM