在运行时从数据库加载WCF应用程序设置

时间:2014-02-03 09:36:03

标签: wcf settings

为了清楚起见,我想要实现的是在运行时修改内存中的<applicationSettings>。我的解决方案更简洁,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中的值的解决方案吗?

1 个答案:

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