When passing an object as an argument to a method, string properties are populated, but integers are null
I'm not sure WCF being involved is significant or not.
I have a class and method exposed to a client asp.net app. The class looks like
<DataContract()> _
Public Class Class1
Private v_string As String
Private v_integer As Integer
Public Sub New()
v_string = ""
v_integer = -1
End Sub
<DataMember()> _
Public Property P_String() As String
Get
Return v_string
End Get
Set(ByVal value As String)
v_string = value
End Set
End Property
<DataMember()> _
Public Property P_Integer() As Integer
Get
Return v_integer
End Get
Set(ByVal value As Integer)
v_integer = value
End Set
End Property
End Class
The method is declared as
<OperationContract()> _
Function GetStuff(ByVal bar As Class1) As String
In the client code I create an instance of Class1 and set the values for v_string and v_integer, but using Wireshark to look at the xml being sent to the server only a value for v_string is being sent as part of Class1. I'm guessing this is because it considers the value of v_integer to be null/not set. Here is an example of the client code.
Dim MyService as New Service1.ServiceClient
Dim test as New Service1.Class1
test.P_integer = 1
te开发者_运维问答st.P_string = "hello"
Dim result as String = MyService.GetStuff(test)
I'm guessing this is a problem with how different types are passed/used since Integer is a intergral type and String is a class, but can't seem to work out what to do to fix the problem.
WCF will only ever transmit data - you cannot expose a "method" via WCF.
WCF is a serialized messaging system - only serialized data travels between client and server - there is no other connection (no "remote object" or something like that) going on.
But both string
and int
should be handled no problem....
Maybe you are stumbling over this behavior?? I don't fully understand where your problem is, too - what are you expecting, and what are you seeing instead?? What does your service method actually do in its implementation??
Update: when I do the same thing as you have, and then use WCF Tracing to see what's happening, this is the request going into the service:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:8433/Services/GetStuff</To>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IGetStuffService/GetStuff</Action>
</s:Header>
<s:Body>
<GetStuff xmlns="http://tempuri.org/">
<data xmlns:a="http://schemas.datacontract.org/2004/07/wcf_test"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:P_Integer>5</a:P_Integer>
<a:P_String>Where to go for holidays...</a:P_String>
</data>
</GetStuff>
</s:Body>
</s:Envelope>
So what is it you're not seeing that should be there??..... I clearly see both the integer and the string value in that SOAP message - as expected.
Could it be you're not seeing your value of 1
because that's hidden away in the byte stream somewhere?? Try specifying some other value, like 4711067
or something - that won't get lost in your message....
To enable WCF tracing, put these two sections into your web.config
or app.config
of your WCF service:
<system.diagnostics>
<sources>
<source name="UserTraceSource" switchValue="Warning, ActivityTracing" >
<listeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="C:\logs\UserTraces.svclog" />
</listeners>
</source>
</sources>
<trace autoflush="true" />
</system.diagnostics>
(make sure the C:\logs
directory exists before hand!!), and
<system.serviceModel>
<diagnostics>
<messageLogging maxMessagesToLog="30000"
logEntireMessage="true" logMalformedMessages="true"
logMessagesAtServiceLevel="false"
logMessagesAtTransportLevel="true">
<filters>
<clear/>
</filters>
</messageLogging>
</diagnostics>
</system.serviceModel>
Now you'll get *.svclog
files in C:\logs
- open Windows Explorer, double-click on them, and you should be taken into the WCF Trace Viewer for analysis.
A client of ours is having the same problem and we have not found a fix, the encapsulated a WCF service in a vb.net 4.0 dll and then from thier exe they showed me from thier machine they are passing strings and integers, with a packet sniffer on my server, the only soap xml tags i see are the string tags.
I have written a client in c# 4.0 and it works fine, even from thier machine.
Michael Evanchik
This doesn't quite make sense to me:
<DataMember()> _
Public Property P_Integer() As Integer
Get
Return v_integer
End Get
Set(ByVal value As Integer)
v_string = value
End Set
End Property
Shouldn't that Set function be:
v_integer = value
A colleague of mine found that everthing works properly we change the decorations for the data members from:
<DataMember()>_
Public Property P_Integer() As Integer
...
End Property
To:
<DataMember(IsRequired:=True)> _
Public Property P_Integer() As Integer
...
End Property
for all value types. Class types such as string work fine without this.
精彩评论