private WebServiceDescription GetServiceDescription() { var serviceDescription = new WebServiceDescription { Guid = ServiceGuid }; using (var stream = new MemoryStream(Resources.TestService)) { serviceDescription.ServiceDescriptions.Add(ServiceDescription.Read(stream)); } return serviceDescription; }
/// <summary> /// Gets the web service description from the supplied URL. /// </summary> /// <param name="url"> /// The URL where XML Web services discovery begins. /// </param> /// <param name="username"> /// The username. /// </param> /// <param name="password"> /// The password. /// </param> /// <returns> /// The <see cref="WebServiceDescription"/>. /// </returns> /// <exception cref="ArgumentNullException"> /// The <paramref name="url"/> parameter is null. /// </exception> /// <exception cref="InvalidOperationException"> /// The <paramref name="url"/> parameter is a valid URL, but does not point to a valid discovery document, service description, or XSD schema. /// </exception> public WebServiceDescription GetServiceDescription(string url, string username, string password) { if (url == null) throw new ArgumentNullException("url"); url = url.Trim(); var protocol = new DiscoveryClientProtocol { AllowAutoRedirect = true, Credentials = CredentialCache.DefaultCredentials }; if (!string.IsNullOrEmpty(username)) protocol.Credentials = new NetworkCredential(username, password); try { protocol.DiscoverAny(url); } catch (InvalidOperationException) { if (!url.EndsWith("?WSDL", StringComparison.InvariantCultureIgnoreCase)) protocol.DiscoverAny(url + "?WSDL"); else throw; } protocol.ResolveAll(); var serviceDescription = new WebServiceDescription(); foreach (DictionaryEntry entry in protocol.References) { var contractReference = entry.Value as ContractReference; if (contractReference != null) serviceDescription.ServiceDescriptions.Add(contractReference.Contract); var schemaReference = entry.Value as SchemaReference; if (schemaReference != null) serviceDescription.XmlSchemas.Add(schemaReference.Schema); } return serviceDescription; }
/// <summary> /// Loads the specified dto. /// </summary> /// <param name="dto">The dto.</param> /// <returns>WebServiceDescription.</returns> public static WebServiceDescription Load(WebServiceDescriptionDto dto) { var result = new WebServiceDescription { Id = dto.Id, Guid = dto.Guid }; result.ServiceDescriptions.Clear(); result.XmlSchemas.Clear(); foreach (var document in dto.Documents) { switch (document.DocumentType) { case WebServiceDescriptionDocumentType.ServiceDescription: result.ServiceDescriptions.Add(LoadServiceDescription(document.Data)); break; case WebServiceDescriptionDocumentType.XmlSchema: result.XmlSchemas.Add(LoadXmlSchema(document.Data)); break; } } return result; }
/// <summary> /// Generates the code. /// </summary> /// <param name="serviceDescription">The service description.</param> /// <returns>WebServiceClientCodeDto.</returns> /// <exception cref="VeyronException">Service endpoints not found.</exception> public WebServiceClientCodeDto GenerateCode(WebServiceDescription serviceDescription) { var result = new WebServiceClientCodeDto { ServiceId = serviceDescription.Id, ServiceGuid = serviceDescription.Guid }; using (var codeProvider = new CSharpCodeProvider()) { var metadataSections = new List<MetadataSection>(); metadataSections.AddRange(serviceDescription.XmlSchemas.Select(MetadataSection.CreateFromSchema)); metadataSections.AddRange(serviceDescription.ServiceDescriptions.Select(MetadataSection.CreateFromServiceDescription)); var @namespace = string.Format("sp_{0}", serviceDescription.Guid.ToString().Replace('-', '_')); var codeUnit = new CodeCompileUnit(); var importer = new WsdlImporter(new MetadataSet(metadataSections)); AddStateForXmlSerializerImport(importer, codeUnit, codeProvider, @namespace); AddStateForFaultSerializerImport(importer); RemoveDataContractSerializerExtension(importer); var endpoints = importer.ImportAllEndpoints(); if (endpoints.Count == 0) { if (importer.Errors.Any(e => !e.IsWarning)) throw new AggregateException("Service description import failed.", importer.Errors.Select(e => new Exception(e.Message))); throw new VeyronException("Service endpoints not found."); } var endpoint = endpoints[0]; var configDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); Directory.CreateDirectory(configDirectory); var configPath = Path.Combine(configDirectory, "endpoints.config"); File.WriteAllText(configPath, EmptyConfiguration, Encoding.UTF8); var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configPath }; var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); var contractGenerator = new ServiceContractGenerator(codeUnit, config); contractGenerator.NamespaceMappings.Add("*", @namespace); contractGenerator.Options = ServiceContractGenerationOptions.ClientClass | ServiceContractGenerationOptions.TypedMessages; contractGenerator.GenerateServiceContractType(endpoint.Contract); ChannelEndpointElement endpointElement; contractGenerator.GenerateServiceEndpoint(endpoint, out endpointElement); config.Save(); result.EndpointsConfiguration = File.ReadAllText(configPath); Directory.Delete(configDirectory, true); using (var stringWriter = new StringWriter()) { var options = new CodeGeneratorOptions { BracingStyle = "C" }; codeProvider.GenerateCodeFromCompileUnit(codeUnit, stringWriter, options); stringWriter.Flush(); result.Code = stringWriter.ToString(); return result; } } }