2

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

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

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

已编辑

仅当Discover服务在Wcf Service Library中时才有效。但是一旦我转移到其他项目(ConsoleCl​​ass Library),Discover就再也找不到它了。为什么?

应用程序配置

<?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>

服务1.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; }
        }
    }
}

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

4 回答 4

6

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

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

于 2012-07-15T09:18:02.113 回答
1

看起来 WCF 选项仅在您使用 wcf 项目模板创建项目时显示,如果您有一个现有项目(类库),您可以通过在第一个属性组中添加它来转换它更多讨论在这里

在第一部分中,添加以下行:{3D9AD99F-2412-4246-B90B-4EAA41C64699};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}

于 2015-12-17T16:31:16.437 回答
0

在项目属性中打开Wcf options start host

于 2012-07-11T12:43:09.230 回答
0

我有同样的问题。转到调试-> 不调试就开始。这应该启动主机和客户端应用程序。然后转到客户端并添加服务参考。换句话说,服务必须正在运行

于 2018-08-30T21:46:42.367 回答