Results 1 to 10 of 10

Thread: [RESOLVED] WCF service returning null data

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Resolved [RESOLVED] WCF service returning null data

    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:
    Code:
    namespace WcfTest.Services
    {
        [DataContract]
        public class TestData
        {
            public string Value { get; set; }
        }
    
        [ServiceContract]
        public interface ITestService
        {
            [OperationContract]
            TestData DoSomeWork();
        }
    }
    Server console app:
    Code:
    <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:
    Code:
    <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.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: WCF service returning null data

    Could you post the implementation of the ITestService interface?
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: WCF service returning null data

    Oops, forgot that part.
    Code:
    public class TestService : ITestService
    {
        public TestData DoSomeWork()
        {
            return new TestData() { Value = "Hello World!" };
        }
    }
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  4. #4
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: WCF service returning null data

    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.
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: WCF service returning null data

    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
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: WCF service returning null data

    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
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WCF service returning null data

    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) :

    Code:
    [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:

    Code:
    <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!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: WCF service returning null data

    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.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WCF service returning null data

    No problem
    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WCF service returning null data

    Oh and dont forget to mark this thread as Resolved
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width