在.NET Core上探索ServiceStack的简单快速的Web服务

I've been doing .NET Open Source since the beginning. Trying to get patches into log4net was hard without things like GitHub and Twitter. We emailed .patch files around and hoped for the best. It was a good time.

从一开始我就一直在做.NET开放源代码。 没有GitHub和Twitter之类的东西,试图将补丁添加到log4net很难。 我们通过电子邮件发送.patch文件,并希望能做到最好。 那是曾是一段美好时光。

There's been a lot of feelings around .NET Open Source over the last decade or so - some positive, some negative. There's been some shining lights though and I'm going to do a few blog posts to call them out. I think having .NET Core be cross platform and open source will be a boon for the .NET Community. However, the community needs to also help out by using non-Microsoft OSS, supporting it, doing PRs, helping with docs, giving talks on new tech and spreading the word.

在过去十年左右的时间里,人们对.NET Open Source产生了很多感觉-有些是积极的,有些是消极的。 虽然有一些闪亮的灯光,但我将写一些博客文章来称呼他们。 我认为让.NET Core成为跨平台和开源将是.NET社区的福音。 但是,社区还需要通过使用非Microsoft OSS,提供支持,进行PR,帮助文档,就新技术进行讨论并进行推广来提供帮助。

While some OSS projects are purely volunteer projects, ServiceStack has found some balance with a per-developer pricing model. They also support free usage for small projects. They've got deep integration with all major IDEs and support everything from VS, Xcode, IntelliJ IDEA, and the commandline.

虽然某些OSS项目纯粹是志愿者项目,但ServiceStack在按开发人员定价的模型中找到了一些平衡点。 他们还支持小型项目的免费使用。 他们已经与所有主要的IDE进行了深度集成,并支持VS,Xcode,IntelliJ IDEA和命令行中的所有内容

One major announcement in the least few days as been ServiceStack 4.5.2 on .NET Core! Effectively one year to the day from the feature request and they did it! Their announcement paragraph says it best, emphasis mine.

至少几天以来,.NET Core上的ServiceStack 4.5.2就是一个重要的公告! 从功能要求到实际上一天到一天都做到了! 他们的公告段落说得最好,重点是我的。

Whilst the development and tooling experience is still in a transitionary period we believe .NET Core puts .NET Web and Server App development on the cusp of an exciting future - the kind .NET hasn’t seen before. The existing Windows hosting and VS.NET restraints have been freed, now anyone can develop using .NET’s productive expertly-designed and statically-typed mainstream C#/F# languages in their preferred editor and host it on the most popular server Operating Systems, in either an all-Linux, all-Windows or mixed ecosystem. Not only does this flexibility increase the value of existing .NET investments but it also makes .NET appeal to the wider and highly productive developer ecosystem who’ve previously disregarded .NET as an option.

尽管开发和工具开发经验仍处于过渡期,但我们相信.NET Core将.NET Web和Server App开发置于激动人心的未来的风口浪尖上-以前从未见过这种.NET。 现已释放了现有的Windows托管和VS.NET限制,现在任何人都可以在其首选的编辑器中使用.NET高效,专业设计和静态类型化的主流C#/ F#语言进行开发,并将其托管在最受欢迎的服务器操作系统中,全Linux,全Windows或混合生态系统。 注意,这种灵活性不仅可以增加现有.NET投资的价值,而且还可以使.NET吸引更广泛且生产力更高的开发人员生态系统,而这些开发人员生态系统以前已不考虑.NET作为选择。

Many folks ran (and run) ServiceStack on Mono, but it's time to move forward. While Mono is still a fantastic stack on many platforms that .NET Core doesn't support, for mainstream Linux, .NET Core is likely the better choice.

许多人在Mono上运行(并运行)ServiceStack,但现在该迈出第一步了。 尽管Mono在.NET Core不支持的许多平台上仍然是理想的堆栈,但是对于主流Linux,.NET Core可能是更好的选择。

If you’re currently running ServiceStack on Mono, we strongly recommend upgrading to .NET Core to take advantage of its superior performance, stability and its top-to-bottom supported Technology Stack.

如果您当前在Mono上运行ServiceStack ,我们强烈建议升级到.NET Core ,以利用其卓越的性能,稳定性和自上而下支持的技术堆栈。

I also want to call out ServiceStack's amazing Release Notes. Frankly, we could all learn from Release Note this good - Microsoft absolutely included. These release notes are the now Gold Standard as far as I'm concerned. Additionally, ServiceStack's Live Demos are unmatched.

