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

时间:2013-04-04 15:16:27

标签: c# web-services workflowservice

我知道如何使用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语句中做到这一点?(在调用其他一些活动之前从配置文件中检查一下)

2 个答案:

答案 0 :(得分: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.
                    }
                }

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

答案 1 :(得分: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>

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