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 无法处理消耗依赖关系的对象,它必须采用直接调用它的接口。