none
MessageBody object gets null at Service side when calling service with Channelfactory in WCF RRS feed

  • Question

  • I am creating a WCF service. I am sending request with messageContract, MessageBody is a class (DataContract) 
    Request is filled with data at client side and send to service, but at service side messageBody object gets null.
    Below are the conditions,

    1 - Create proxy using Channelfactory (faces this issue only when call service with Channelfactory, works fine when use ServiceReference proxy)

    2 - MessageBody should be class object (works fine when using predefined datatypes "tried using String instead which has worked")

    code sample for reference:

    IService1.cs (will be at Service side and client side)

    [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            Response CallService(Request request);
        }

        [MessageContract]
        public class Request
        {
            [MessageHeader]
            public string Id { get; set; }

            [MessageBodyMember]
            public RequestDetail RequestDetail { get; set; }
        }

        [MessageContract]
        public class Response
        {
            [MessageBodyMember]
            public string Reply { get; set; }
        }

        [DataContract]
        public class RequestDetail // class used as MessageBodyMember in request
        {
            [DataMember]
            public string RequestData { get; set; }
        }

    Service1.cs (Service implementation)

    public class Service1 : IService1
        {
            public Response CallService(Request request)
            {
                Response response = new Response();
                if (request != null)
                {
                    if (request.RequestDetail == null)
                    {

                        response.Reply = "RequestData is null";
                    }
                    else
                    {
                        response.Reply = "Got request data as - " + request.RequestDetail.RequestData;
                    }
                }
                return response;
            }
    }

    Program.cs

    // Call service using Channelfactory (at client side)

    static void Main(string[] args)
            {
                try
                {
                    // Create proxy using ChannelFactory
                    WSHttpBinding myBinding = new WSHttpBinding();

                    EndpointAddress myEndpoint = new EndpointAddress(new Uri("http://localhost:26513/Service1.svc"));
                    ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding, myEndpoint);

                    IService1 channelFactoryClient = myChannelFactory.CreateChannel();

                    // call service 

                    Request req = new Request();
                    req.Id = "12345";
                    req.RequestDetail = new RequestDetail();
                    req.RequestDetail.RequestData = "Data send from Service";
                    Response resp = channelFactoryClient.CallService(req);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.Read();
    }

    Tuesday, September 16, 2014 1:14 PM

Answers

  • I got the reason why with ChannelFactory "RequestDetail" is getting null at Service side and working when using add web ServiceReference proxy.

    The Just put NameSpace with DataContract, like as below..

    [DataContract(Namespace="http://schema.myservice.com/TestService/customtypes")]
    public class RequestDetail // class used as MessageBodyMember in request
    {
        [DataMember]
        public string RequestData { get; set; }
    }

    Same class should be at client side.

    It worked for me.. Thanks

    • Marked as answer by Bhoopen Monday, September 29, 2014 6:42 AM
    Monday, September 29, 2014 6:42 AM

All replies

  • Have you tried to enable the WCF Trace?

    Thursday, September 18, 2014 12:44 PM
  • Thanks for putting this point.

    I haven't check it this after enable WCF trace.. I check it and update.

    Thursday, September 18, 2014 1:34 PM
  • Hi Mike,

    I have checked Trace for dig down this issue. I tried with below settings, 

     <messageLogging
               logEntireMessage="false"
               logMalformedMessages="false"
               logMessagesAtServiceLevel="true"
               logMessagesAtTransportLevel="true"
               maxMessagesToLog="3000" >
          </messageLogging>

    I could not find the MessageBody in logs which is actually missing. 

    If I log entire message as with below settings,

    <messageLogging
               logEntireMessage="true"
               logMalformedMessages="true"
               logMessagesAtServiceLevel="true"
               logMessagesAtTransportLevel="true"
               maxMessagesToLog="3000" >
          </messageLogging>

    I can see the MessageBody as well in the log.

    so with these scenarios I think that the messageBody is missing somewhere may be at TransportLevel.

    Please correct me or suggest if I am wrong. 

    Friday, September 19, 2014 6:30 AM
  • I got the reason why with ChannelFactory "RequestDetail" is getting null at Service side and working when using add web ServiceReference proxy.

    The Just put NameSpace with DataContract, like as below..

    [DataContract(Namespace="http://schema.myservice.com/TestService/customtypes")]
    public class RequestDetail // class used as MessageBodyMember in request
    {
        [DataMember]
        public string RequestData { get; set; }
    }

    Same class should be at client side.

    It worked for me.. Thanks

    • Marked as answer by Bhoopen Monday, September 29, 2014 6:42 AM
    Monday, September 29, 2014 6:42 AM