WCF启动:无法添加服务

时间:2013-10-23 13:31:41

标签: c# wcf service

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

1 个答案:

答案 0 :(得分: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();
}