2013-10-23 110 views
0

我的WCF出現問題。我的服務合同接口:WCF啓動:未能添加服務

namespace A.B.C 
{ 
    [ServiceContract] 
    public interface IWSpExporter 
    { 
     [OperationContract] 
     int ExportFile(); 

     [OperationContract] 
     int ExportMetaData(); 

     [OperationContract] 
     int ExportAll(); 
    } 
} 

當然,我有類:

namespace A.B.C 
{ 
    class WSpExporter : IWSpExporter 
    { 

     public int ExportFile() 
     { 
      return 0; 
     } 

     public int ExportMetaData() 
     { 
      return 0; 
     } 

     public int ExportAll() 
     { 
      return 0; 
     } 
    } 
} 

而我的App.config是:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <services> 

     <service name="A.B.C.WSpExporter" 
       behaviorConfiguration="WSpExporterBehavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8000/WSpExporter/service"/> 
      </baseAddresses> 
     </host> 

     <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/WSpExporter/service--> 
     <endpoint address="" 
        binding="wsHttpBinding" 
        contract="A.B.C.IWSpExporter"> 
      <identity> 
      <servicePrincipalName value="host/localhost"/> 
      </identity> 
     </endpoint> 

     <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/WSpExporter/service/mex --> 
     <endpoint address="mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange" /> 

     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="WSpExporterBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="False"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

然後我編譯和安裝我的服務。我看到它正確啓動在Windows圖形用戶界面,您可以看到所有服務推出...

但是,當我嘗試訪問:http://localhost:8000/WSpExporter/service,我沒有結果。

,當我嘗試我的服務添加到WcfTestClient,我以下錯誤:

無法添加服務。服務元數據可能無法訪問。確保您的服務正在運行並展示元數據。

我不明白的地方是我的問題...

編輯:(所有的服務代碼是不是在這裏...)

namespace A.B.C 
{ 
class PExporter : ServiceBase 
{ 
    public static void Main() 
    { 
     ServiceBase.Run(new PExporter()); 
    } 

    protected override void OnStart(string[] args) 
    { 

     if (serviceHost != null) 
     { 
      serviceHost.Close(); 
     } 

     serviceHost = new ServiceHost(typeof(PExporter)); 

     serviceHost.Open(); 
    } 

    protected override void OnStop() 
    { 
     if (serviceHost != null) 
     { 
      serviceHost.Close(); 
      serviceHost = null; 
     } 
    } 
} 
} 
+0

你的班級'PExporter'似乎不是'公共',或者你沒有粘貼嗎?最終,您需要將調試器附加到正在運行的服務進程以檢查變量和異常,但您也可以添加日誌記錄。 ServiceHost實際上是否啓動?它的端點是什麼? [WCF跟蹤](http://msdn.microsoft.com/zh-cn/library/ms733025.aspx)對你的服務有什麼看法? 'netstat -ab'告訴你什麼,它在聽嗎?你有防火牆阻止'localhost'連接嗎? – CodeCaster

回答

0

我發現我的錯誤:

方法:

protected override void OnStart(string[] args) 
{ 

    if (serviceHost != null) 
    { 
     serviceHost.Close(); 
    } 

    serviceHost = new ServiceHost(typeof(PExporter)); // ERROR HERE 

    serviceHost.Open(); 
} 

發動錯誤的類.. 。更正:

protected override void OnStart(string[] args) 
{ 

    if (serviceHost != null) 
    { 
     serviceHost.Close(); 
    } 

    serviceHost = new ServiceHost(typeof(WSpExporter)); //GOOD NOW 

    serviceHost.Open(); 
}