2013-10-23 112 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(); 
}