【问题标题】:Use of MessageContract crashes WCF service on startup使用 MessageContract 在启动时使 WCF 服务崩溃
【发布时间】:2011-05-24 05:58:17
【问题描述】:

我正在尝试将 MessageContract 添加到我的 WCF 服务中,类似于这个问题中发生的事情:WCF: using streaming with Message Contracts

这是我得到的例外: 无法加载操作“UploadFile”,因为它具有 System.ServiceModel.Channels.Message 类型的参数或返回类型,或者具有 MessageContractAttribute 和其他不同类型参数的类型。使用 System.ServiceModel.Channels.Message 或带有 MessageContractAttribute 的类型时,该方法不得使用任何其他类型的参数。

这是我的合同:

[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; }
}

这是我在 app.config 中使用的绑定配置:

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

现在我在想这可能与我使用的绑定类型有关,但我不完全确定。

【问题讨论】:

  • 如果你在创建服务宿主实例之前在你的windows服务宿主项目中添加Debugger.Launch();这行,那么你可以调试你的实例的启动问题,并且能够看到异常是什么。配置文件的更多细节也会很有用。
  • @Sam:感谢您的提示。这条线以后对我来说将非常有用。无论如何,这是我从中得到的异常消息:无法加载操作“UploadFile”,因为它具有 System.ServiceModel.Channels.Message 类型的参数或返回类型或具有 MessageContractAttribute 和其他参数的类型不同种类。使用 System.ServiceModel.Channels.Message 或带有 MessageContractAttribute 的类型时,该方法不得使用任何其他类型的参数。

标签: c# wcf messagecontract wcf-streaming


【解决方案1】:

从 cmets 看来,您遇到的问题是,一旦开始使用消息协定,您必须将它们用于所有参数,这意味着您的方法无法返回 bool,它必须返回另一个消息协定,例如 FileUploadResult。

尝试将其更改为返回 void 并查看它是否已加载,是否确实将其更改为返回一个属性为消息协定的类。

this MSDN page 上的第一个注释警告此问题,并包含一个可能提供更多信息的链接。

【讨论】:

  • 将 UploadFile() 返回类型从“bool”更改为“void”有效。
【解决方案2】:

这基本上意味着特定操作正在使用以下任意组合中的消息协定类型和原始类型的组合:

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

上面列出的任何情况都会产生错误。

更多详情:http://www.codeproject.com/Articles/199543/WCF-Service-operations-can-t-be-loaded-due-to-mixi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多