【问题标题】:How to generate Proxy for WCF Service如何为 WCF 服务生成代理
【发布时间】:2013-01-28 13:33:57
【问题描述】:

我这里有一些旧代码,其中有一个 WCF 服务。

服务由客户端通过生成的Proxy.cs调用

我认为它应该很简单,并想生成我自己的 使用 svcutil.exe 代理,但我得到很多不同的错误, 取决于我的尝试(使用运行 exe 进行下载,使用 程序集,使用 webconfig)。例如 svcutil 告诉我 该服务可能不会发布元数据(在我看来 这不是真的)。​​

webconfig 有:

<serviceMetadata httpGetEnabled="true"/>

它有一个多站点绑定

如何生成这样的代理?想要更改合同中的某些内容 然后生成当前版本的代理。

目前我没有服务参考,因为它有一些错误 当像 svcutil 那样添加一个时。

有什么想法吗?

-编辑- 这些服务是通过自托管服务发布的,非常类似于 msdn 中的 WCF 示例中的一个。有端点 多个服务,在 dll 中实现。

-编辑 2- 这是自托管服务的app.config 我已经替换了一些名称,只是为了让它不那么特别。

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="PersonDataModelContainer" connectionString="metadata=res://*/PersonDataModel.csdl|res://*/PersonDataModel.ssdl|res://*/PersonDataModel.msl;provider=System.Data.SqlServerCe.3.5;provider connection string=&quot;Data Source=|DataDirectory|\PersonDatabase.sdf&quot;" providerName="System.Data.EntityClient"/>
  </connectionStrings>
  <system.serviceModel>
    <bindings>
      <netMsmqBinding>
        <binding name="TransactionalNoSecurity" durable="true" exactlyOnce="true" maxReceivedMessageSize="2147483647" maxRetryCycles="1" receiveRetryCount="20" retryCycleDelay="00:05:00">
          <security mode="None"></security>
        </binding>
      </netMsmqBinding>

      <basicHttpBinding>

        <binding name="TransportSecurity" openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </basicHttpBinding>

      <ws2007HttpBinding>

        <binding name="NoSecurity" transactionFlow="true" maxReceivedMessageSize="200000" openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="None"></security>
        </binding>

        <binding name="UNMessageSecurity" transactionFlow="true" openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="Message">
            <message clientCredentialType="UserName" establishSecurityContext="true"/>
          </security>
        </binding>

        <binding name="CertMessageSecurity" transactionFlow="true" openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="Message">
            <message clientCredentialType="Certificate" establishSecurityContext="true"/>
          </security>
        </binding>

        <binding name="DomainMessageSecurity" transactionFlow="true" openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="Message">
            <message clientCredentialType="Windows" establishSecurityContext="true"/>
          </security>
        </binding>

        <binding name="RM_NoSecurity" transactionFlow="true" openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
          <reliableSession enabled="true" ordered="false"/>
          <security mode="None"></security>
        </binding>
      </ws2007HttpBinding>

      <netTcpBinding>
        <binding name="NoSecurity" transactionFlow="true">
          <security mode="None"></security>
        </binding>


        <binding name="DomainMessageSecurity" transactionFlow="true" openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="Message">
            <message clientCredentialType="Windows"/>
          </security>
        </binding>

        <binding name="CertMessageSecurity" transactionFlow="true" openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="Message">
            <message clientCredentialType="Certificate"/>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="SimpleServiceBehavior">
          <serviceMetadata httpGetEnabled="True" policyVersion="Policy15"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>

      <service name="MessagePlatform.LoggingService.GeneralLoggingService" behaviorConfiguration="SimpleServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/"/>
          </baseAddresses>
        </host>
        <endpoint address="Logging" binding="basicHttpBinding" name="LoggingService" contract="MessagePlatform.LoggingService.Contracts.ILoggingService"/>

      </service>
      <service name="MessagePlatform.GatewayService.GeneralGatewayService" behaviorConfiguration="SimpleServiceBehavior">
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost:8000/"/>
            </baseAddresses>
          </host>
          <endpoint address="gateway" binding="basicHttpBinding" name="GatewayService" contract="MessagePlatform.GatewayService.Contracts.IGatewayService"/>
          <endpoint address="gateway" binding="basicHttpBinding" name="Db2GatewayService" contract="MessagePlatform.GatewayService.Contracts.IDb2GatewayService"/>
          <endpoint address="gateway" binding="basicHttpBinding" name="Db1GatewayService" contract="MessagePlatform.GatewayService.Contracts.IDb1GatewayService"/>

          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>

    </services>
    <client>
      <endpoint name="LoggingServiceEP" address="http://localhost/Logging" binding="basicHttpBinding" contract="MessagePlatform.LoggingService.ILoggingService">        
      </endpoint>

      <endpoint name="GatewayServiceEP" address="http://localhost/gateway" binding="basicHttpBinding" contract="MessagePlatform.GatewayService.IGatewayService">
      </endpoint>

      <endpoint name="GatewayServiceDb2" address="http://localhost/gateway" binding="basicHttpBinding" contract="MessagePlatform.GatewayService.IDb2GatewayService">
      </endpoint>

      <endpoint name="GatewayServiceDb1" address="http://localhost/gateway" binding="basicHttpBinding" contract="MessagePlatform.GatewayService.IDb1GatewayService">
      </endpoint>

    </client>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
  </startup>
</configuration>

【问题讨论】:

  • 您遇到了什么错误?此外,如果您不更改合同的签名,您可以简单地进行更改并重新编译 DLL。
  • 我想添加新方法,即扩展现有方法。将在短时间内发布我遇到的错误。我得到的最后一个是“计算机拒绝访问 127.0.0.1:8000”。想到了windows防火墙,但禁用它并不容易(可能是由于组策略,在对话框中有一个“管理员保护您免受更改以保护您自己的安全”的框)。

标签: c# wcf service client


【解决方案1】:

您还需要启用元数据交换端点,以便创建新代理:

<service name="MessagePlatform.GatewayService.GeneralGatewayService" behaviorConfiguration="SimpleServiceBehavior">
  <host>
    <baseAddresses>
      <add baseAddress="http://localhost:8000/"/>
    </baseAddresses>
  </host>
  <endpoint address="gateway" binding="basicHttpBinding" name="GatewayService" contract="MessagePlatform.GatewayService.Contracts.IGatewayService"/>
  <endpoint address="gateway" binding="basicHttpBinding" name="Db2GatewayService" contract="MessagePlatform.GatewayService.Contracts.IDb2GatewayService"/>
  <endpoint address="gateway" binding="basicHttpBinding" name="Db1GatewayService" contract="MessagePlatform.GatewayService.Contracts.IDb1GatewayService"/>

  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

</service>

然后运行

svcutil http://localhost:8000/mex

【讨论】:

  • 但是已经有一个生成的客户端。不知何故,以前的开发人员不得不生成它。在配置中没有看到任何重大变化。
  • 会试一试。重要的一行是带有“mex”的那一行,对吧?
  • 是的,它是一个额外的端点,您可以在创建代理代码后将其删除。
  • 还是不行。 svcutil 告诉它不能将 exe 作为程序集加载。所有端点都在该程序集中,它是自托管服务,如 msdn 示例中所示。
  • 可以贴出整个 serviceModel 的配置吗?
猜你喜欢
  • 2011-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多