0

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

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

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

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

但它不起作用。不知何故,它不理解类型转换,我无法将 GetSection 输出转换为 clientSection 对象。您知道如何在工作流服务的 if 语句中执行此操作吗?(在调用其他活动之前从配置文件中检查一些内容)

4

2 回答 2

0

要从 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>

希望这可以帮助。这可用于读取任何标准设置。

于 2014-05-13T08:41:35.930 回答
0

我有类似的要求,经过一些原型设计后,我按照以下方式进行了管理。希望这会对您和其他人有所帮助。

/* 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.
                    }
                }

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

于 2013-11-27T14:26:56.873 回答