导出 (0) 打印
全部展开

CreateMessage 方法 (MessageVersion, String, StreamWriterDelegate)

注:本页面内容可能不完全适用中国大陆地区运营的 Windows Azure服务。如要了解不同地区 Windows Azure 服务的差异, 请参考本网站.

使用指定的版本、操作和委派从流创建消息。

命名空间:  Microsoft.ServiceBus.Web
程序集:  Microsoft.ServiceBus(在 Microsoft.ServiceBus.dll 中)

public static Message CreateMessage(
	MessageVersion version,
	string action,
	StreamWriterDelegate writer
)

参数

version
类型:System.ServiceModel.Channels..::..MessageVersion
指定要用于新建消息的 Addressing 和 Envelope 版本。如果请求是使用 REST 发出的,则 Addressing 和 Envelope 版本为 None
action
类型:System..::..String
有关消息处理方式的描述。对于为响应 HTTP 请求而发送的消息,此值必须是“GETRESPONSE”。
writer
类型:Microsoft.ServiceBus.Web..::..StreamWriterDelegate
一个委派,它允许 CreateMessage()()()() 回调并要求流填充消息的正文。

返回值

类型:System.ServiceModel.Channels..::..Message
返回流创建的 Message

The following example, pulled from the Image sample in the SDK (as well as the REST Tutorial for Windows Azure Service Bus), uses StreamWriterDelegate to asynchronously stream data into a Message object.

    [ServiceBehavior(Name = "ImageService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    class ImageService : ImageContract
    {
        const string imageFileName = "image.jpg";

        Image bitmap;

        public ImageService()
        {
            this.bitmap = Image.FromFile(imageFileName);
        }

        public ImageService(string fname)
        {
            this.bitmap = Image.FromFile(fname);
        }

        public Message GetImage()
        {
            MessageVersion msgVersion = OperationContext.Current.IncomingMessageVersion;

            // Create a StreamWriterDelegate that the StreamMessageHelper
            // will call back on when it needs the data 
            // to write to the message body.
            StreamWriterDelegate swDelegate = new StreamWriterDelegate(this.WriteImage);
            Message response = StreamMessageHelper.CreateMessage(OperationContext.Current.IncomingMessageVersion, "GETRESPONSE", swDelegate);


            HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty();
            responseProperty.Headers.Add("Content-Type", "image/jpeg");
            response.Properties.Add(HttpResponseMessageProperty.Name, responseProperty);

            return response;
        }

        void WriteImage(Stream stream)
        {
            this.bitmap.Save(stream, ImageFormat.Jpeg);
        }
    }

社区附加资源

添加
显示:
© 2014 Microsoft