2013-10-03 16 views
1

我必須使用COM可見組件來使用Windows服務中託管的WCF服務。Com可見組件中使用WCF服務

我有一個WCF服務託管在Windows服務中,我必須使用COM Visible程序集中的服務,我創建了一個COM +應用程序併爲其添加了服務引用。以下是app.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <netTcpBinding> 
       <binding name="TcpEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00" 
        receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false" 
        transferMode="Buffered" transactionProtocol="OleTransactions" 
        hostNameComparisonMode="StrongWildcard" listenBacklog="10" 
        maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" 
        maxReceivedMessageSize="65536"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="None"> 
         <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" /> 
         <message clientCredentialType="Windows" /> 
        </security> 
       </binding> 
      </netTcpBinding> 
     </bindings> 
     <client> 
      <endpoint address="net.tcp://serverMachine:9600/DocumentsWcfService/Tcp" 
       binding="netTcpBinding" bindingConfiguration="TcpEndpoint" 
       contract="MysWcfService.IMysWcfService" name="TcpEndpoint" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

當我在傳統的ASP項目中使用這個程序集並調用初始化服務的方法時,我得到如下的錯誤。但是當我在Console應用程序中引用相同的WCF服務時,它工作正常。在COM可見應用程序中是否需要使用特定更改?

找不到在ServiceModel客戶端配置部分中引用合同'MyWcfService.IMysWcfService'的默認端點元素。這可能是因爲沒有爲您的應用程序找到配置文件,或者因爲在客戶端元素中找不到與此合同匹配的端點元素

+1

,如果這是一個正被調用的DLL通過com組件,您將無法依賴應用程序配置,但必須通過代碼進行配置。 – PatFromCanada

+0

@PatFromCanada我將WCF服務引用直接添加到Com組件並使用ASP中的COM組件。如果我們必須通過代碼進行配置,我們如何使用沒有硬編碼地址的端點,請指出任何資源謝謝您。 – Arvind

+0

Adrian的回答是一種可行的方式,當然你總是可以創建一個文本(XML)文件並在其中放置一個地址,我從來不必處理這個特定的問題。 – PatFromCanada

回答

2

COM服務器不讀取正常的配置文件,因此您需要執行此操作通過代碼或者使用下面的技巧:

(你的COM服務器必須是.exe

  1. 轉到DCOMCNFG並創建一個COM +應用
  2. 在COM +應用程序有一個設置「申請根目錄「。輸入您的.exe文件位於
  3. 您的COM對象添加到COM +應用
  4. 創建一個名爲Application.manifest的文件夾中文件的文件夾。該文件必須包含:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" />

  5. 把你的配置文件相同的文件夾。請注意,配置文件中必須有確切的名字Application.config,而不是「myapp.exe.config」

現在的配置文件將工作以同樣的方式在你的控制檯應用程序。

+0

這是一個非常酷的把戲。我希望儘快嘗試一下。 – PatFromCanada

+0

謝謝@adrian,但對於我的應用程序,我們不能將COM服務器作爲exe,因爲我們需要在ASP應用程序中使用該COM對象。 – Arvind

+0

我沒有創建一個.exe,而是在Post build event命令行下的COM項目中添加了「C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ regsvcs/appdir:$(ProjectDir)$(TargetFileName)」,其中ProjectDir是具有Application.config和Application.manifest – Arvind

0

嘗試讀取配置與

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); 
fileMap.ExeConfigFilename = "MysWcfService.dll.config"; 
configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); 

這時可以嘗試讀一些WCF配置部分和編程配置主機(example):

ServicesSection servicesSection = (ServicesSection)configuration.GetSection("system.serviceModel/services"); 
    ServiceEndpointElement endpoint = servicesSection.Services[0].Endpoints[0]; 
    //use endpoint.Address                        
    //use endpoint.Binding 
    //use endpoint.Contract