Пример #1
0
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            var protoBinding = endpoint.Binding as ProtoBufBinding;

            if (protoBinding == null)
            {
                throw new ConfigurationException("The endpoint behaviour, ProtoBufBindingEndpointBehaviour, can only be applied to an endpoint which has ProtoBufBinding as its binding.");
            }

            foreach (var clientOperation in clientRuntime.ClientOperations)
            {
                var compressionBehaviour = protoBinding.GetOperationCompressionBehaviour(clientOperation.Name);

                var contractInfo = ContractInfo.FromAction(clientOperation.Action);

                var serviceContract = TypeFinder.FindServiceContract(contractInfo.ServiceContractName);

                var paramTypes = TypeFinder.GetContractParamTypes(serviceContract, contractInfo.OperationContractName,
                                                                  contractInfo.Action, false);

                var formatter = new ProtoBufClientFormatter(new List <TypeInfo>(paramTypes), contractInfo.Action,
                                                            compressionBehaviour);

                clientOperation.Formatter        = formatter;
                clientOperation.SerializeRequest = true;
                clientOperation.DeserializeReply = true;
            }
        }
        private void CheckAndMakeMetaDataRequest(Message originalMessage, TimeSpan timeout)
        {
            var action = originalMessage.Headers.Action;

            var contractInfo = ContractInfo.FromAction(action);

            var serviceContract = TypeFinder.FindServiceContract(contractInfo.ServiceContractName);

            var paramTypes = TypeFinder.GetContractParamTypes(serviceContract, contractInfo.OperationContractName,
                                                              contractInfo.Action);

            var store = ObjectBuilder.GetModelStore();

            var metaDataRequired = paramTypes.Any(paramType => store.GetModel(paramType.Type, ModeType.Wcf) == null);

            if (!metaDataRequired)
            {
                return;
            }

            var metadataRequestMessage = GetMetaDataRequestMessage(action);

            var metaDataReply = _innerChannel.Request(metadataRequestMessage);

            if (metaDataReply == null)
            {
                throw new FaultException(
                          string.Format(
                              "The service at {0} could not be contacted to get the necessary metadata, make sure the server is available.",
                              RemoteAddress.Uri.ToString()
                              ));
            }

            var headers = metaDataReply.Headers;

            var serializer    = ObjectBuilder.GetSerializer();
            var modelProvider = ObjectBuilder.GetModelProvider();

            foreach (var messageHeader in headers)
            {
                if (messageHeader.Name.StartsWith(Constants.MetaDataHeaderKeySuffix))
                {
                    var contractNamespace = messageHeader.Name.Replace(Constants.MetaDataHeaderKeySuffix, string.Empty);

                    var contractType = TypeFinder.FindDataContract(contractNamespace, contractInfo.ServiceContractName,
                                                                   contractInfo.Action);

                    var contractMetaDataString = headers.GetHeader <string>(messageHeader.Name, messageHeader.Namespace);

                    var contractMeta = BinaryConverter.FromString(contractMetaDataString);

                    var metaData = serializer.Deserialize <TypeMetaData>(contractMeta, ModeType.Wcf);

                    modelProvider.CreateModelInfo(contractType, metaData, ModeType.Wcf);
                }
            }
        }
        private void ReplyWithMetaData(RequestContext context)
        {
            var action = context.RequestMessage.Headers.Action;

            var contractInfo = ContractInfo.FromAction(action);

            var contractType = TypeFinder.FindServiceContract(contractInfo.ServiceContractName);

            var paramTypes = TypeFinder.GetContractParamTypes(contractType, contractInfo.OperationContractName, contractInfo.Action);

            var modelProvider = ObjectBuilder.GetModelProvider();

            var typeMetaDatas = new Dictionary <string, string>();
            var serializer    = ObjectBuilder.GetSerializer();

            foreach (var paramType in paramTypes)
            {
                if (typeMetaDatas.ContainsKey(paramType.Name))
                {
                    continue;
                }

                var modelInfo = modelProvider.CreateModelInfo(paramType.Type, ModeType.Wcf);

                var metaData = modelInfo.MetaData;

                var result = serializer.Serialize(metaData, ModeType.Wcf);

                var val = BinaryConverter.ToString(result.Data);

                typeMetaDatas.Add(paramType.Name, val);
            }

            var replyMessage = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, context.RequestMessage.Headers.Action);

            foreach (var typeMetaData in typeMetaDatas)
            {
                replyMessage.Headers.Add(MessageHeader.CreateHeader(Constants.MetaDataHeaderKeySuffix + typeMetaData.Key,
                                                                    Constants.DefaultCustomHeaderNamespace, typeMetaData.Value));
            }

            context.Reply(replyMessage);

            context.Close();
        }