1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
namespace System.ServiceModel.Activities
{
using System.Activities;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using SR2 = System.ServiceModel.Activities.SR;
class FromReply : CodeActivity
{
Collection<OutArgument> parameters;
public InArgument<Message> Message
{
get;
set;
}
public IClientMessageFormatter Formatter
{
get;
set;
}
public IClientFaultFormatter FaultFormatter
{
get;
set;
}
public OutArgument Result
{
get;
set;
}
public Collection<OutArgument> Parameters
{
get
{
if (this.parameters == null)
{
this.parameters = new Collection<OutArgument>();
}
return this.parameters;
}
}
protected override void CacheMetadata(CodeActivityMetadata metadata)
{
RuntimeArgument messageArgument = new RuntimeArgument(Constants.Message, Constants.MessageType, ArgumentDirection.In, true);
metadata.Bind(this.Message, messageArgument);
metadata.AddArgument(messageArgument);
if (this.Result != null)
{
RuntimeArgument resultArgument = new RuntimeArgument(Constants.Result, this.Result.ArgumentType, ArgumentDirection.Out);
metadata.Bind(this.Result, resultArgument);
metadata.AddArgument(resultArgument);
}
if (this.parameters != null)
{
int count = 0;
foreach (OutArgument parameter in this.parameters)
{
RuntimeArgument parameterArgument = new RuntimeArgument(Constants.Message + count++, parameter.ArgumentType, ArgumentDirection.Out);
metadata.Bind(parameter, parameterArgument);
metadata.AddArgument(parameterArgument);
}
}
}
protected override void Execute(CodeActivityContext context)
{
Message inMessage = this.Message.Get(context);
if (inMessage == null)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.NullReplyMessageContractMismatch));
}
if (inMessage.IsFault)
{
FaultConverter faultConverter = FaultConverter.GetDefaultFaultConverter(inMessage.Version);
Exception exception = DeserializeFault(inMessage, faultConverter);
// We simply throw the exception
throw FxTrace.Exception.AsError(exception);
}
else
{
object[] outObjects;
if (this.parameters != null)
{
outObjects = new object[this.parameters.Count];
}
else
{
outObjects = Constants.EmptyArray;
}
object returnValue = this.Formatter.DeserializeReply(inMessage, outObjects);
if (this.Result != null)
{
this.Result.Set(context, returnValue);
}
if (parameters != null)
{
for (int i = 0; i < this.parameters.Count; i++)
{
OutArgument outArgument = this.parameters[i];
Fx.Assert(outArgument != null, "Parameter cannot be null");
object obj = outObjects[i];
if (obj == null)
{
obj = ProxyOperationRuntime.GetDefaultParameterValue(outArgument.ArgumentType);
}
outArgument.Set(context, obj);
}
}
}
}
Exception DeserializeFault(Message inMessage, FaultConverter faultConverter)
{
// Reproduce logic in ClientOperationFormatterHelper
MessageFault messageFault = MessageFault.CreateFault(inMessage, TransportDefaults.MaxFaultSize);
string action = inMessage.Headers.Action;
if (action == inMessage.Version.Addressing.DefaultFaultAction)
{
action = null;
}
Exception exception = null;
if (faultConverter != null)
{
faultConverter.TryCreateException(inMessage, messageFault, out exception);
}
if (exception == null)
{
exception = this.FaultFormatter.Deserialize(messageFault, action);
}
if (inMessage.State != MessageState.Created)
{
inMessage.Close();
}
return exception;
}
}
}
|