WCF app.config服务名称在Project之外

时间:2013-07-26 19:12:24

标签: c# asp.net wcf app-config

我在尝试创建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”下,该名称也是项目名称。

提前致谢!

2 个答案:

答案 0 :(得分:0)

尝试在serviceBehaviors标记下的行为中为您的服务行为命名。 并在service tag中的behaviourConfiguration中指定相同的名称。

答案 1 :(得分:0)

我终于弄明白了我的问题。我将'Namespace'添加到我的ServiceContract中,一切似乎都运行良好!

示例:

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