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

时间:2012-07-11 12:40:04

标签: c# .net wcf

我将 Wcf服务库项目添加到解决方案中,并创建了2个类(Service1IService1)和配置文件。现在,我想在同一解决方案中将该服务添加到我的控制台项目中。我点击"添加服务参考 - >发现"它找到了服务。

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

创建 Wcf服务库项目或 Windows类库项目并在 Wcf服务库中创建相同内容时有何不同?

被修改

仅当服务位于 Wcf服务库时,Discover才有效。但是,一旦我转移到其他项目(控制台类库),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;
        }
    }
}

4 个答案:

答案 0 :(得分:6)

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

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

之后

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

在项目属性Wcf options start host

中启用

答案 3 :(得分:0)

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