WCF MessageContract格式问题

时间:2011-09-26 01:51:18

标签: wcf

我遇到了MessageContact和客户端调用函数的WCF定义问题。

方案;客户端使用字符串类型引用ID调用服务,服务器响应AudioObject类型的实例。 AudioObject由Stream作为MessageBodyMember,FormatObject作为MessageHeader。

以下是WCF代码段。

[ServiceContract]
public interface IAudioStreamService
{
    [OperationContract]
    AudioObject GetAudioDataStream(StringMessage RefID);
}

//StringMessageContract
[MessageContract]
public class StringMessage
{
    [MessageBodyMember]
    public string Name;
}

[MessageContract]
public class AudioObject
{
    Stream _audioStream;
    AudioFormat _audioFormat;

    [MessageBodyMember (Order=1)]
    public Stream AudioStream
    {
        get { return _audioStream; }
        set { _audioStream = value; }
    }

    [MessageHeader(MustUnderstand=true)]
    public AudioFormat AudioFormat
    {
        get { return _audioFormat; }
        set { _audioFormat = value; }
    }
}

[DataContract]
public class AudioFormat
{
    int _nChannels;
    int _nKilloBitsPerSec;
    int _nSamplesPerSec;

    [DataMember(Name="nChannels", Order=0, IsRequired=true)]
    public int nChannels
    {
        get { return _nChannels; }
        set { _nChannels = value; }
    }
    [DataMember(Name = "nKilloBitsPerSec", Order = 1, IsRequired = true)]
    public int nKilloBitsPerSec
    {
        get { return _nKilloBitsPerSec; }
        set { _nKilloBitsPerSec = value; }
    }
    [DataMember(Name = "nSamplesPerSec", Order = 2, IsRequired = true)]
    public int nSamplesPerSec
    {
        get { return _nSamplesPerSec; }
        set { _nSamplesPerSec = value; }
    }
}

客户端代码如下,

BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = 176160768;                       
EndpointAddress endpointAddress = new EndpointAddress("URLToService");
AudioStreamServiceClient client = new AudioStreamServiceClient(binding, 
endpointAddress);
    AudioFormat audioFormat = client.GetAudioDataStream("000", out serverStream); 

上面的代码工作正常。但问题是,

为了 OperationContract 的格式,我期望客户端代码如下,

AudioObject audioObject = client.GetAudioDataStream("0000");

但我的ServiceReference以其他方式生成了客户端 - 存根(如代码所示)。谁能解释一下这件事的原因。

1 个答案:

答案 0 :(得分:1)

通过从ClientBase派生类来编写客户端,或者使用您选择的方法签名编写扩展方法。