发送自定义WCF信息给服务 [英] Sending custom WCF Message to a service

查看:140
本文介绍了发送自定义WCF信息给服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是要记录WCF一个IIS托管WCF服务调用,并将其重播到不同的WCF服务。到目前为止,我有一个 IDispatchMessageInspector 工作下面这个例子,它可以记录一个传入请求和到磁盘上对应的应答。

My goal is to record wcf calls to one IIS hosted wcf service and replay them to a different wcf service. So far I have an IDispatchMessageInspector working following this example, it can log an incoming request and the corresponding reply to disk.

我怎样才能从磁盘中读取信息,然后将其发送给其他服务?是否有客户端的方式来发送一个低的水平Message对象的服务,而无需通过正常的客户端代理对象回事?

How can I read in a message from disk and then send it to the other service? Is there a way for the client to send a low level Message object to the service without going through the normal client proxy object?

推荐答案

我能得到它,只需创建一个IRequestChannel,阅读下面的有助于解释它是如何工作的工作

I was able to get it to work by simply creating an IRequestChannel, reading the following helped explain how it works

  • Using the Message Class
  • WCF Data Transfer Architecture
  • WCF Messaging Fundamentals

要发送消息的代码:

private static void TestDispatchingMessage()
{
    var reader = XmlDictionaryReader.CreateBinaryReader(new FileStream(@"path\request_6c6fc02f-45a7-4049-9bab-d6f2fff5cb2d.xml", FileMode.Open), XmlDictionaryReaderQuotas.Max);
    var message = Message.CreateMessage(reader, int.MaxValue, MessageVersion.Soap11);
    message.Headers.To = new System.Uri(@"url");


    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None)
    {
        MessageEncoding = WSMessageEncoding.Mtom,
        MaxReceivedMessageSize = int.MaxValue,
        SendTimeout = new TimeSpan(1, 0, 0),
        ReaderQuotas = { MaxStringContentLength = int.MaxValue, MaxArrayLength = int.MaxValue, MaxDepth = int.MaxValue }
    };

    var cf = new ChannelFactory<IRequestChannel>(binding, new EndpointAddress(@"url"));

    foreach (OperationDescription op in cf.Endpoint.Contract.Operations)
    {
        op.Behaviors.Remove<DataContractSerializerOperationBehavior>();
        op.Behaviors.Add(new ProtoBehaviorAttribute());
    }

    cf.Open();
    var channel = cf.CreateChannel();
    channel.Open();

    var result = channel.Request(message);

    Console.WriteLine(result);

    channel.Close();
    cf.Close();
}

这是什么在 IDispatchMessageInspector 类:

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
    var callId = Guid.NewGuid();

    var action = request.Headers.Action.Substring(request.Headers.Action.LastIndexOf('/'));
    var fileName = string.Format(@"path\{0}_{1}.data", action, callId);

    try
    {
        var buffer = request.CreateBufferedCopy(int.MaxValue);

        var writeRequest = buffer.CreateMessage();
        using (var stream = new FileStream(fileName, FileMode.CreateNew))
        {
            using (var writer = XmlDictionaryWriter.CreateBinaryWriter(stream))
            {
                writeRequest.WriteMessage(writer);
                writer.Flush();
            }
        }

        request = buffer.CreateMessage();
        buffer.Close();
    }
    catch (Exception ex)
    {
        Log.ErrorException("Error writing", ex);
    }

    Log.Info("Call {0}", callId);

    return callId;
}

这篇关于发送自定义WCF信息给服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