private OperationDescription CreateOperationDescription(ContractDescription contract, System.Reflection.MethodInfo methodInfo, ComContractElement config, bool allowReferences)
 {
     System.ServiceModel.Description.XmlName methodName = new System.ServiceModel.Description.XmlName(ServiceReflector.GetLogicalName(methodInfo));
     System.ServiceModel.Description.XmlName returnValueName = TypeLoader.GetReturnValueName(methodName);
     if (ServiceReflector.IsBegin(methodInfo))
     {
         throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.NoAsyncOperationsAllowed());
     }
     if (contract.Operations.FindAll(methodName.EncodedName).Count != 0)
     {
         throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.DuplicateOperation());
     }
     OperationDescription description = new OperationDescription(methodName.EncodedName, contract) {
         SyncMethod = methodInfo,
         IsInitiating = true,
         IsTerminating = false
     };
     description.KnownTypes.Add(typeof(Array));
     description.KnownTypes.Add(typeof(DBNull));
     description.KnownTypes.Add(typeof(CurrencyWrapper));
     description.KnownTypes.Add(typeof(ErrorWrapper));
     if (allowReferences)
     {
         description.KnownTypes.Add(typeof(PersistStreamTypeWrapper));
     }
     foreach (ComUdtElement element in config.UserDefinedTypes)
     {
         Type type;
         Guid typeLibId = Fx.CreateGuid(element.TypeLibID);
         TypeCacheManager.Provider.FindOrCreateType(typeLibId, element.TypeLibVersion, Fx.CreateGuid(element.TypeDefID), out type, false);
         this.info.AddUdt(type, typeLibId);
         description.KnownTypes.Add(type);
     }
     string ns = contract.Namespace;
     XmlQualifiedName contractName = new XmlQualifiedName(contract.Name, ns);
     string action = NamingHelper.GetMessageAction(contractName, methodName.DecodedName, null, false);
     string str3 = NamingHelper.GetMessageAction(contractName, methodName.DecodedName, null, true);
     MessageDescription item = this.CreateIncomingMessageDescription(contract, methodInfo, ns, action, allowReferences);
     MessageDescription description3 = this.CreateOutgoingMessageDescription(contract, methodInfo, returnValueName, ns, str3, allowReferences);
     description.Messages.Add(item);
     description.Messages.Add(description3);
     return description;
 }
Пример #2
0
        protected bool AddComContractToConfig(Configuration config, string name, string contractType, IList<string> methods)
        {
            Guid contractIID = new Guid(contractType);
            ServiceModelSectionGroup sg = ServiceModelSectionGroup.GetSectionGroup(config);
            ComContractElementCollection contractCollection = sg.ComContracts.ComContracts;
            foreach (ComContractElement comContract in contractCollection)
            {
                try
                {
                    Guid contractFound = new Guid(comContract.Contract);
                    if (contractIID == contractFound)
                    {
                        bool methodsAdded = false;
                        bool found = false;
                        foreach (string methodName in methods)
                        {
                            found = false;
                            foreach (ComMethodElement methodElement in comContract.ExposedMethods)
                            {
                                if (methodElement.ExposedMethod == methodName)
                                    found = true;
                            }
                            if (!found)
                            {
                                comContract.ExposedMethods.Add(new ComMethodElement(methodName));
                                methodsAdded = true;
                            }
                        }

                        if (comContract.PersistableTypes.Count == 0 && Tool.Options.AllowReferences && methodsAdded)
                        {
                            comContract.PersistableTypes.EmitClear = true;
                        }

                        return methodsAdded;
                    }
                }
                catch (FormatException)
                {

                }
            }
            // The contract does not exists 
            // so we are going to add it
            ComContractElement newComContract = new ComContractElement(contractIID.ToString("B").ToUpperInvariant());
            newComContract.Name = name;
            newComContract.Namespace = EndpointConfig.TempURI + contractIID.ToString().ToUpperInvariant();
            foreach (string methodName in methods)
                newComContract.ExposedMethods.Add(new ComMethodElement(methodName));

            if (newComContract.PersistableTypes.Count == 0 && Tool.Options.AllowReferences)
            {
                newComContract.PersistableTypes.EmitClear = true;
            }

            newComContract.RequiresSession = true;

            contractCollection.Add(newComContract);

            return true;

        }
        //
        // Note - the code below this line a paraphrase of the SM reflection code in TypeLoader.cs
        // Ideally we would be re-using their code, but our assumptions are too disjoint
        // for that to be realistic at the time of writing (12/2004).
        //

        OperationDescription CreateOperationDescription(ContractDescription contract, MethodInfo methodInfo, ComContractElement config, bool allowReferences)
        {
            XmlName operationName = new XmlName(ServiceReflector.GetLogicalName(methodInfo));
            XmlName returnValueName = TypeLoader.GetReturnValueName(operationName);

            if (ServiceReflector.IsBegin(methodInfo) || ServiceReflector.IsTask(methodInfo))
            {
                Fx.Assert("No async operations allowed");

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.NoAsyncOperationsAllowed());
            }
            if (contract.Operations.FindAll(operationName.EncodedName).Count != 0)
            {
                Fx.Assert("Duplicate operation name");

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.DuplicateOperation());
            }

            OperationDescription operationDescription = new OperationDescription(operationName.EncodedName, contract);
            operationDescription.SyncMethod = methodInfo;
            operationDescription.IsInitiating = true;
            operationDescription.IsTerminating = false;

            operationDescription.KnownTypes.Add(typeof(Array));
            operationDescription.KnownTypes.Add(typeof(DBNull));
            operationDescription.KnownTypes.Add(typeof(CurrencyWrapper));
            operationDescription.KnownTypes.Add(typeof(ErrorWrapper));

            if (allowReferences)
                operationDescription.KnownTypes.Add(typeof(PersistStreamTypeWrapper));

            foreach (ComUdtElement udt in config.UserDefinedTypes)
            {
                Type knownType;

                Guid typeLibID = Fx.CreateGuid(udt.TypeLibID);

                TypeCacheManager.Provider.FindOrCreateType(typeLibID, udt.TypeLibVersion, Fx.CreateGuid(udt.TypeDefID), out knownType, false);

                this.info.AddUdt(knownType, typeLibID);
                operationDescription.KnownTypes.Add(knownType);
            }


            string ns = contract.Namespace;
            XmlQualifiedName contractQName = new XmlQualifiedName(contract.Name, ns);

            string requestAction = NamingHelper.GetMessageAction(contractQName,
                                                                 operationName.DecodedName,
                                                                 null,
                                                                 false);

            string responseAction = NamingHelper.GetMessageAction(contractQName,
                                                                  operationName.DecodedName,
                                                                  null,
                                                                  true);

            MessageDescription inMessage = CreateIncomingMessageDescription(contract,
                                                                            methodInfo,
                                                                            ns,
                                                                            requestAction,
                                                                            allowReferences);

            MessageDescription outMessage = CreateOutgoingMessageDescription(contract,
                                                                             methodInfo,
                                                                             returnValueName,
                                                                             ns,
                                                                             responseAction,
                                                                             allowReferences);

            operationDescription.Messages.Add(inMessage);
            operationDescription.Messages.Add(outMessage);

            return operationDescription;
        }