Welcome to MSDN Blogs Sign in | Join | Help

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

Published Monday, November 27, 2006 5:00 AM by Nicholas Allen
Filed under: , ,

Comments

Monday, November 27, 2006 8:46 AM by ronjacobs

# re: Mixing Message Contract Attributes

Nice - but what should you do instead?

Monday, November 27, 2006 10:15 AM by Sam Gentile

# New and Notable 128

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

Monday, November 27, 2006 11:27 AM by Nicholas Allen's Indigo Blog

# Dangers of Application Domain Recycling

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

Monday, November 27, 2006 12:12 PM by Nicholas Allen

# re: Mixing Message Contract Attributes

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

Monday, February 19, 2007 1:46 PM by Nicholas Allen's Indigo Blog

# Table of Contents Scratch Work

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

Wednesday, December 03, 2008 1:19 PM by Sam Gentile's Blog

# New and Notable 128

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

New Comments to this post are disabled
 
Page view tracker