Bertrand 创建了一个博客post http://weblogs.asp.net/bleroy/archive/2011/04/20/orchard-1-1-what-s-in-it-for-developers.aspx指定如何在 Orchard 的 WCF 模块中使用 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 时,我的问题就开始了。
UPDATE
感谢 Piotr 的帮助,
这是我实施该服务所采取的步骤。
路线.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.
Orchard 根 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.
UPDATE 2
Orchard 根 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 无法处理消耗依赖项的对象,它必须采用直接调用它的 Interface。