2

Bertrand 创建了一篇博客文章来指定如何在 WCF Modules for Orchard 中使用 IoC。

在 1.1 中,您可以使用新的 Orchard 主机工厂创建 SVC 文件:

<%@ ServiceHost Language="C#" Debug="true" 
    Service="MyModule.IMyService, MyAssembly"
    Factory="Orchard.Wcf.OrchardServiceHostFactory, Orchard.Framework" %>
Then register your service normally as an IDependency but with service and operation contract attributes:

using System.ServiceModel;

namespace MyModule {
    [ServiceContract]
    public interface IMyService : IDependency {
        [OperationContract]
        string GetUserEmail(string username);
    }
}

我的问题是 Orchard 的所有模块都是真正的区域模块。那么如何构建一个路由来访问在区域/模块中创建的 svc 文件呢?

您是否应该使用完整的物理路径来访问 svc 文件(尝试过并导致 web.config 问题,因为它正在桥接站点和区域)。

http://localhost/modules/WebServices/MyService.svc

或者您是否使用 WebServiceHostFactory/OrchardServiceHostFactory 创建 ServiceRoute?

new ServiceRoute("WebServices/MyService", new OrchardServiceHostFactory(), typeof(MyService))

无论我尝试什么,在尝试访问资源时都会得到 404。我能够使用 wcf 应用程序项目并将 WCF 设置为独立应用程序来完成这项工作,我的问题是在尝试将其引入 Orchard/MVC 时开始的。

更新

感谢彼得的帮助,

这是我实施该服务的步骤。

路由.cs

new RouteDescriptor {   Priority = 20,
                        Route = new ServiceRoute(
                                      "Services",
                                      new WebServiceHostFactory(),
                                      typeof(MyService)) }

如果我使用 OrchardServiceHostFactory() 而不是 WebServiceHostFactory() 我会收到以下错误。

Operation is not valid due to the current state of the object.

果园根 Web.Config

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

我的服务

[ServiceContract]
public interface IMyService : IDependency
{
    [OperationContract]
    string GetTest();
}

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
class MyService : IMyService
{
    public string GetTest()
    {
        return "test";
    }
}

我无法通过修改模块的 web.config 来使服务正常工作。我收到以下错误

ASP.NET routing integration feature requires ASP.NET compatibility.

更新 2

果园根 Web.Config

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <!-- ... -->
  </system.serviceModel>

路由.cs

public IEnumerable<RouteDescriptor> GetRoutes() {
    return new[] {
                     new RouteDescriptor {   Priority = 20,
                                             Route = new ServiceRoute(
                                                 "Services",
                                                 new OrchardServiceHostFactory(),
                                                 typeof(IMyService))

                     }
                 };
}

这行得通,这里的关键是您必须在引用 IDependency 的对象上调用 typeof,WorkContextModule.IsClosingTypeOf 无法处理消耗依赖关系的对象,它必须采用直接调用它的接口。

4

1 回答 1

2

正如您所说,Orchard 模块是 ASP.NET MVC 术语中的区域,因此您提供的 URL 不正确,应该是:

http://localhost/Your.Orchard.Module/WebServices/MyService.svc

其中localhost是运行应用程序的虚拟目录,/WebServices是模块根目录中的文件夹。

您还可以毫无问题地以编程方式创建服务路由。本文讲述如何在 Orchard 中添加新路线。您可以将ServiceRoute分配给 RouteDescriptor的Route属性,而不是默认的 MVC 路由(如文档中所示)。 关于在启用区域的 ASP.NET MVC 应用程序中添加 ServiceRoute的问题之前被问过,检查一下它可能会对您有所帮助。

顺便说一句 - 你也可以检查这个关于前缀服务路由的 SO question。

高温高压

于 2011-04-26T20:13:11.267 回答