从工作流服务的配置文件中读取端点

本文关键字:读取 端点 配置文件 工作流 服务 | 更新日期: 2023-09-27 18:07:20

我知道如何通过使用c#从活动中的配置文件中读取信息

 var servicesSection = (ClientSection)WebConfigurationManager.GetSection("system.serviceModel/client");
 ChannelEndpointElement endpoint = servicesSection.Endpoints[0];

但是当我试图在工作流服务的if语句中读取此信息时,它不起作用。我尝试使用以下代码从web读取端点信息。配置文件。

((ClientSection)WebConfigurationManager.GetSection("system.serviceModel/client")).Endpoints[0].toString().Equals("");

但它不起作用。不知怎么的,它不理解类型转换,我不能将GetSection输出转换为clientSection对象。你知道我如何在工作流服务的if语句中做到这一点吗?(在调用其他活动之前检查配置文件中的内容)

从工作流服务的配置文件中读取端点

我也有类似的需求,在制作了一些原型之后,我采用了如下方法。希望这能对你和其他人有所帮助。

/* Using in System.ServiceModel.dll */
    using System.ServiceModel.Configuration;
    using System.Web.Configuration;
    /* Inside any method  */
    var clientSection = ((ClientSection)(WebConfigurationManager.GetSection("system.serviceModel/client")));
                if (clientSection != null)
                {
                    foreach (ChannelEndpointElement endPoint in clientSection.Endpoints)
                    {
                        ..... endPoint.Name / endPoint.Address etc.
                    }
                }

您可以从配置中读取任何元素并将其强制转换为适当的元素类型。

为了从app.config中读取端点、绑定和其他部分,我们定义了一组Section类来帮助我们读取设置。

例如,要读取绑定列表,只需使用

private void GetNetTcpBindingName()
    {
        Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);
        BindingsSection oBinding = serviceModel.Bindings;
        List<BindingCollectionElement> bindingCollection = oBinding.BindingCollections;
        NetTcpBindingCollectionElement netTCPBindingCollectionElement = (NetTcpBindingCollectionElement)bindingCollection.Where(obj => obj.BindingName.Equals("netTcpBinding")).SingleOrDefault();
        if (netTCPBindingCollectionElement != null)
        {
            Console.WriteLine(netTCPBindingCollectionElement.ConfiguredBindings.ElementAt(0).Name);
        }
    }

给定下面的app.config XML(感兴趣的部分用粗体表示),

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ITrainerManagement" />
      </basicHttpBinding>
      **<netTcpBinding>
        <binding name="NetTcpBinding_ILiveStream">
          <security mode="None" />
        </binding>
      </netTcpBinding>**
    </bindings>
    <client>
      <endpoint address="net.tcp://sever-pc/PST.TS.LiveStream/LiveStream.svc"
          binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ILiveStream"
          contract="LiveStreamServiceReference.ILiveStream" name="NetTcpBinding_ILiveStream" />
      <endpoint address="http://10.5.50.115/PST.TS.TrainerService/TrainerManagement.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITrainerManagement"
          contract="TrainerManagementServiceReference.ITrainerManagement"
          name="BasicHttpBinding_ITrainerManagement" />
    </client>
  </system.serviceModel>

希望这对你有帮助。