WCF 启动:无法添加服务

本文关键字:添加 服务 启动 WCF | 更新日期: 2023-09-27 18:36:44

我的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;
        }
    }
}

我的应用程序配置是:

<?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 GUI 中正确启动,您可以在其中看到启动的所有服务......

但是当我尝试访问 : 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;
        }
    }
}
}

WCF 启动:无法添加服务

我发现了我的错误:

方法:

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();
}