locked
System.ServiceModel.Channels.Message error RRS feed

  • Question

  • User-81382370 posted

    Hi

    I try to set up a new Wcf service which look like this..

    [ServiceContract]
    public interface IRestService
    {
    
        [WebInvoke(Method = "POST", UriTemplate = "PostTest", RequestFormat = WebMessageFormat.Xml)]
        Message DoPost(string uName, string pWord, string msg);
    
    }

    public class RestService : IRestService
    {
    
        private XmlReader MakeXml(string Status)
        {
            var buffer = new StringBuilder();
            using (XmlWriter MyXml = XmlWriter.Create(buffer))
            {
                MyXml.WriteStartDocument();
                MyXml.WriteStartElement("root");
    
                // Write the status.
                MyXml.WriteStartElement("status");
                MyXml.WriteString(Status);
                MyXml.WriteEndElement();
    
                MyXml.WriteEndDocument();
            }
    
            TextReader reader = new StringReader(buffer.ToString());
            return XmlReader.Create(reader);
        }
    
    public Message DoPost(string userName, string PassWord, string Msg)
    {
            XmlReader xmlReader = MakeXml("OK");
            return Message.CreateMessage(MessageVersion.None, "", xmlReader);
    }
    
    }

    Everything compile but when I try to add the service reference I get this error..

    "The operation 'DoPost' could not be loaded because it has a parameter or return type of type System.ServiceModel.Channels.Message or a type that has MessageContractAttribute and other parameters of different types. When using System.ServiceModel.Channels.Message or types with MessageContractAttribute, the method must not use any other types of parameters."

    I don't know exactly what I do wrong here, can someone please help?

    Friday, February 28, 2014 4:45 AM

Answers

  • User-417640953 posted

    But what do I need to change then? Can you show me? Maybe its obvious but I'm lost right now...

    Hello,

    So you should make your service method signature like below.

    # Contract types as return vaule and parameters.

    # Primitive type as return vaule and parameters.

    If you want your operation return the Message (contract type), so you should set operation parameters as contract types.

    When an operation contract required to pass a message as a parameter or return value as a message, the type of this message will be defined as message contract.

    A message contract defines the elements of the message (like as Message Header, Message Body), as well as the message-related settings,

    such as the level of message security. Message contracts give you complete control over the content of the SOAP header, as well as the structure of the SOAP body.

    http://www.dotnet-tricks.com/Tutorial/wcf/3ISW280913-Understanding-different-types-of-WCF-Contracts.html

    Thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 3, 2014 8:33 PM

All replies

  • User-417640953 posted

    Hi modig,

    Thank you post the issue to asp.net forum.

    The operation 'DoPost' could not be loaded because it has a parameter or return type of type System.ServiceModel.Channels.Message or a type that has MessageContractAttribute and other parameters of different types.

    "The problem is that message contracts cannot be used at the same time as other parameter types.  In this case, the return value of the operation is a string. 

    Return values are just another output parameter, so this operation is mixing a message contract message with a primitive parameter type. 

    This fails because message contracts give you control of the layout of the SOAP message, preventing the system from melding in these additional parameters."

    Your the operation return values' type must be consistent with parameters' type.

    [ServiceContract]
    
    public interface IRegisterOK
    
        {
    
            [OperationContract]
    
            string Operation1(string firstname, string lastname); 
    
            [OperationContract]
    
            Message Operation2(Message message);
    
    }

    http://ozkary.blogspot.com/2011/05/wcf-service-operations-cant-be-loaded.html

    Hope this helps, thanks.

    Best Regards!

    Monday, March 3, 2014 1:11 AM
  • User-81382370 posted

    I'm not sure I follow you, I only have the DoPost that returns a message, whats mixed?

    Monday, March 3, 2014 4:54 AM
  • User-417640953 posted

    modig

    I'm not sure I follow you, I only have the DoPost that returns a message, whats mixed?

    Hi modig,

    Thanks for your response. As Oscar Garcia mentioned below.

    This basically means that a particular operation is using a combination of message contract types and primitive types in any of the following combinations:

    • MixType1:      Contract type and primitive types as operation parameters
    • MixType2:      Contract type as a parameter and primitive type as return type
    • MixType3:      Primitive type as a parameter and Contract type as return type

    As your service method like below.

    public Message DoPost(string userName, string PassWord, string Msg)
    {
            XmlReader xmlReader = MakeXml("OK");
            return Message.CreateMessage(MessageVersion.None, "", xmlReader);
    }
    

    It has the primitive type (string userName, string PassWord, string Msg) as a parameter and contract type (Message) as return type.

    Thanks.

    Best Regards!

    Monday, March 3, 2014 6:58 AM
  • User-81382370 posted

    But what do I need to change then? Can you show me? Maybe its obvious but I'm lost right now...

    Monday, March 3, 2014 8:08 AM
  • User-417640953 posted

    But what do I need to change then? Can you show me? Maybe its obvious but I'm lost right now...

    Hello,

    So you should make your service method signature like below.

    # Contract types as return vaule and parameters.

    # Primitive type as return vaule and parameters.

    If you want your operation return the Message (contract type), so you should set operation parameters as contract types.

    When an operation contract required to pass a message as a parameter or return value as a message, the type of this message will be defined as message contract.

    A message contract defines the elements of the message (like as Message Header, Message Body), as well as the message-related settings,

    such as the level of message security. Message contracts give you complete control over the content of the SOAP header, as well as the structure of the SOAP body.

    http://www.dotnet-tricks.com/Tutorial/wcf/3ISW280913-Understanding-different-types-of-WCF-Contracts.html

    Thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 3, 2014 8:33 PM