如何为驻留在一个类中的两个服务分别生成 WCF 代理代码

本文关键字:代码 服务 两个 代理 WCF 一个 | 更新日期: 2023-09-27 17:56:29

基于这个答案,我创建了以下实现两个WCF服务的类:

public class WcfEntryPoint : IMyService1, IMyService2
{
    #region IMyService1
    #endregion
    #region IMyService2
    #endregion
}

它托管在 Windows Service 中。App.config 看起来像:

<system.serviceModel>
<services>
  <service name="TestWcfProject.WcfEntryPoint" behaviorConfiguration="WcfEntryPointBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8000/TestWcfProject/service"/>
      </baseAddresses>
    </host>
    <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/TestWcfProject/service  -->
    <endpoint address="Service1" binding="wsHttpBinding" contract="TestWcfProject.IMyService1" />
    <endpoint address="Service2" binding="wsHttpBinding" contract="TestWcfProject.IMyService2" />
    <!-- the mex endpoint is exposed at http://localhost:8000/TestWcfProject/service/mex -->
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="WcfEntryPointBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="False"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

为了从某些客户端应用程序使用这些服务,我通过以下命令生成代理代码:

SvcUtil.exe http://localhost:8000/TestWcfProject/service?wsdl

问题是生成的 WcfEntryPoint.cs 包含 IMyService1 和 IMyService2 的代理代码。

如何仅为IMyService1IMyService2生成代理代码,而不同时生成代理代码?我尝试启动

SvcUtil.exe /excludeType:TestWcfProject.IMyService2 http://localhost:8000/TestWcfProject/service?wsdl

没有运气。

我知道我需要的可以通过在两个不同的类中实现IMyService1IMyService2来实现。但对于项目来说,这两个服务必须由同一个类实现。

如何为驻留在一个类中的两个服务分别生成 WCF 代理代码

如何仅为 IMyService1 或 IMyService2 生成代理代码,但 不是为了两个人在一起吗?

它不是svcutil支持的东西。但是,您可以更改服务发布其元数据的方式。

在服务上,您可以:

  1. 隐藏服务操作,防止 MEX 终结点公开。
  2. 添加路径为"Mex2"的第二个自定义 Mex 终结点。

在这两个引用之间,您应该能够为服务创建两个自定义 mex 终结点。每个终端节点针对特定用例发布方法的子集。