WCF服务自定义配置

6 .net wcf .net-3.5

在托管多个WCF服务的应用程序中,为每个服务添加自定义配置信息的最佳方法是什么?例如,您可能希望传递或设置公司名称或指定connectionString服务或其他一些参数.

我猜这可能是通过实现IServiceBehavior实现的.

即...像....

<behaviors>
  <serviceBehaviors>
    <behavior name="MyBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug />
      <customBehavior myCompany="ABC" />
    </behavior>
    <behavior name="MyOtherBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug />
      <customBehavior myCompany="DEF" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<services>
  <service behaviorConfiguration="MyBehavior" name="MyNameSpace.MyService">
    <endpoint address="" behaviorConfiguration="" binding="netTcpBinding" 
      name="TcpEndpoint" contract="MyNameSpace.IMyService" />
    <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
      name="TcpMexEndpoint" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:4000/MyService" />
      </baseAddresses>
    </host>
  </service>
  <service behaviorConfiguration="MyOtherBehavior" name="MyNameSpace.MyOtherService">
    <endpoint address="" behaviorConfiguration="" binding="netTcpBinding" 
      name="TcpEndpoint" contract="MyNameSpace.IMyOtherService" />
    <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
      name="TcpMexEndpoint" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:4000/MyOtherService" />
      </baseAddresses>
    </host>
  </service>
</services>
Run Code Online (Sandbox Code Playgroud)

在MyService上设置ABC,在MyOtherService上设置DEF(假设它们与公司名称有一些共同的接口).

任何人都可以详细说明你如何实现这个?

TIA

迈克尔

Che*_*eso 8

我知道这是旧的,但它从未被标记为已回答,所以我想我会开枪.如果我理解你所追求的是什么,你可以使用自定义的ServiceHostFactory来实现.
好后在这个位置.

您设置yuour自定义ServiceHostFactory,如下所示:

<%@ ServiceHost
 Language="C#"
 Debug="true"
 Service="Ionic.Samples.Webservices.Sep20.CustomConfigService"
 Factory="Ionic.ServiceModel.ServiceHostFactory"%>
Run Code Online (Sandbox Code Playgroud)

然后,在ServiceHostFactory中,您可以覆盖名为ApplyConfiguration的方法.通常,对于在IIS中托管的WCF应用程序,WCF会自动在web.config中查找配置.在此示例中,我们重写该行为以首先查找以WCF服务描述命名的配置文件.

protected override void ApplyConfiguration()
{
    // generate the name of the custom configFile, from the service name:
    string configFilename = System.IO.Path.Combine ( physicalPath,
        String.Format("{0}.config", this.Description.Name));

    if (string.IsNullOrEmpty(configFilename) || !System.IO.File.Exists(configFilename))
        base.ApplyConfiguration();
    else
        LoadConfigFromCustomLocation(configFilename);
}
Run Code Online (Sandbox Code Playgroud)

您可以用"任何东西"替换它 - 例如,在数据库表中查找配置.

还有一些方法可以完成拼图.

private string _physicalPath = null;
private string physicalPath
{
    get
    {
        if (_physicalPath == null)
        {
            // if hosted in IIS
            _physicalPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

            if (String.IsNullOrEmpty(_physicalPath))
            {
                // for hosting outside of IIS
                _physicalPath= System.IO.Directory.GetCurrentDirectory();
            }
        }
        return _physicalPath;
    }
}


private void LoadConfigFromCustomLocation(string configFilename)
{
    var filemap = new System.Configuration.ExeConfigurationFileMap();
    filemap.ExeConfigFilename = configFilename;
    System.Configuration.Configuration config =
        System.Configuration.ConfigurationManager.OpenMappedExeConfiguration
        (filemap,
         System.Configuration.ConfigurationUserLevel.None);
    var serviceModel = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
    bool loaded= false;
    foreach (System.ServiceModel.Configuration.ServiceElement se in serviceModel.Services.Services)
    {
        if(!loaded)
            if (se.Name == this.Description.ConfigurationName)
            {
                base.LoadConfigurationSection(se);
                loaded= true;
            }
    }

    if (!loaded)
        throw new ArgumentException("ServiceElement doesn't exist");
}
Run Code Online (Sandbox Code Playgroud)


tom*_*asr 0

这在很大程度上取决于您希望在何处以及如何使用所述信息。如果这不是对基础设施做很多事情(即让服务运行和处理请求),我很想说尝试将其推入 WCF 行为可能会增加比其价值更多的复杂性。仅使用您自己的自定义配置部分可能会更简单。

您能否澄清一下您希望如何在运行时使用这些信息?也许这样我们可以提供更明确的建议......