views:

53

answers:

2

Hey there!

I am getting the following exception and don't have much clue about what and how it should be fixed:

The operation 'ShowData' 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.

My code:

[ServiceContract(SessionMode=SessionMode.NotAllowed)] 
public interface IHelper 
{
  [WebGet(UriTemplate = "/cgi/manager.exe?GetData={data}")]
  [OperationContract]
  Message ShowData(int data);
}

public class Helper : IHelper 
{
  public Message ShowData(int data)
  {
    var result = new StringBuilder(...);

    foreach (...)
    {
      result.AppendFormat(...);
    }

    result.AppendLine(...);

    return WebOperationContext.Current.CreateTextResponse(result.ToString(), "text/xml", Encoding.ASCII);
  } 

I guess it says that I can't intermix Message with int? What is the correct way to rely on parsing the request than?

A: 

Is your return type actually of type System.ServiceModel.Channels.Message?

KJN
The question body updated with the relevant code (IHelper implementation). Can you have a look please?
BreakPhreak
A: 

You're getting the error because you're returning the WCF Message type. This means you'll either have to remove the data parameter from the inputs, or also make it a Message type instead of an int.

The Message class is a fundamental piece of the WCF infrastructure. It's documented here: http://msdn.microsoft.com/en-us/library/ms734675.aspx

It's better to define your own data-contract type rather than use Message.

Tim Roberts
thanks! I've used the Message only because I need to return plain ASCII text. I'll try to return Byte[] and will see if it works.
BreakPhreak
You won't get the exception if you return Byte[], but why not return a string if you're simply sending back text?
Tim Roberts
Sending back string results in sending back Unicode with a few special characters at the start (3 bytes). The legacy application expects an XML ASCII oldskul string. If you have any other recommendation - I'll be glad to hear.
BreakPhreak
@BreakPhreak - Ah, right. Strings in .NET are unicode (UTF-16) which explains the funny bytes at the start (the byte-ordering marks). A Byte[] is probably the correct choice then.
Tim Roberts
Funny but it's not: http://stackoverflow.com/questions/3805330/getting-gibberish-instead-of-hello-world-from-a-service-with-webhttpbinding (just letting you know that the saga continues). And thanks again for the answer.
BreakPhreak