是否可以像托管 wcf 库一样通过标记配置 wcf 客户端



这是我对如何托管 wcf 库项目的理解。创建 wcf 库项目后,您的 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="WcfLib.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfLib/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="wsHttpBinding" contract="WcfLib.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>

只需创建一个的控制台应用程序项目(只需在其中放置一个Console.ReadLine()),并添加对上一个项目程序集的引用,并添加整个services标记,如上所示。现在它托管了。只要该控制台应用程序存在,它就会存在。

请注意如何编写标记以使其工作。<services/>中的所有内容都代表要托管的 WCF 服务实体。在这种情况下,托管是通过我的控制台应用程序通过针叶锥形完成的。

我正在研究是否可以对 wcf 服务消费进行类似的类比。是否可以使用相同的配置模式编写 wcf 客户端?最终,我正在考虑将这些配置放在单独的配置文件中。

以下是以前帖子的链接,如果您对促使我问这个问题的原因感兴趣

您的服务不会自行托管。调试 WCF 库时,WCF 服务主机由调试器运行以承载服务。您必须在真实环境中手动执行此操作。

在此处阅读有关 WCF 服务主机的信息。

编辑:

并回答您的问题:是的,您可以以相同的方式配置客户端。请参阅此文章

我遇到了以下SO帖子。也许我的答案在于此与否...我不确定

如何告诉 WCF 客户端从不同的配置文件中读取设置?

最新更新