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.

public class MyMessage
public string name;

public interface IService
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 (6)

  1. Ron Jacobs says:

    Nice – but what should you do instead?

  2. Sam Gentile says:

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

  3. Secure calls to a hosted service are intermittently failing with the following error message: An unsecured

  4. 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:


    string Method(string name);

  5. I haven’t forgotten about the goal to put together a table of contents for all of these articles. The

  6. 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