# .NET and More > WPF, WCF, WF >  [RESOLVED] WCF service returning null data

## DNA7433

I'm a professional developer, but I'm fairly new to WCF.  I've been beating my head against the wall with this for some time, Google turned up nothing, and I'm hoping you guys will be able to help.  I thought I had a WCF project working and now I don't - and no way to revert my code back to a working state.  I can even create a small sample project to demonstrate the (bad) behavior.  I've had this problem in both VS2010 RC and now RTM.

My solution has three projects: a contracts class library, a server console app, and a client console app.

My contracts look like this:


```
namespace WcfTest.Services
{
    [DataContract]
    public class TestData
    {
        public string Value { get; set; }
    }

    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        TestData DoSomeWork();
    }
}
```

Server console app:


```
<service name="WcfTest.Services.TestService">
  <endpoint address="http://localhost:8002/WcfTest/Services/Test"
            binding="wsHttpBinding"
            contract="WcfTest.Services.ITestService" />
</service>

static void Main(string[] args)
{
    ServiceHost serviceHost = new ServiceHost(typeof(TestService));
    serviceHost.Open();
    Console.ReadKey();
    serviceHost.Close();
}
```

Client console app:


```
<client>
  <endpoint name="Test"
            address="http://localhost:8002/WcfTest/Services/Test"
            binding="wsHttpBinding"
            contract="WcfTest.Services.ITestService">
  </endpoint>
</client>

static void Main(string[] args)
{
    ChannelFactory<ITestService> testServiceFactory = new ChannelFactory<ITestService>("Test");
    testServiceFactory.Open();
    ITestService testService = testServiceFactory.CreateChannel();
    TestData value = testService.DoSomeWork();
    Console.WriteLine(value.Value);
    testServiceFactory.Close();
    Console.ReadKey();
}
```

When I run the server and client apps (in that order) they both run without throwing any exceptions, but the only thing that shows on the client output is a blank line.  I've also set a breakpoint and looked at the return value from the service.  The variable value has a valid instance of the class TestData, but the Value property is null.  I've read that this can happen if your data contracts on the client and server sides are in different namespaces, but since I'm reusing the contract via referencing a 3rd assembly I don't see how that's possible.  Any ideas?  Thanks for your help.

----------


## Lightning

Could you post the implementation of the ITestService interface?

----------


## DNA7433

Oops, forgot that part.


```
public class TestService : ITestService
{
    public TestData DoSomeWork()
    {
        return new TestData() { Value = "Hello World!" };
    }
}
```

----------


## Lightning

You could debug the server side and see if it returns the correct value, and you could enabled WCF tracing ( in the app.config file). So you can see where the problem is.

----------


## DNA7433

What do you mean by "debug the server side and see if it returns the correct value"?  I set a breakpoint in TestService.DoSomeWork(), and it created the object and returned it.  What else can I do?

So I enabled tracing on both the client and server sides at the information level.  I opened the message file in the trace viewer.

I found two messages with Actions titled "http://tempuri.org/ITestService/DoSomeWork" and "http://tempuri.org/ITestService/DoSomeWorkResponse".  Other than that I don't see anything that makes any sense to me in either the messages or the activities.

What should I be looking for?

Thanks

----------


## DNA7433

Over a hundred thread views...I'm guessing no one has seen this problem before?  Can someone please put my code into a quick project and verify that it works/doesn't work them?  Or can someone point me to an example/tutorial online that has a downloadable sample project with a service that returns a DataContract object that works?  Thanks

----------


## chris128

Its been a while since i've used WCF so sorry if this is b*llocks but dont you have to specify another attribute on properties within your data contract to show that those should be transmitted with the data contract? So something like this (but the attribute name is almost certainly wrong as I cant remember what it was called) :



```
[DataContract]
    public class TestData
    {
        [DataContractMember]
        public string Value { get; set; }
    }
```

EDIT: aha! Yeah you do have to specify another attribute (and I was close with the name!). Here's an example from one of my old VB projects:



```
<DataContract()> _
    Public Class ServerInfoDescription

        Private _ServerVersion As String
        <DataMember()> _
        Public Property ServerVersion() As String
            Get
                Return _ServerVersion
            End Get
            Set(ByVal value As String)
                _ServerVersion = value
            End Set
        End Property

        Private _ServerStartTime As DateTime
        <DataMember()> _
        Public Property ServerStartTime() As DateTime
            Get
                Return _ServerStartTime
            End Get
            Set(ByVal value As DateTime)
                _ServerStartTime = value
            End Set
        End Property

    End Class
```

Hope that sorts it out for you, I remember how frustrated I used to get with WCF!  :Smilie:

----------


## DNA7433

Thanks!!  I can't believe I missed that - I tried building my WCF project from scratch and I should have gone back to one of the examples online.  I forgot that data contract serialization is opt-in instead of the traditional opt-out for other .NET serialization.

----------


## chris128

No problem  :Smilie: 
One completely random word of warning about something that tripped me up for ages too - you cant make a read-only property available via data contract  :Wink:

----------


## chris128

Oh and dont forget to mark this thread as Resolved  :Smilie:

----------

