2009-07-07 39 views
3

如何在app.config文件中用MEX端點定義端點,然後我需要運行我的應用程序。我有一個名爲IXMLService的服務合約,我正在使用WsHttpBinding。 請給我一個例子。 創建app.config後,我該如何啓動服務?如何在app.config文件中定義端點?

回答

6
<system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior name="MetadataBehavior"> 
       <serviceMetadata httpGetEnabled="true" /> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
     <service name="YourNamespace.XMLService" behaviorConfiguration="MetadataBehavior"> 

      <!-- Use the host element only if your service is self-hosted (not using IIS) --> 
      <host> 
       <baseAddresses> 
        <add baseAddress="http://localhost:8000/service"/> 
       </baseAddresses> 
      </host> 

      <endpoint address="" 
         binding="wsHttpBinding" 
         contract="YourNamespace.IXMLService"/> 

      <endpoint address="mex" 
         binding="mexHttpBinding" 
         contract="IMetadataExchange"/> 
     </service> 
    </services> 
</system.serviceModel> 

UPDATE:要開始,你可以寫下面的控制檯應用程序來承載它(通過將以前的app.config)服務:

class Program 
{ 
    static void Main(string[] args) 
    { 
     using (var host = new System.ServiceModel.ServiceHost(typeof(XMLService))) 
     { 
      host.Open(); 
      Console.WriteLine("Service started. Press Enter to stop"); 
      Console.ReadLine(); 
     } 
    } 
} 
+0

這可能贏不起作用 - MEX端點沒有完整的完整地址,並且沒有定義「baseAddresses」...... – 2009-07-07 06:01:39

2

Darin的答案是幾乎有 - 您需要爲您的服務和mex端點指定全部完整地址,或者您需要添加基址:

<system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior name="MetadataBehavior"> 
       <serviceMetadata httpGetEnabled="true" /> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
     <service name="XMLService" behaviorConfiguration="MetadataBehavior"> 
      <host> 
      <baseAddresses> 
       <add baseAddress="http://localhost:8888/"/> 
      </baseAddresses> 
      </host> 
      <endpoint address="MyService" 
         binding="wsHttpBinding" 
         contract="IXMLService"/> 

      <endpoint address="mex" 
         binding="mexHttpBinding" 
         contract="IMetadataExchange"/> 
     </service> 
    </services> 
</system.serviceModel> 

您的服務將是對http://localhost:8888/MyService,並在http://localhost:8888/mex

您的MEX數據。如果你願意,你也可以在終端直接指定完整地址:

 <service name="XMLService" behaviorConfiguration="MetadataBehavior"> 
      <endpoint address="http://localhost:8888/MyService" 
         binding="wsHttpBinding" 
         contract="IXMLService"/> 

      <endpoint address="http://localhost:8888/MyService/mex" 
         binding="mexHttpBinding" 
         contract="IMetadataExchange"/> 
     </service> 

馬克

相關問題