我还想谈一谈ServiceStack惊人的发行说明。 坦白地说,我们都可以从发行说明中学到这一点-绝对包括Microsoft。 就我而言,这些发行说明是现在的黄金标准。 此外, ServiceStack的实时演示无与伦比

Enough gushing. What IS ServiceStack? It's a different .NET way for creating web services. I say you should give it a hard look if you're making Web Services today. They say this:

涌得足够多了。 什么是ServiceStack? 这是创建Web服务的.NET的另一种方式。 我说,如果您今天要进行Web服务,则应该仔细看一下。 他们说

Service Stack provides an alternate, cleaner POCO-driven way of creating web services.

Service Stack提供了一种更简洁的POCO驱动的创建Web服务的方式。

  • Simplicity

    简单

  • Speed

    速度

  • Best Practices

    最佳实践

  • Model-driven, code-first, friction-free development

    模型驱动,代码优先,无摩擦的开发

  • No XML config, no code-gen, conventional defaults

    没有XML配置,没有代码生成,常规默认值

  • Smart - Infers intelligence from strongly typed DTOs

    智能-推断强类型DTO的智能

  • .NET and Mono

    .NET和Mono

  • Highly testable - services are completely decoupled from HTTP

    高度可测试-服务与HTTP完全分离

  • Mature - over 5+ years of development

    成熟-超过5年的发展

  • Commercially supported and Continually Improved

    商业支持并不断改进

  • and most importantly - with AutoQuery you get instant queryable APIs. Take a look at what AutoQuery does for a basic Northwind sample.

    最重要的是-通过自动查询,您可以获得即时可查询的API。 看一下自动查询对Northwind示例的作用

They've plugged into .NET Core and ASP.NET Core exactly as it was design. They've got sophisticated middleware and fits in cleanly and feels natural. Even more, if you have existing ServiceStack code running on .NET 4.x, they've designed their "AppHost" such that moving over the .NET Core is extremely simple.

他们已经完全按照设计方式插入了.NET Core和ASP.NET Core。 他们拥有完善的中间件,干净利落,感觉自然。 甚至,如果您已经在.NET 4.x上运行了现有的ServiceStack代码,他们将设计自己的“ AppHost”,从而在.NET Core上移动非常简单。

ServiceStack has the standard "Todo" application running in both .NET Full Framework and .NET Core. Here's two sites, both .NET and both ServiceStack, but look what's underneath them:

ServiceStack具有在.NET Full Framework和.NET Core中运行的标准“ Todo”应用程序。 这是.NET和ServiceStack这两个站点,但是请看它们下面是什么:

服务堆栈入门 (Getting Started with Service Stack)

There's a million great demos as I mentioned above with source at https://github.com/NetCoreApps, but I love that ServiceStack has a Northwind Database demo here https://github.com/NetCoreApps/Northwind. It even includes a Dockerfile. Let's check it out. I was able to get it running in Docker in seconds.

如上所述,我在https://github.com/NetCoreApps上有上百万个演示源,但我喜欢ServiceStackhttps://github.com/NetCoreApps/Northwind上提供了Northwind数据库演示。 它甚至包括一个Dockerfile 。 让我们来看看。 我能够在几秒钟内使其在Docker中运行。

>git clone https://github.com/NetCoreApps/Northwind
>cd Northwind
>docker build -t "northwindss/latest" .
>docker run northwindss/latest
Project Northwind.ServiceModel (.NETStandard,Version=v1.6) was previously compiled. Skipping compilation.
Project Northwind.ServiceInterface (.NETStandard,Version=v1.6) was previously compiled. Skipping compilation.
Project Northwind (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Hosting environment: Production
Content root path: /app/Northwind
Now listening on: https://*:5000
Application started. Press Ctrl+C to shut down.

Let's briefly look at the code, though. It is a great sample and showcases a couple cool features and also is nicely RESTful.

不过,让我们简要地看一下代码。 这是一个很好的示例,展示了几个很酷的功能,并且很好用。

There's some cool techniques in here. It uses SqLITE for the database and the database itself is created with this Unit Test. Here's the ServiceStack AppHost (AppHost is their concept)

这里有一些很酷的技术。 它对数据库使用SqLITE,并且数据库本身是使用此Unit Test创建的。 这是ServiceStack AppHost(AppHost是他们的概念)

public class AppHost : AppHostBase
{
public AppHost() : base("Northwind Web Services", typeof(CustomersService).GetAssembly()) { }

public override void Configure(Container container)
{
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(MapProjectPath("~/App_Data/Northwind.sqlite"), SqliteDialect.Provider));

//Use Redis Cache
//container.Register<ICacheClient>(new PooledRedisClientManager());

VCardFormat.Register(this);

Plugins.Add(new AutoQueryFeature { MaxLimit = 100 });
Plugins.Add(new AdminFeature());

Plugins.Add(new CorsFeature());
}
}

