为什么在"Windows类库"项目中创建wcf服务时未发现它?

the*_*ist 2 .net c# wcf

我将Wcf服务库项目添加到解决方案中,它创建了2个类(Service1,IService1)和配置文件.现在我想在同一个解决方案中将该服务添加到我的Console项目中.我单击"添加服务引用 - >发现",它会找到该服务.

当我创建Windows类库项目并在那里创建与刚刚创建的Wcf服务库项目相同的示例,然后尝试将其添加为我的控制台项目的引用,因此单击Discover不会返回任何内容.为什么?

当我创建Wcf服务库项目或Windows类库项目并在那里创建与Wcf服务库相同时,有什么区别

编辑

Discover只有当服务是作品WCF服务库.但是一旦我转移到其他项目(控制台,类库),Discover它就不会再找到它了.为什么?

的app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary1.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>
  </system.serviceModel>

</configuration>
Run Code Online (Sandbox Code Playgroud)

Service1.cs

namespace WcfServiceLibrary1
{
    // 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; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

IService.cs

namespace WcfServiceLibrary1
{
    // 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

the*_*ist 6

经过一些检查后,我得出的结论是,只有创建WCF服务库,才能使用Add Service Reference->Discover按钮创建客户端而不显式运行主机,它将找到该服务.

如果您创建将具有服务文件的类库项目,那么Add Service Reference->Discover如果未托管服务(未运行),则无法创建客户端.您应该只在将服务地址放在地址栏中并按Go后运行主机