【问题标题】:Why wcf service is not discovered when it's created in "Windows Class Library" project?为什么在“Windows 类库”项目中创建 wcf 服务时没有发现它?
【发布时间】:2012-07-11 02:01:15
【问题描述】:

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

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

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

已编辑

Discover 仅在服务位于 Wcf 服务库 中时才有效。但是,一旦我转到其他项目(ConsoleClass Library),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>

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

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

【问题讨论】:

  • 不确定您所说的发现是什么意思。服务库是一个添加了一些管道的类库。知道我从类库开始构建了一个 WCF 服务,它可以工作。三个项目。服务(类)、服务主机(控制台)和服务客户​​端(控制台)。客户端需要主机运行。您确定服务正在运行吗?
  • 服务正在运行。我通过从 bin/Debug 目录运行服务,然后尝试添加服务引用来运行服务。
  • 还是坏了?请贴出启动主机的代码。
  • 这会对你有所帮助-tilr.blogspot.com/2014/06/…

标签: c# .net wcf


【解决方案1】:

在项目属性中开启Wcf optionsstart host

【讨论】:

  • 它没有这样的项目属性。它是常规的 Windows 类库
  • 重新创建为 WcfClassLibrary 或启动主机手册,例如在控制台应用程序中
  • 但是为什么会这样呢? Wcf options start host 与我的问题有什么关系?即使我在“Wcf 服务库”项目中取消选中Wcf options start host,它仍然被发现。
  • 那么,为什么 Discover 仅在使用 Wcf 服务库 项目创建的 wcf 服务时才有效?
  • 你的项目没有开始或者你删除了 mex,检查一下
【解决方案2】:

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

如果您创建 类库 项目,该项目将包含您的服务文件,因此如果服务未托管(未运行),您将无法使用 Add Service Reference-&gt;Discover 创建客户端。您应该运行主机,只有在它把服务地址放在 Address 栏中并按 Go

之后

【讨论】:

  • 见 cmets。 “你确定服务正在运行吗?” “是的,服务正在运行。”
  • @Blam,我说的不是通过在地址栏中写入来添加服务引用,而是当我单击Discover 按钮时
  • 是的,发现按钮,所以?关键是服务需要运行。第一条评论是您回答是的正在运行的服务。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 2019-05-02
      • 2011-01-26
      • 1970-01-01
      • 2015-03-18
      • 2019-02-24
      • 1970-01-01
      相关资源
      最近更新 更多