【问题标题】:Can a wcf client be configured via markup the same way a wcf library is hosted?是否可以像托管 wcf 库一样通过标记配置 wcf 客户端?
【发布时间】:2012-06-02 14:53:23
【问题描述】:

这是我对如何托管 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>

您可以简单地创建一个 empty 控制台应用程序项目(只需在其中放一个Console.ReadLine()),并添加对先前项目程序集的引用,并添加整个services 标签,如图所示更多。现在它的托管。只要该控制台应用程序存在,它就会存在。

请注意如何编写标记以使其工作的模式。 &lt;services/&gt; 中的所有内容都代表要托管的 wcf 服务实体。在这种情况下,托管是通过我的控制台应用程序通过配置完成的。

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

Here are links of previous posts if you are interested in what made me ask this question

【问题讨论】:

  • 您是否尝试过编写空控制台应用程序,添加引用和实体服务标签?是托管的吗?不知何故,这似乎很神奇:)
  • 它会启动 WCF 服务主机实用程序应用程序...!
  • 奇怪。当我从 Visual Studio 中运行 exe 时,似乎没有托管服务... :(
  • 当然不是。因为它不是那样工作的。
  • 为什么Visual Studio会那样做呢?

标签: .net wcf-client .net-assembly wcf-configuration wcf-library


【解决方案1】:

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

了解 WCF 服务主机here

编辑:

并回答您的问题:是的,您可以以相同的方式配置您的客户端。见this article

【讨论】:

  • +1 纠正我。但是关于 wcf 客户端配置是否可以使用相同的 pattern 有什么想法吗?
  • 这与您在添加服务引用时通过 VS 获得的标记相同。如果您有 2 个或更多服务要连接,那么 serviceModels 部分看起来会很大……我正在考虑对其进行模块化;单独管理ABC?基本上将它们移动到单独的文件中......
  • 那么你的问题到底是什么?
  • 我连接的一个服务可能有它自己的绑定、契约、端点。我只想将这些移动到单独的 *.config 文件中。类似于这里提到的方式->stackoverflow.com/a/10672636/145682
  • 我仍然认为没有问题。你有没有试过这样拆分?
【解决方案2】:

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

How to tell WCF client to read settings out of different config file?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多