简体   繁体   English

如何从app.config中读取终结点值并将其传递给客户端的代理类?

[英]How to read an endpoint value from an app.config and pass it to my proxy class for the client?

I have a service host and its app.config is below: 我有一个服务主机,其app.config如下:

<system.serviceModel>
 <bindings>
  <netTcpBinding>
    <binding name="NetTcpBinding_ISimSession" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="104857600" maxConnections="10" maxReceivedMessageSize="104857600">          
    </binding>

    <binding name="NetTcpBinding_ISimExportImportSession" closeTimeout="00:10:00" openTimeout="00:10:00"
           receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false" transferMode="Streamed"
           hostNameComparisonMode="StrongWildcard"
           maxBufferSize="2147483647"
           maxBufferPoolSize="2147483647"
           maxReceivedMessageSize="2147483647">         

    </binding>
  </netTcpBinding>
</bindings>

<services>
  <service name="SimCentral.Server.SimSession">
    <endpoint address="net.tcp://localhost:8732/Design_Time_Addresses/NextGenService/SimSession/" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ISimSession"
              contract="NextGenServices.Contract.ISimSessionService.ISimSession">
      <identity>
        <dns value=""/>
      </identity>
    </endpoint>
    <endpoint address="net.tcp://localhost:8735/Design_Time_Addresses/NextGenService/SimExportImportSession/" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ISimExportImportSession"
              contract="NextGenServices.Contract.ISimSessionService.ISimExportImportSession">
      <identity>
        <dns value=""/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8732/Design_Time_Addresses/NextGenService/SimSession/"/>
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior>

      <serviceMetadata httpGetEnabled="false"/>

      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

Now, in the client side, I want to create an instance for the proxy class and call the methods of my application. 现在,在客户端,我想为代理类创建一个实例并调用我的应用程序的方法。 However, my proxy class constructor is taking 2 parameters binding and endpoint which is taking from the app config file above. 但是,我的代理类构造函数使用2个参数绑定和终结点,它们是从上面的应用程序配置文件中提取的。

// constructor for the proxy
public SimSessionServiceReferenceProxyObject(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress)
        {
            ....
        }

Question is, how am I gonna get the binding and remoteAddress value from the app.config above to pass it to the constructor ? 问题是,我如何从上面的app.config获取bindingremoteAddress值,以将其传递给构造函数? Consider remoteAddress will be the first endpoint of all 3 endpoints in the app.config. 考虑remoteAddress将是app.config中所有3个端点的第一个端点。 Thank you. 谢谢。

I have use following code to access the end point address. 我使用以下代码访问端点地址。 This might help you. 这可能对您有帮助。

string crmAppSvcEndpointAddrTemplate = null;
        var serviceModelClientConfigSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
        foreach (ChannelEndpointElement endpoint in serviceModelClientConfigSection.Endpoints)
        {
            if (endpoint.Name == "MyService")
            {
                crmAppSvcEndpointAddrTemplate = endpoint.Address.ToString();
                break;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM