2010-01-13 34 views
4

我試圖允許用戶配置WCF服務,包括服務偵聽的IP和端口號。用戶有一個單獨的配置應用程序,允許設置這些值,但我遇到的問題是,app.config必須定義一個端點以創建一個新的ServiceHost條目......但我的端點正在在單獨的配置文件中定義,然後必須在運行時以編程方式進行綁定。如何以編程方式爲WCF服務設置單個端點

如果我這樣做(基於How to programatically modify WCF app.config endpoint address setting?

 m_SvcHost = new ServiceHost(this); 

     if (Config.ServiceEndpoint != null && Config.ServiceEndpoint != String.Empty) 
     { 
      m_SvcHost.AddServiceEndpoint(typeof(IMyService), 
       new BasicHttpBinding(), 
       Config.ServiceEndpoint); 
     } 

     m_SvcHost.Open(); 

該服務將聽取雙方在App.config中定義的URI和配置文件中定義的URI沒有辦法說。我可以找到刪除原始端點或在沒有定義端點的情況下創建服務

從配置應用程序寫入app.config不是一個選項 - 我需要從單獨的XML配置中以編程方式提取配置的值檔案....

有什麼想法?

編輯:運行服務作爲Windows服務,並公開HTTP端點,它不運行在IIS託管的Web服務 - 如果,在所有

回答

1

通過結合和的gWiz迪倫的答案,我想出了一個辦法做到這一點,雖然我沒有測試不夠徹底知道我是否已經打破這些變化的任何其他功能。

基本上,我加入這個類:

public class MobileMonitoringSvcHost : ServiceHost 
{ 
    protected override void ApplyConfiguration() 
    { 
     // skip this line to not apply default config - unsure of other ramifications of doing this yet... 
     base.ApplyConfiguration(); 

     base.Description.Endpoints.Clear(); 
    } 

    public MobileMonitoringSvcHost(object singletonInstance, params Uri[] baseAddresses) : base(singletonInstance, baseAddresses) 
    { 

    } 
} 

這跳過的ServiceHost「ApplyConfiguration」通話和(可能是不必要的,因爲現在如果配置不加載應該是沒有終點)清除端點。然後,我做到以下幾點:

m_SvcHost = new MySvcHost(this); 


     if (Config.ServiceEndpoint != null && Config.ServiceEndpoint != String.Empty) 
     { 
      //m_SvcHost.Description.Endpoints.Clear(); 


      m_SvcHost.AddServiceEndpoint(typeof(IMobileMonitoringSvc), 
       new BasicHttpBinding(), 
       Config.ServiceEndpoint); 
     } 


     // open the svchost and allow incoming connections 
     m_SvcHost.Open(); 

這並導致服務僅在外部配置端點聽,而不是在app.config配置的端點

謝謝!

0

變化的東西你不必都具有的配置我不相信 - 即你可以用代碼完成所有工作。如果你將這些東西放在.config中,那麼它將與你在代碼中編寫的東西一起使用。

如果你想要一個或另一個,我認爲你必須刪除一個或另一個。

+0

我的問題是我希望服務只響應通過配置應用程序配置的IP /端口,而不是app.config中列出的URI ......但我無法刪除應用程序中的端點配置。配置,否則我不能實例化一個新的ServiceHost條目(因爲當時沒有配置端點) – JustinD 2010-01-13 20:12:19

2

賈斯汀,

這對你有幫助嗎?此代碼將允許您響應您在CreateServiceHost()方法中列出的任何地址。

public class CRSyncServiceHost : ServiceHost 
{ 
    public CRSyncServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses) { } 

    protected override void ApplyConfiguration() 
    { 
     base.ApplyConfiguration(); 
    } 
} 

public class CRSyncServiceFactory : ServiceHostFactory 
{ 
    protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) 
    { 
     Uri newServiceAddress = new Uri("http://someaddress.com/CRSyncService.svc"); 
     CRSyncServiceHost newHost = new CRSyncServiceHost(serviceType, newServiceAddress); 
     return newHost; 
    } 
} 

<%@ ServiceHost Language="C#" Debug="true" Service="CRSyncService" Factory="CRSyncServiceFactory" CodeBehind="CRSyncService.svc.cs" %> 
+0

我最初沒有提及該服務是一個Windows服務,並沒有在IIS中託管 - 我試過這只是爲了發現CreateServiceHost不適用於Windows服務 – JustinD 2010-01-13 20:27:51

2

那麼我沒有巨大的WCF背景,但會工作嗎?

m_SvcHost = new ServiceHost(this); 
m_SvcHost.Description.Endpoints.Clear(); // <-- added 

if (Config.ServiceEndpoint != null && Config.ServiceEndpoint != String.Empty) 
{ 
    m_SvcHost.AddServiceEndpoint(typeof(IMyService), 
     new BasicHttpBinding(), 
     Config.ServiceEndpoint); 
} 

m_SvcHost.Open(); 
+0

它確實清除了端點,然後僅添加了我想要的端點,但即使端點集合中僅列出了外部配置的端點,服務仍然在兩個端口上作出響應。 – JustinD 2010-01-13 20:33:24

相關問題