.NET soap客户端调用

时间:2009-08-04 13:19:58

标签: c# soap

我们有Windows应用程序。预计将连接不同的肥皂网服务。服务URL动态添加到数据库。我试过“添加Web引用”羽毛,但问题是它只接受一个URL。

任何人都可以提出不同的方法吗?或链接到来源

6 个答案:

答案 0 :(得分:3)

只需设置代理的Url属性即可。请参阅Ways to Customize your ASMX Client Proxy

答案 1 :(得分:2)

我建议改用“添加服务参考”。

但是,您仍然只能在设计时设置一个地址。

因此,您需要从数据库中读取URL并在使用时设置代理的地址。

答案 2 :(得分:2)

我在Dynamically Invoking a Web Service上的“Kirk Evans Blog”处找到了这样的代码。希望会帮助别人......


(原始代码需要一些工作。这应该是等效的)

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.");
        }
    }
}

答案 3 :(得分:1)

您必须为要连接的每个服务添加Web引用。该引用生成用于连接到该服务的代理类。因此,您想要使用的每个不同服务都需要自己的引用。

答案 4 :(得分:1)

如何使用此代码将用户名和密码传递给Web服务。我想到的Web服务具有以下身份验证类:

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

然后在它的Web服务类中,它具有以下内容:

    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

我发现我无法获得您上面提到的代理类来将用户名和密码传递给此

答案 5 :(得分:0)