Soap.NET client call

We have a windows application. which is expected to connect various soap networks. Service URLs are added dynamically to the database. I tried the "Add Web Directory" pen, but the problem is that it only accepts one URL.

Can anyone suggest a different approach or source link

+3
source share
6 answers

Just set the Url property for the proxy. See How to configure your ASMX proxy server .

+3
source

Add Service Reference.

.

URL- - , .

+2

- " . -...


( . )

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Net;
using System.Security.Permissions;
using System.Web.Services.Description;
using System.Xml.Serialization;

namespace ConnectionLib
{
    internal class WsProxy
    {
        [SecurityPermission(SecurityAction.Demand, Unrestricted = true)]
        internal static object CallWebService(
            string webServiceAsmxUrl,
            string serviceName,
            string methodName,
            object[] args)
        {
            var description = ReadServiceDescription(webServiceAsmxUrl);

            var compileUnit = CreateProxyCodeDom(description);
            if (compileUnit == null)
            {
                return null;
            }

            var results = CompileProxyCode(compileUnit);

            // Finally, Invoke the web service method
            var wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);
            var mi = wsvcClass.GetType().GetMethod(methodName);
            return mi.Invoke(wsvcClass, args);
        }

        private static ServiceDescription ReadServiceDescription(string webServiceAsmxUrl)
        {
            using (var client = new WebClient())
            {
                using (var stream = client.OpenRead(webServiceAsmxUrl + "?wsdl"))
                {
                    return ServiceDescription.Read(stream);
                }
            }
        }

        private static CodeCompileUnit CreateProxyCodeDom(ServiceDescription description)
        {
            var importer = new ServiceDescriptionImporter
                           {
                               ProtocolName = "Soap12",
                               Style = ServiceDescriptionImportStyle.Client,
                               CodeGenerationOptions =
                                   CodeGenerationOptions.GenerateProperties
                           };
            importer.AddServiceDescription(description, null, null);

            // Initialize a Code-DOM tree into which we will import the service.
            var nmspace = new CodeNamespace();
            var compileUnit = new CodeCompileUnit();
            compileUnit.Namespaces.Add(nmspace);

            // Import the service into the Code-DOM tree. This creates proxy code
            // that uses the service.
            var warning = importer.Import(nmspace, compileUnit);
            return warning != 0 ? null : compileUnit;
        }

        private static CompilerResults CompileProxyCode(CodeCompileUnit compileUnit)
        {
            CompilerResults results;
            using (var provider = CodeDomProvider.CreateProvider("CSharp"))
            {
                var assemblyReferences = new[]
                                         {
                                             "System.dll",
                                             "System.Web.Services.dll",
                                             "System.Web.dll", "System.Xml.dll",
                                             "System.Data.dll"
                                         };
                var parms = new CompilerParameters(assemblyReferences);
                results = provider.CompileAssemblyFromDom(parms, compileUnit);
            }

            // Check For Errors
            if (results.Errors.Count == 0)
            {
                return results;
            }

            foreach (CompilerError oops in results.Errors)
            {
                Debug.WriteLine("========Compiler error============");
                Debug.WriteLine(oops.ErrorText);
            }

            throw new Exception(
                "Compile Error Occurred calling webservice. Check Debug output window.");
        }
    }
}
+2

- , . -, . , , , .

+1

How would you skip the username and password for the web service using this code. The web service that I mean has the following authentication class:

Public Class AuthHeader : Inherits SoapHeader
    Public SalonID As String
    Public SalonPassword As String
End Class

Then, inside this web service class, it has the following:

    Public Credentials As AuthHeader 'Part of the general declarations of the class - not within any particular method



    Private Function AuthenticateUser(ByVal ID As String, ByVal PassWord As String, ByVal theHeader As AuthHeader) As Boolean
        If (Not (ID Is Nothing) And Not (PassWord Is Nothing)) Then
            If ((ID = "1")) And (PassWord = "PWD")) Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function



    <WebMethod(Description:="Authenticat User."), SoapHeader("Credentials")> _
    Public Function AreYouAlive() As Boolean
        Dim SalonID As String = Credentials.SalonID
        Dim SalonPassword As String = Credentials.SalonPassword
        If (AuthenticateUser(ID, Password, Credentials)) Then
            Return True
        Else
            Return False
        End If
    End Function

I find that I cannot get the proxy class mentioned above to pass username and password to this

+1
source

Source: https://habr.com/ru/post/1714303/


All Articles