private WsdlService CreateService(ServiceDescription wsdl, Service service)
        {
            var s = new WsdlService();

            s.XName = new XmlQualifiedName(service.Name, service.ServiceDescription.TargetNamespace);
            foreach (Port p in service.Ports)
            {
                var wsp = new WsdlServicePort();
                wsp.Binding = _bindingLookup[p.Binding.Name];
                foreach (ServiceDescriptionFormatExtension extension in p.Extensions)
                {
                    if (extension is SoapAddressBinding)
                    {
                        wsp.SoapLocation = (extension as SoapAddressBinding).Location;
                    }
                }
                s.Ports.Add(wsp);
            }
            return(s);
        }
Beispiel #2
0
        private void CodeService(WsdlService s, string directory, string namespaceName)
        {
            using (var file = new CodeFile(directory, $"{s.CodeName}.cs"))
            {
                file.WriteLine("using Microsoft.Extensions.Logging;");
                file.WriteLine("using System.Collections.Generic;");
                file.WriteLine("using System.Threading.Tasks;");
                file.WriteLine("");
                using (var nsScope = file.CreateNamespaceScope(namespaceName))
                {
                    using (var classScope = file.CreateClassScope("public", s.CodeName, s.Ports.Select(x => x.Binding.PortType.CodeName).ToArray()))
                    {
                        file.WriteLine("private readonly AdWordsApiConfig _config;");
                        file.WriteLine("private readonly INetUtility _netUtil;");
                        file.WriteLine("private readonly ILogger _logger;");

                        // constructor
                        file.WriteLine($"public {s.CodeName}(AdWordsApiConfig config, INetUtility netUtil, ILoggerFactory loggerFactory)");
                        file.Ocb();
                        file.WriteLine("_config = config;");
                        file.WriteLine("_netUtil = netUtil;");
                        file.WriteLine($"_logger = loggerFactory?.CreateLogger<{s.CodeName}>();");
                        file.Ccb();

                        foreach (var p in s.Ports)
                        {
                            var headerElements = new List <WsdlType>();
                            foreach (var opBinding in p.Binding.Operations)
                            {
                                var pt = p.Binding.PortType;
                                var op = pt.Operations.First(x => x.Name == opBinding.Name);

                                var    outputType     = GetPropertyTypeCode(op.OutputElement.Properties[0], true);
                                string inputs         = null;
                                string inputParamName = null;
                                if (op.InputElement.Properties.Count > 0)
                                {
                                    var prop = op.InputElement.Properties[0];
                                    inputParamName = EscapeKeyword(StringUtil.ToCamelCaseName(prop.CodeName));
                                    inputs         = GetPropertyTypeCode(prop, true) + " " + inputParamName;
                                }
                                file.Comment(op.Documentation);
                                file.WriteLine($"public async Task<{outputType}> {op.CodeName}Async({inputs})");
                                file.Ocb();
                                file.WriteLine($"var binding = new {p.Binding.CodeName}(\"{p.SoapLocation}\", _config.AccessToken, _config.Timeout, _config.EnableGzipCompression, _netUtil, _logger);");
                                file.WriteLine($"var inData = new SoapData<{opBinding.InputSoapHeader.CodeName}, {op.InputElement.CodeName}>();");
                                file.WriteLine($"inData.Header = new {opBinding.InputSoapHeader.CodeName}();");
                                file.WriteLine("AssignHeaderValues(inData.Header);");
                                if (!headerElements.Contains(opBinding.InputSoapHeader))
                                {
                                    headerElements.Add(opBinding.InputSoapHeader);
                                }
                                file.WriteLine($"inData.Body = new {op.InputElement.CodeName}();");
                                if (inputParamName != null)
                                {
                                    var prop = op.InputElement.Properties[0];
                                    if (prop.AllowMultiple)
                                    {
                                        file.WriteLine($"inData.Body.{prop.CodeName} = new {GetPropertyTypeCode(prop, false)}({inputParamName});");
                                    }
                                    else
                                    {
                                        file.WriteLine($"inData.Body.{prop.CodeName} = {inputParamName};");
                                    }
                                }
                                file.WriteLine($"var outData = await binding.{op.CodeName}Async(inData).ConfigureAwait(false);");
                                file.WriteLine($"return outData.Body.{op.OutputElement.Properties[0].CodeName};");
                                file.Ccb();
                            }
                            foreach (var hEle in headerElements)
                            {
                                file.WriteLine($"private void AssignHeaderValues({hEle.CodeName} header)");
                                file.Ocb();
                                file.WriteLine("header.ClientCustomerId = _config.ClientCustomerId;");
                                file.WriteLine("header.DeveloperToken = _config.DeveloperToken;");
                                file.WriteLine("header.PartialFailure = _config.PartialFailure;");
                                file.WriteLine("header.UserAgent = _config.UserAgent;");
                                file.WriteLine("header.ValidateOnly = _config.ValidateOnly;");
                                file.Ccb();
                            }
                        }
                    }
                }
            }
        }