private ContractDescription CreateContractDescriptionInternal(Guid iid, Type type) { ComContractElement comContract = ConfigLoader.LookupComContract(iid); if (comContract == null) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ListenerInitFailed(System.ServiceModel.SR.GetString("InterfaceNotFoundInConfig", new object[] { iid }))); } if (string.IsNullOrEmpty(comContract.Name) || string.IsNullOrEmpty(comContract.Namespace)) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ListenerInitFailed(System.ServiceModel.SR.GetString("CannotHaveNullOrEmptyNameOrNamespaceForIID", new object[] { iid }))); } ContractDescription contract = new ContractDescription(comContract.Name, comContract.Namespace) { ContractType = type, SessionMode = comContract.RequiresSession ? SessionMode.Required : SessionMode.Allowed }; bool flag = false; List <Guid> list = new List <Guid>(); foreach (ComPersistableTypeElement element2 in comContract.PersistableTypes) { Guid item = Fx.CreateGuid(element2.ID); list.Add(item); } IDataContractSurrogate contractSurrogate = null; if ((list.Count > 0) || comContract.PersistableTypes.EmitClear) { contractSurrogate = new DataContractSurrogateForPersistWrapper(list.ToArray()); } foreach (ComMethodElement element3 in comContract.ExposedMethods) { flag = false; foreach (System.Reflection.MethodInfo info in type.GetMethods()) { if (info.Name == element3.ExposedMethod) { OperationDescription operation = this.CreateOperationDescription(contract, info, comContract, null != contractSurrogate); this.ConfigureOperationDescriptionBehaviors(operation, contractSurrogate); contract.Operations.Add(operation); flag = true; break; } } if (!flag) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ListenerInitFailed(System.ServiceModel.SR.GetString("MethodGivenInConfigNotFoundOnInterface", new object[] { element3.ExposedMethod, iid }))); } } if (contract.Operations.Count == 0) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ListenerInitFailed(System.ServiceModel.SR.GetString("NoneOfTheMethodsForInterfaceFoundInConfig", new object[] { iid }))); } this.ConfigureContractDescriptionBehaviors(contract); return(contract); }
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); }
ContractDescription CreateContractDescriptionInternal(Guid iid, Type type) { ComContractElement contractConfigElement = ConfigLoader.LookupComContract(iid); if (contractConfigElement == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ListenerInitFailed(SR.GetString(SR.InterfaceNotFoundInConfig, iid))); } if (String.IsNullOrEmpty(contractConfigElement.Name) || String.IsNullOrEmpty(contractConfigElement.Namespace)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ListenerInitFailed(SR.GetString(SR.CannotHaveNullOrEmptyNameOrNamespaceForIID, iid))); } ContractDescription contract = new ContractDescription(contractConfigElement.Name, contractConfigElement.Namespace); contract.ContractType = type; contract.SessionMode = contractConfigElement.RequiresSession ? SessionMode.Required : SessionMode.Allowed; bool methodFound = false; List <Guid> guidList = new List <Guid>(); foreach (ComPersistableTypeElement typeElement in contractConfigElement.PersistableTypes) { Guid typeGuid = Fx.CreateGuid(typeElement.ID); guidList.Add(typeGuid); } IDataContractSurrogate contractSurrogate = null; // We create a surrogate when the persistable types config section is there // even if we have no types that we allow. // That way we have control over the error when the client tries to make a call // persistable type. if (guidList.Count > 0 || contractConfigElement.PersistableTypes.EmitClear) { contractSurrogate = new DataContractSurrogateForPersistWrapper(guidList.ToArray()); } foreach (ComMethodElement configMethod in contractConfigElement.ExposedMethods) { methodFound = false; foreach (MethodInfo method in type.GetMethods()) { if (method.Name == configMethod.ExposedMethod) { OperationDescription operation = CreateOperationDescription(contract, method, contractConfigElement, (null != contractSurrogate)); ConfigureOperationDescriptionBehaviors(operation, contractSurrogate); contract.Operations.Add(operation); methodFound = true; break; } } if (!methodFound) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ListenerInitFailed(SR.GetString(SR.MethodGivenInConfigNotFoundOnInterface, configMethod.ExposedMethod, iid))); } } if (contract.Operations.Count == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ListenerInitFailed(SR.GetString(SR.NoneOfTheMethodsForInterfaceFoundInConfig, iid))); } ConfigureContractDescriptionBehaviors(contract); return(contract); }
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); }
// // 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); }