0

我在尝试创建 WCF 服务时遇到问题。我正在尝试将服务、数据合同和服务合同放在不同的项目中。我不断收到“WCF 服务主机找不到任何服务元数据”。错误。这是我的 appconfig 文件:

<system.serviceModel>
<services>
  <service name="SampleServiceContract.Service1">
    <host>
      <baseAddresses>
        <add baseAddress = "http://localhost:8732/Service1" />
      </baseAddresses>
    </host>
    <!-- Service Endpoints -->
    <!-- Unless fully qualified, address is relative to base address supplied above -->
    <endpoint address ="" binding="wsHttpBinding" contract="SampleServiceContract.IService1">
      <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <!-- Metadata Endpoints -->
    <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
    <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, 
      set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="True"/>
      <!-- To receive exception details in faults for debugging purposes, 
      set the value below to true.  Set to false before deployment 
      to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>

这是我的 IService 文件:

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}

// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

最后这是我的 Service1 文件:

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

app.config 位于命名空间“SampleWCF”下。这也是项目名称。我的 IService1 和 Service1 文件位于名称空间“SampleServiceContract”下,这也是项目名称。

提前致谢!

4

2 回答 2

0

尝试在 serviceBehaviors 标签下的行为中为您的服务行为命名。并在服务标签的行为配置中指定相同的名称。

于 2013-07-26T20:22:38.380 回答
0

我终于弄清楚了我的问题。我在我的 ServiceContract 中添加了“命名空间”,一切似乎都运行良好!

例子:

[ServiceContract(Namespace = "Services.CountermeasureService")]

于 2013-07-30T18:57:14.170 回答