Preserve objects between wcf calls?
This should be fairly basic, but say I have a Public property as local variable on my WCF service, and I set this in one call to the service. Is the开发者_运维知识库re a way to preserve that data for another call to the service? (Without writing the data to xml or a db, and re-referencing it or anything like that)
Executing the calls from the Winform:
Public Class ClientSideWinForm
Private proxy As ServiceReference.Client
Private Sub Client_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
proxy = New ServiceReference.Client
End Sub
Private Sub btnStartTests_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartTests.Click
addToTxtResults("Try Chk Program Valid...")
Try
addToTxtResults(proxy.RequestChkValidProgram("Some-serial-number")) 'returns true or false and instanciates the object server side
addToTxtResults(proxy.RequestFirstName()) ' returns nothing
Catch ex As Exception
addToTxtResults(ex.ToString)
End Try
End Sub
End Class
The service itself (dumbed down a bit, but the behaivor still exists):
Public Class Service
Implements IService
Public Property X As String
Function RequestChkValidProgram(ByVal strSerialNumber As String) As Integer Implements IService.RequestChkValidProgram
X = "hello"
End Function
Function RequestFirstName() As String Implements IService.RequestFirstName
Return X
End Function
End Class
If I understand the question correctly, your service implementation's state is lost between service calls. You should look into setting the InstanceContextMode ServiceBehavior of your service implementation - it sounds like it's currently set to PerCall
, such that every service call gets its own instance. PerSession
or Single
may be better alternatives.
And while I don't necessarily agree with @John Saunders that this is a bad idea, it would be useful to have more details about what you're trying to accomplish. :)
If you only have a single instance of your service, then you can preserve data from one call to the next. You should consider telling us why you want to do this, as it sounds like a bad idea.
精彩评论