【问题标题】:Load WCF application settings from database at runtime在运行时从数据库加载 WCF 应用程序设置
【发布时间】:2014-02-03 09:36:03
【问题描述】:

为了清楚起见,我想要实现的是在运行时修改内存中的<applicationSettings>。我的解决方案是 ntier,WCF 服务也包含依赖库的设置,因此也需要修改这些设置,即:

   <applicationSettings>

        <!-- WCF Service Settings-->
        <TestWebService.Properties.Settings>
            <setting name="TestProperty"
                     serializeAs="String">
                <value>False</value>
            </setting>
        </TestWebService.Properties.Settings>   

        <!--Dependent Class Library Settings-->
        <TestLibrary.Properties.Settings>
            <setting name="TestProperty2"
                     serializeAs="String">
                <value>ABC</value>
            </setting>
        </TestLibrary.Properties.Settings>

</applicationSettings>

应该变成:

   <applicationSettings>

        <!-- WCF Service Settings-->
        <TestWebService.Properties.Settings>
            <setting name="TestProperty"
                     serializeAs="String">
                <value>Updated  at runtime</value>
            </setting>
        </TestWebService.Properties.Settings>   

        <!--Dependent Class Library Settings-->
        <TestLibrary.Properties.Settings>
            <setting name="TestProperty2"
                     serializeAs="String">
                <value>Updated  at runtime</value>
            </setting>
        </TestLibrary.Properties.Settings>

</applicationSettings>

我知道应用程序范围设置是只读的,并且在修改物理配置文件时存在安全隐患,因此我正在寻找一种可以在可能的情况下更改 DOM 中的值的解决方案?

【问题讨论】:

    标签: wcf settings


    【解决方案1】:

    好的,使用配置文件显然行不通,所以第一步是摆脱它并在代码中配置您的 Web 服务,如下所示。

    以下是代码中的基本步骤(由于客户要求,我使用 VB,但如果您使用 C#,则可以通过转换器运行此代码):

        'Database stuff
        dim sSQL as string = "select * from wcfConfig"
        dim sPort as string 
        dim sDomain as string  
        dim sService as string 
        dim sPortcol as string  
        Using connection As New SqlConnection(connectionString)
            connection.Open()
    
            Dim command As New SqlCommand(sSQL, connection)
            Dim reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
                sPort = reader("port").toString
                sDomain = reader("Domain").toString
                sPortcol = reader("protocol").toString
                sService = reader("service").toString
            End While 
        End Using 
    
        'should look something like http ://localhost:8888/ServiceName/ServiceMethod
    
        Dim sURL As String = sProtocol & "://" & sDomain & ":" & sPort & "/" & sService
        Dim ServiceHostURI As New Uri(sURL)
        Dim ServiceHost As New ServiceHost(GetType(YourClass), ServiceHostURI) 
        'Your class is the functioning class that catches and processes the request
    
        Dim objReadQuotas = New System.Xml.XmlDictionaryReaderQuotas
        With objReadQuotas
            .MaxStringContentLength = Integer.MaxValue
            .MaxArrayLength = Integer.MaxValue
            .MaxBytesPerRead = Integer.MaxValue
            .MaxDepth = 1000
            .MaxNameTableCharCount = Integer.MaxValue
        End With
    
        'basic binding setup
        Dim b As BasicHttpBinding
        b = New BasicHttpBinding(BasicHttpSecurityMode.None)
        b.TransferMode = TransferMode.Buffered
        b.MaxReceivedMessageSize = Integer.MaxValue
        b.MessageEncoding = WSMessageEncoding.Text
        b.TextEncoding = System.Text.Encoding.UTF8 
    
        'behaviors for binding
        Dim mb As ServiceMetadataBehavior = New ServiceMetadataBehavior()
        ServiceHost.Description.Behaviors.Add(mb)
        mb.HttpsGetEnabled = False
        mb.HttpGetEnabled = True
        ServiceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex")
    
        'add the end point
        ServiceHost.AddServiceEndpoint(GetType(Accenture.Cas.Replication.SynchronizationWS.IHostInterface), binding, ServiceHostEndPoint.ToString)
    
        'open the port
        ServiceHost.Open()
    
        'monitor the host
        While True
          'broken connection case
          If ServiceHost.State <> CommunicationState.Opened Then
            Throw New Exception("SynchronizationWS Service Host failed.")   
                 'dump from loop and throw error
            Exit While
          End If
    
        Threading.Thread.Sleep(1000) 'sleep 1 second before going trying next
        End While
    

    【讨论】:

    • 感谢您的回复,麻烦的是这个 wcf 服务配置文件还包含 用于其他依赖库,所以我实现的任何解决方案都需要使用(或扩展)现有的设置机制
    • WCF 的问题是您不能像我使用的那样真正同时使用传统的配置文件和运行时配置。您可以参考您的 app.config 文件来帮助在运行时配置设置。
    猜你喜欢
    • 2014-03-20
    • 2019-03-26
    • 2015-11-11
    • 2010-12-22
    • 2010-11-26
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    相关资源
    最近更新 更多