2016-12-16 18 views
0

調用WCF服務我正在開發一個SharePoint外掛程序,它具有SharePoint託管部分和提供程序託管部分。在提供商託管的部分中,我有幾個安裝了分類和搜索等幾項服務的服務。我爲此使用C#CSOM。這是提供者託管部分的唯一目的。當插件安裝時,會調用遠程事件接收器的AppInstalled事件觸發器。這個遠程事件接收器應該一個接一個地調用我的WCF服務。使用參數

現在我實際的問題:我目前使用這種方法來消耗我的服務:

var taxBinding = new BasicHttpBinding(); 
var taxEndpoint = new EndpointAddress(remoteUrl.ToString() + "/Services/TaxonomySetupService.svc"); 
var taxChannelFactory = new ChannelFactory<ISetupService>(taxBinding, taxEndpoint); 

ISetupService taxClient = null; 

try 
{ 
    taxClient = taxChannelFactory.CreateChannel(); 
    taxClient.SetAppWebUrl(appWebUrl.ToString()); 
    if (!taxClient.IsInstalled()) 
     taxClient.Install(); 
    string logs = taxClient.GetLogs(); 

    ((ICommunicationObject)taxClient).Close(); 
} 
catch (Exception ex) 
{ 
    if (taxClient != null) 
    { 
     ((ICommunicationObject)taxClient).Abort(); 
    } 
} 

ISetupService:

[ServiceContract] 
public interface ISetupService 
{ 
    string OpenText { get; } 
    string DoneText { get; } 
    string AppWebUrl { get; set; } 

    [OperationContract] 
    bool IsInstalled(); 

    [OperationContract] 
    void SetLogComponent(LogList logList); 

    [OperationContract] 
    void SetAppWebUrl(string url); 

    [OperationContract] 
    void WriteToLog(string message); 

    [OperationContract] 
    string GetLogs(); 

    [OperationContract] 
    void Install();   
} 

我的解決方案沒有,雖然,所以我採用這種做法尋找更好的東西。具體來說,我需要將一個ClientContext對象傳遞給我的ISetupService構造函數。這裏最簡單的方法是什麼?

+0

你能更具體地解釋你想要解決什麼嗎? – jtabuloc

+0

我正在尋找類似於http://stackoverflow.com/questions/21623432/how-to-pass-multiple-parameter-in-wcf-restful-service的東西,只是我想將我的參數傳遞給我的構造函數,不是一種方法。 – LeonidasFett

回答

0

選項1-懶惰可注入屬性 爲什麼在構造函數中?爲什麼不有一個Lazy Injectable屬性?

internal IClientContext Context 
{ 
    get { return _Context ?? (_Context = SomeStaticHelper.Context); } 
    set { _Context = value; } // Allows for replacing IContext for unit tests 
} private IClientContext _Context; 

public class SomeStaticHelper 
{ 
    public static IContext Context { get; set; } // Set this in global.asax 
} 
  • 臨:沒有額外的庫
  • 臨:您可以在單元測試中替換IContext容易(使用InternalsVisibleTo)
  • 缺點:類耦合到SomeStaticHelper的編譯。
  • Con:爲一個班級做這個很好,但是爲100個班級做這個不太好。

選擇2 - 依賴注入 或者你可以使用直線上升依賴注入,如Autofac。 http://docs.autofac.org/en/latest/getting-started/

  • 臨:類是解耦的,並且依賴被注入。
  • Pro:如果你有很多需要依賴注入的類,這是要走的路,因爲開銷現在是一些類文件,而不是每個類文件中的屬性。答:你必須爲你的代碼添加一個框架。答:你現在需要更多的代碼和其他對象來配置依賴注入。

對於幾乎不需要依賴注入的小項目使用選項1。我認爲這是最簡單的方法。

對於使用DI的大型項目,始終使用選項2。

+0

我會在週一回去工作並提供反饋時嘗試此操作。擡起頭,回到一個破碎的男人的家中。 :) – LeonidasFett