2011-04-26 48 views
2

Bertrand創建了一個博客post來指定如何在WCF模塊中爲Orchard使用IoC。Orchard/MVC WCF服務與區域網址

在1.1,您使用新的果園主機廠可以創建一個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); 
    } 
} 

我的問題是,所有的烏節路模塊是真的地區的。那麼你如何建立一個命中區域/模塊中創建的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

感謝您的幫助彼得,

這是我接過來實現服務的步驟。

Routes.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> 

爲MyService

[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> 

Routes.cs

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

        } 
       }; 
} 

這工作,這裏的關鍵是,您必須在引用IDependency的對象上調用typeof ,WorkContextModule.IsClosingTypeOf不能處理消耗依賴性的對象,它必須接受它直接調用的接口。

+0

嘗試更改* RequirementsMode *從* Allowed *改爲* Required *您的服務類。我會試着調查是什麼造成這個問題。 – 2011-04-27 16:38:49

+0

同樣的問題。如果您想查看示例模塊並親自查看錯誤,請在https://bitbucket.org/e2software/orchardwcf創建一個hg存儲庫。 – 2011-04-27 18:37:20

+0

我想這個問題是因爲你的服務實現被標記爲* internal *。嘗試使* public *,它應該工作,而不需要這種類型(IMyService)黑客。內部方法對底層DI引擎是不可見的,它們只是被忽略... – 2011-04-28 02:19:48

回答

2

如您所述,果園模塊是ASP領域。NET MVC條款,所以您提供的網址是不正確的,應該是:

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

本地主機是虛擬目錄下,你的應用程序運行和/WebServices的是在你的模塊的根文件夾。

您還可以以編程方式創建服務路線,而不會出現問題。 This文章講述瞭如何在烏節路添加新路線。您可以將ServiceRoute分配給RouteDescriptor的路由屬性,而不是默認的MVC路由(如文檔中所示)。 The question有關在啓用區域的ASP.NET MVC應用程序中添加ServiceRoute之前曾被詢問,請檢查它,因爲它可能會幫助您。

順便說一句 - 你也可以檢查this SO有關前綴服務路線的問題。

HTH

+0

當我訪問http://localhost/Your.Orchard.Module/WebServices/MyService.svc時,出現錯誤IControllerFactory'Orchard.Mvc。 OrchardControllerFactory「沒有返回名稱爲」Data「的控制器。如果我在路由中使用OrchardServiceHostFactory,則由於對象的當前狀態,我得到操作無效。 WebServiceHostFactory工作正常。 – 2011-04-27 14:07:28