请注意,本站并不支持低于IE8的浏览器,为了获得最佳效果,请下载最新的浏览器,推荐下载 Chrome浏览器
欢迎光临。交流群:166852192

OrchardProject + WebApi = interesting


Update: Code for the module is available on GitHub.
Orchard is really cool for CMS solutions, however, I am learning more about dependency injection, inversion of control, etc, and what I see in orchard is very interesting.
Asp.net mvc does well to de-couple pieces of the application when it comes to handling routes, but something is still missing. With asp.net mvc, you have to deploy these pieces along with the application, and perhaps discover them with mef, autofac, etc, but what about updates? If you wanted to update your modules, you have to FTP them to the application root, and re-write the dlls.
Is there an easier way to deploy updates, what about an application that could update itself????????
Orchard对CMS解决方案非常酷,但是,我正在学习更多关于依赖注入,控制反转等等,我在果园里看到的是非常有趣。

Asp.net mvc在处理路由时很好地分离应用程序的部分,但是仍然有一些缺失。 有了asp.net mvc,你必须与应用程序一起部署这些部分,并可能发现它们与mef,autofac等,但更新? 如果你想更新模块,你必须将它们FTP到应用程序根目录,然后重新编写dll。
有一个更简单的方式来部署更新,什么应用程序可以更新自己???

Nuget to the rescue

Phil Hack (@Haacked) wrote something on his blog about building a self-updating site using nuget (link.) This is really cool because it allows you to package up an application update, and the application can update itself.
However… the scenario I am going for is really about multiple modules.

Orchard CMS

Orchard uses Nuget, and is fully able to handle modules. From inside of the orchard application’s dashboard, I can install modules, as well as updates to those modules. The modules can also state dependencies, which are installed automatically.

Getting Started

To get started, first download an installation of Orchard CMS.  I used the core recipe, as this is going to be more of a rest endpoint:
Nuget的救援
Phil Hack(@Haacked)在他的博客上写了一个关于使用nuget(链接)构建自我更新网站的内容。这真的很酷,因为它允许你打包一个应用程序更新,并且应用程序可以更新自己。
但是...我要做的是真正关于多个模块的场景。
果园CMS
Orchard使用Nuget,完全能够处理模块。 从orchard应用程序的仪表板的内部,我可以安装模块,以及这些模块的更新。 模块还可以指定自动安装的依赖关系。
入门
要开始,首先下载Orchard CMS的安装。 我使用核心配方,因为这将是一个更多的休息终点:

Then you will want to enable the code-generation module.  Now you can create a module like using the orchard.exe command that should be in the bin folder of your application.  I created a module called myRestfulModule:
然后,您将要启用代码生成模块。 现在,您可以创建一个类似于使用orchard.exe命令的模块,该命令应该位于应用程序的bin文件夹中。 我创建了一个名为myRestfulModule的模块:

Orchard just generated a module for us, and placed it in the /Modules directory inside the Orchard web application’s folder structure.  This means that the module is already installed, just not yet activated.
I looked in visual studio, and my new module was not included automatically in the solution, so I added it.  Here is my module: as you can see, it already has references to orchard dlls.
Orchard刚刚为我们生成了一个模块,并将其放在Orchard Web应用程序的文件夹结构中的/ Modules目录中。 这意味着模块已经安装,但尚未激活。
我在visual studio中看过,我的新模块没有自动包含在解决方案中,所以我添加它。 这里是我的模块:如你所见,它已经引用了orchard dll。

I tweaked the module.txt file to look something like this:
我调整module.txt文件看起来像这样:
?
01
02
03
04
05
06
07
08
09
10
Name: myRestfulModule
AntiForgery: enabled
Author: Jeremiah Redekop
Version: 0.5.0
OrchardVersion: 0.5.0
Description: A sample restful endpoint using wcfwebapi
Features:
    myRestfulModule:
        Description: Hello world using rest from inside orchard.
Which then gives Orchard the ability to render the module in the admin module library like this:
然后让Orchard能够在管理模块库中渲染模块,如下所示:

