2012-07-13 21 views
3

「我怎樣才能使用的發動機在我的控制檯應用程序」如何使用引擎對象在我的控制檯應用程序

我不應該使用的ITemplate接口和變換的方法。

我正在使用Tridion 2011

任何人都可以請建議我。

+3

你能解釋一下你的用例和你在做什麼?一個友好的建議,你提供的信息越多,你就會收到更好的答案。對於你的用例,可能是引擎不是正確的設計方法,我不知道,除非你解釋你正在努力實現什麼,你已經做了什麼。 – 2012-07-13 15:43:46

回答

8

你不能。該Engine類是TOM.NET的一部分,API明確保留用於:

  1. 模板積木
  2. 事件處理程序

對於所有其他情況(如控制檯應用程序)你應該使用核心服務。

有很多很好的問題(和文章在其他網站)已經:

如果你被沿途停留,向我們展示了相關代碼+你擁有的配置和你得到的錯誤信息(或者你陷入了哪個步驟),我們會盡力幫助。

6

從控制檯應用程序,您應該使用核心服務。我使用核心服務編寫了一個小例子來搜索內容管理器中的項目。

Console.WriteLine("FullTextQuery:"); 

var fullTextQuery = Console.ReadLine(); 

if (String.IsNullOrWhiteSpace(fullTextQuery) || fullTextQuery.Equals(":q", StringComparison.OrdinalIgnoreCase)) 
{ 
    break; 
} 

Console.WriteLine("SearchIn IdRef:"); 

var searchInIdRef = Console.ReadLine(); 

var queryData = new SearchQueryData 
        { 
         FullTextQuery = fullTextQuery, 
         SearchIn = new LinkToIdentifiableObjectData 
             { 
              IdRef = searchInIdRef 
             } 
        }; 

var results = coreServiceClient.GetSearchResults(queryData); 
results.ToList().ForEach(result => Console.WriteLine("{0} ({1})", result.Title, result.Id)); 

將對Tridion.ContentManager.CoreService.Client的引用添加到Visual Studio項目中。

的核心代碼服務客戶端提供商:

public interface ICoreServiceProvider 
{ 
    CoreServiceClient GetCoreServiceClient(); 
} 

public class CoreServiceDefaultProvider : ICoreServiceProvider 
{ 
    private CoreServiceClient _client; 

    public CoreServiceClient GetCoreServiceClient() 
    { 
     return _client ?? (_client = new CoreServiceClient()); 
    } 
} 

而且客戶端本身:

public class CoreServiceClient : IDisposable 
{ 
    public SessionAwareCoreServiceClient ProxyClient; 

    private const string DefaultEndpointName = "netTcp_2011"; 

    public CoreServiceClient(string endPointName) 
    { 
     if(string.IsNullOrWhiteSpace(endPointName)) 
     { 
      throw new ArgumentNullException("endPointName", "EndPointName is not specified."); 
     } 

     ProxyClient = new SessionAwareCoreServiceClient(endPointName); 
    } 

    public CoreServiceClient() : this(DefaultEndpointName) { } 

    public string GetApiVersionNumber() 
    { 
     return ProxyClient.GetApiVersion(); 
    } 

    public IdentifiableObjectData[] GetSearchResults(SearchQueryData filter) 
    { 
     return ProxyClient.GetSearchResults(filter); 
    } 

    public IdentifiableObjectData Read(string id) 
    { 
     return ProxyClient.Read(id, new ReadOptions()); 
    } 

    public ApplicationData ReadApplicationData(string subjectId, string applicationId) 
    { 
     return ProxyClient.ReadApplicationData(subjectId, applicationId); 
    } 

    public void Dispose() 
    { 
     if (ProxyClient.State == CommunicationState.Faulted) 
     { 
      ProxyClient.Abort(); 
     } 
     else 
     { 
      ProxyClient.Close(); 
     } 
    } 
} 

當你想通過核心服務執行CRUD操作就可以實現在下面的方法客戶端:

public IdentifiableObjectData CreateItem(IdentifiableObjectData data) 
{ 
    data = ProxyClient.Create(data, new ReadOptions()); 
    return data; 
} 

public IdentifiableObjectData UpdateItem(IdentifiableObjectData data) 
{ 
    data = ProxyClient.Update(data, new ReadOptions()); 
    return data; 
} 

public IdentifiableObjectData ReadItem(string id) 
{ 
    return ProxyClient.Read(id, new ReadOptions()); 
} 

要構建一個數據對象例如一個組件可以實現一個實現了創建方法,做一個組件生成器類爲你好:

public ComponentData Create(string folderUri, string title, string content) 
{ 
    var data = new ComponentData() 
        { 
         Id = "tcm:0-0-0", 
         Title = title, 
         Content = content, 
         LocationInfo = new LocationInfo() 
        }; 

    data.LocationInfo.OrganizationalItem = new LinkToOrganizationalItemData 
    { 
     IdRef = folderUri 
    }; 

using (CoreServiceClient client = provider.GetCoreServiceClient()) 
{ 
    data = (ComponentData)client.CreateItem(data); 
} 

    return data; 
} 

希望這可以讓你開始。

+0

+1通過空合併運算符和傳遞賦值進行延遲加載。要偷那個。 – 2012-07-16 13:05:48

相關問題