0

在此先感谢您的帮助。我阅读了几篇关于并发、实例化和线程安全的文章,但我承认我仍然有疑问。事实上,文章中的信息有时是相互矛盾的。

我有一个简单的 WFC 服务,它接收一个日期期间(StartDate 和 EndDate)。该服务必须在 StartDate 和 EndDate 定义的时间段内搜索名称中包含日期的文件。然后将创建的文件压缩并上传到服务器。

经过多次测试后,我从未注意到该服务的运行有任何问题。但是在生产环境中使用该服务之前,我需要确保服务代码是安全的,以确保来自客户端的多个同时请求不会影响其他请求的数据完整性。

我发布了一个代码片段,并在代码中添加了评论/问题。

请注意,我已将服务 Instancing 设置为 PerCall。正如我在 microsoft 文档中注意到的那样,“在 PerCall 实例中,并发性是不相关的,因为每条消息都由一个新的服务实例处理。”。但是每个服务实例本身都是线程安全的吗?换句话说,每个客户端调用服务不能改变其他调用的数据?

国王问候。坎波斯

' - - - - - - - - - - - - - - -  WFC Service  Contract - - - - - - - - - - - - - - - 
<ServiceContract()>
Public Interface IMyReports
    <OperationContract(IsOneWay:=True)>
    Sub SvcRequest(ByVal ClientID as integer, ByVal startDate as string, ByVal EndDate)
End Interface

' - - - - - - - - - - - - - - -  WFC Service - - - - - - - - - - - - - - - 

<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerCall)>
Public Class SvcDoRequest
    Implements IMyReports

    '??? These Private members can be changed by another Service Call?
    Private Dat1 as string=""
    Private Dat2 as string=""
    Private SearchFolder as string=""

    Public Sub SvcRequest(StartDate as string, EndDate as String) Implements IMyReports.SvcRequest
        SearchFolder= "c:\SearchFolder\" 
        Dat1 as string=StarDate 
        Dat2 as string=EndDate  
      
        Dim ListOfFiles as new List(of String)  
      
       'Recursive File Search 
       '??? Arguments passed byRef. Can the Arguments be Changed by another Service Call?
      
        Call FGetFiles(SearchFolder, ListOfFiles)
        
        Dim Result as integer=0  
        If ListOfFiles.Count>0  
            Dim ZipFileName as string="c:\MyZipFile.Zip" 
            Dim ErrMsg as string=""

            ' External Library to Create a ZIP File containing ListOfFiles()
            '??? ErrMsg is passed byRef in order to return a possible error message. 
            '??? Can ErrMsg be Changed by another Service Call?

            Dim ClZip As New  MyZipLibrary.ZipClass
            If ClZip.FCreateZip(ListOfFiles, ZipFileName, ErrMsg)=False Then
               Call WriteToLog(ErrMsg)
           Result=-1
        Else
           Result=1             
        End if
    End if
     
    ' Upload ZipFile 
    '           
    End Sub

    Private Function FGetFiles(ByRef Folder As String, ByRef LFiles As List(Of String)) as boolean 
    
        '??? is this recursive function safe? Byref args Folder,LFiles can be changed by another Call?

        For Each File1 As String In Directory.GetFiles(Folder)
            Dim FileInfo1 As New FileInfo(File1)
            Dim FileDate  As String = FileInfo1.Name.Substring(0,8)       
             If FileDate >= Dat1 And FileDate <= Dat2 Then
                 LFiles.Add(FileInfo1.FullName)
            End If
        Next
        For Each Dir1 As String In Directory.GetDirectories(Folder)
            Call FGetFiles(Dir1, LFiles)
        Next
        Return True
    End Function
End Class

'- - - - - - - - - - - - - - - - - - - - - - - - -  ZIP Library - - - - - - - - - - - 
Public Class MyZipLibrary
    Public Function FCreateZip(byval ListOfFiles as List(of String), 
                               Byval ZipFileName as string, ByRef ErrMsg as string) as Boolean    
        If Not CreateZipFile()
            'Byref Argument ErrMsg. can be changed by another client Call?                         
            ErrMsg="Unable To Create ZipFile"
            Return false
        Else
            Return True
        End if
    End function
End Class
4

0 回答 0