Note host the AppHost base references the Assembly that contains the CustomersService type. That's the assembly that is the ServiceInterface. There's a number of Services in there - CustomersService just happens to be a simple one:

注意主机AppHost基引用了包含客户服务类型的程序集。 这就是ServiceInterface的程序集。 那里有很多服务-客户服务恰好是一个简单的服务:

public class CustomersService : Service
{
public object Get(Customers request) =>
new CustomersResponse { Customers = Db.Select<Customer>() };
}

The response for /customers is just the response and a list of Customers:

/ customers的响应只是响应和客户列表:

[DataContract]
[Route("/customers")]
public class Customers : IReturn<CustomersResponse> {}

[DataContract]
public class CustomersResponse : IHasResponseStatus
{
public CustomersResponse()
{
this.ResponseStatus = new ResponseStatus();
this.Customers = new List<Customer>();
}

[DataMember]
public List<Customer> Customers { get; set; }

[DataMember]
public ResponseStatus ResponseStatus { get; set; }
}

Customers has a lovely clean GET that you can see live here: http://northwind.netcore.io/customers. Compare its timestamp to the cached one at http://northwind.netcore.io/cached/customers.

客户有一个非常干净的GET,您可以在此处实时查看: http : //northwind.netcore.io/customers 。 将其时间戳与http://northwind.netcore.io/cached/customers上的已缓存时间戳进行比较。

[CacheResponse(Duration = 60 * 60, MaxAge = 30 * 60)]
public class CachedServices : Service
{
public object Get(CachedCustomers request) =>
Gateway.Send(new Customers());

public object Get(CachedCustomerDetails request) =>
Gateway.Send(new CustomerDetails { Id = request.Id });

public object Get(CachedOrders request) =>
Gateway.Send(new Orders { CustomerId = request.CustomerId, Page = request.Page });
}

You may find yourself looking at the source for the Northwind sample and wondering "where's the rest?" (no pun intended!) Turns out ServiceStack will do a LOT for you if you just let it!

您可能会发现自己正在查看Northwind样本的来源,并且想知道“其余的地方在哪里?” (没有双关语!)事实证明,如果您愿意,ServiceStack会帮您做很多!

The Northwind project is also an example of how much can be achieved with a minimal amount of effort and code. This entire website literally just consists of these three classes . Everything else seen here is automatically provided by ServiceStack using a code-first, convention-based approach. ServiceStack can infer a richer intelligence about your services to better able to provide more generic and re-usable functionality for free!

Northwind项目也是一个示例,该示例可以用最少的精力和代码来实现多少目标。 从字面上看,整个网站仅包含三个 ServiceStack会自动使用基于约定的基于代码的方法来提供此处看到的所有其他内容 ServiceStack可以推断出有关您的服务的更丰富的情报,以便更好地免费提供更通用和可重用的功能!

ServiceStack is an alternative to ASP.NET's Web API. It's a different perspective and a different architecture than what Microsoft provides out of the box. It's important and useful to explore other points of view when designing your systems. It's especially nice when the systems are so thoughtfully factored, well-documented and designed as ServiceStack. In fact, years ago I wrote their tagline: "Thoughtfully architected, obscenely fast, thoroughly enjoyable web services for all."

ServiceStack是ASP.NET的Web API的替代方法。 与Microsoft开箱即用的产品不同,它的观点和体系结构也不同。 在设计系统时探索其他观点是重要且有用的。 当系统经过深思熟虑,文档详尽并设计为ServiceStack时,尤其好。 实际上,几年前,我写了他们的标语:“为所有人提供经过精心设计的,令人讨厌的快速,完全愉悦的Web服务。”

Have you used ServiceStack? Have you used other open source .NET Web Service/API frameworks? Share your experience in the comments!

是否使用过ServiceStack? 您是否使用过其他开源.NET Web服务/ API框架? 在评论中分享您的经验!

翻译自: https://www.hanselman.com/blog/exploring-servicestacks-simple-and-fast-web-services-on-net-core

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值