WCF MessageContract 格式问题 [英] WCF MessageContract formatting issue
问题描述
我对 MessageContact 的 WCF 定义和客户端调用函数有问题.
I have a problem of WCF definition of MessageContact and client calling function.
场景;客户端使用字符串类型引用 ID 调用服务,服务器响应 AudioObject 类型的实例.AudioObject 由 Stream 作为 MessageBodyMember 和 FormatObject 作为 MessageHeader 组成.
Scenario; Client called the service with a string type reference ID and server responds instance of type of AudioObject. AudioObject consists of Stream as MessageBodyMember and FormatObject as a MessageHeader.
以下是 WCF 代码片段.
Following is WCF code snippet.
[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);
上面的代码运行良好.但问题是,
The above code is worked fine. But the issue is,
为了OperationContract的格式,我期望客户端代码如下,
In order to the format of the OperationContract, I expected the client code as follows,
AudioObject audioObject = client.GetAudioDataStream("0000");
但是我的 ServiceReference 以另一种方式生成了客户端存根(如代码所示).谁能解释一下这件事的原因.
But my ServiceReference generated the client-stub other way round(as shown in code). Can anybody explain me the reason for this matter.
推荐答案
或者通过从 ClientBase 或使用您选择的方法签名编写扩展方法.
Either write a client yourself by deriving a class from ClientBase or write an extension method with the method signature of your choice.
这篇关于WCF MessageContract 格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!