Freigeben über


Mixing Message Contract Attributes

What's wrong with the following code? The error message when you try this is better than it used to be a few months ago. However, since you don't see the error until you try to run your service, I still see people making this mistake.

 [MessageContract]
public class MyMessage
{
   [MessageBodyMember]
   public string name;
}

[ServiceContract]
public interface IService
{
   [OperationContract]
   string Method(MyMessage message);
}

public class Service : IService
{
   public string Method(MyMessage message)
   {
      return "Hello: " + message.name;
   }
}

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.

By the way, the error message you get when you try to mix message contracts looks like this.

System.InvalidOperationException: The operation 'Method' 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.

Next time: Controlling the Synchronization Context

Comments

  • Anonymous
    November 27, 2006
    Nice - but what should you do instead?

  • Anonymous
    November 27, 2006
    Not too much going on today. Ruby/Rails Rails 1.2 RC1 is out , the first release in almost 8 months.

  • Anonymous
    November 27, 2006
    Secure calls to a hosted service are intermittently failing with the following error message: An unsecured

  • Anonymous
    November 27, 2006
    Ron, There are two ways to make the operation contract consistent.  You can define a message contract for the format of the message that gets sent in response, MyMessageResponse for instance.  Or, you can remove the message contract and declare the individual parameters that are being passed. For instance, this particular example is probably easiest to write like this: [OperationContract] string Method(string name);

  • Anonymous
    February 19, 2007
    I haven't forgotten about the goal to put together a table of contents for all of these articles. The

  • Anonymous
    December 03, 2008
    Not too much going on today. Ruby/Rails Rails 1.2 RC1 is out , the first release in almost 8 months. The biggest thing seems to be extensive REST support. Here is what is new in ActiveRecord , new in Action Pack and new in Active Support . Blogsphere