0

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

1 回答 1

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();
}
于 2013-10-24T13:02:33.890 回答