We can just go ahead and enable the module. Now let’s get the module doing something interesting. Go ahead and use nuget to install WebApi.All, which is good to start. (By the way, I started out with this tutorial on the orchard site, but had to tweak it to work with webapi.)
我们可以直接启动模块。 现在让我们让模块做一些有趣的事情。 继续使用nuget安装WebApi.All,这是很好的开始。 (顺便说一句,我开始在orchard网站上使用本教程,但是必须调整它以使用webapi。)

The details

We need to make a few tweaks:
  • Orchard ROOT: Modify Web.config on Orchard application root – allow asp.net compatibility
  • MODULE: Create Rest Endpoint – same as before with one tweak
  • MODULE: Create Route Provider – helps orchard with routing requests to our module
细节
  • 我们需要做一些调整:
  • Orchard ROOT:修改Orchard应用程序root上的Web.config - 允许asp.net兼容性
  • MODULE:创建Rest Endpoint - 与之前一样调整一次
  • MODULE:创建路由提供程序 - 帮助orchard路由请求到我们的模块
Orchard Application Web.config:
?
1
2
3
<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
</serviceHostingEnvironment>
</system.serviceModel>
This is the Rest endpoint. I am using the Fabricator library just to mock up a whole bunch of people. I need this class to implement IDependency for Orchard to create it properly
这是Rest端点。 我使用Fabricator库只是为了模拟一大群人。 我需要这个类来实现IDependency Orchard来正确创建它
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
[ServiceContract]
    public class PeopleService
        : IDependency // required for orchard
    {
        private static readonly List _people = Fabrication.Fabricator.Generate(500).ToList();
 
        [WebGet]
        public IQueryable GetPeople()
        {
 
            return _people.AsQueryable();
            //return new EnumerableQuery(new Person[]{});
        }
 
        [WebGet(UriTemplate = "{name}")]
        public Person GetPerson(string name) {
            return _people.Single(p=> p.Name == name);
        }
    }
Here is the route provider that uses the orchard convention of being at the root of your module. Orchard will use this to route requests to the rest endpoint. If you look at the extenson method, the goodies are there.
这里是路由提供程序,它使用位于模块根目录的orchard约定。 Orchard将使用它将请求路由到其余端点。 如果你看一下extenson方法,好处就在那里。
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Routes : IRouteProvider {
    private static readonly ServiceRoute _route = MapServiceRoute(typeof (PeopleService),
"myRestfulModule/People");
    public IEnumerable GetRoutes() {
        return new[] {
            new RouteDescriptor {
                Priority = 1,
                Route = _route
            }
        };
    }
 
    public void GetRoutes(ICollection routes) {
        foreach (var routeDescriptor in GetRoutes())
            routes.Add(routeDescriptor);
    }
     private static ServiceRoute MapServiceRoute(Type serviceType, string routePrefix,
HttpConfiguration configuration = null, object constraints = null, bool useMethodPrefixForHttpMethod = true)
    {
        if (configuration == null)
            configuration = new WebApiConfiguration(useMethodPrefixForHttpMethod);
 
        var serviceHostFactory = new HttpServiceHostFactory { Configuration = configuration };
        var webApiRoute = new WebApiRoute(routePrefix, serviceHostFactory, serviceType) {
Constraints = new RouteValueDictionary(constraints) };
        return webApiRoute;
    }
}
Now by navigating to the url, you can see the end result.
现在通过导航到网址,您可以看到最终结果。

Conclusion

This is a prototype of using an orchard module to expose rest endpoints.  The main benefit, I believe, is the ability to expose and update endpoints independant of any application code.  Once you deploy your orchard application, you don’t have to redeploy.  All you have to do is publish updates to your modules, and then install them from the application’s admin panel.
There are downsides, such as having a dependency on Orchard, but the current release is production-ready.
结论
这是使用一个orchard模块来暴露休止端点的原型。 我相信的主要好处是能够暴露和更新独立于任何应用程序代码的端点。 部署您的orchard应用程序后,您不必重新部署。 所有您需要做的是发布对模块的更新,然后从应用程序的管理面板安装它们。
有一些缺点,例如依赖于Orchard,但是当前的版本是生产就绪的。



作者原创内容不容易,如果觉得内容不错,请点击右侧“打赏”,赏俩给作者花花,也算是对作者付出的肯定,也可以鼓励作者原创更多更好内容。
更多详情欢迎到QQ群 166852192 交流。