Use of MessageContract crashes WCF service on startup
Asked Answered
C

2

6

I'm trying to add a MessageContract to my WCF service, similar to what's going on in this question: WCF: using streaming with Message Contracts

Here's the exception I get: The operation 'UploadFile' 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.

Here are my contracts:

[ServiceContract]
public interface IFile
{
    [OperationContract]
    bool UploadFile(FileUpload upload);
}
[MessageContract]
public class FileUpload
{
    [MessageHeader(MustUnderstand = true)]
    public int Username { get; set; }
    [MessageHeader(MustUnderstand = true)]
    public string Filename { get; set; }
    [MessageBodyMember(Order = 1)]
    public Stream ByteStream { get; set; }
}

And here's the binding configuration I'm using in my app.config:

  <netTcpBinding>
    <binding name="TCPConfiguration" maxReceivedMessageSize="67108864" transferMode="Streamed">
      <security mode="None" />
    </binding>
  </netTcpBinding>

Right now I'm thinking that this may have something to do with the type of binding I'm using, but I'm not entirely sure.

Characterize answered 24/5, 2011 at 5:58 Comment(2)
if you add the line Debugger.Launch(); in your windows service host project before you create the service host instance, then you can debug the startup problem of your instance and be able to see what the exception is. Some more details of the config file would also be useful.Woodpile
@Sam : Thanks for that tip. That line is going to be incredibly useful to me later on. Anyway, here's the exception message I got from that: The operation 'UploadFile' 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.Characterize
W
7

From the comments it looks like you have the problem that once you start using message contracts you must use them for all parameters, whcih means your method can't return bool it must return another message contract like say FileUploadResult.

Try changing it to return void and see if it loads and if it does change it to return a class which is attributed as a message contract instead.

The first note on this MSDN page warns about this issue, and contains a link which may provide more information.

Woodpile answered 24/5, 2011 at 6:26 Comment(1)
Changing the UploadFile() return type from 'bool' to 'void' worked.Characterize
C
0

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

Any of the scenarios listed above would generate the error.

More details: http://www.codeproject.com/Articles/199543/WCF-Service-operations-can-t-be-loaded-due-to-mixi

Conjoin answered 10/4, 2014 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.