2009-08-04 188 views
3

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

任何一个可以建议不同的方法?或者链接到源

回答

1

你有一个Web引用添加到您想要连接到每个服务。该引用生成用于连接到该服务的代理类。所以,你想要使用的每一个不同的服务都需要自己的引用。

+0

-1:请阅读该问题。 – 2009-08-04 13:23:36

+0

我的确读过这个问题。他连接到多个Web服务,每个只接受一个URL。我忽略了哪一部分? – Amy 2009-08-04 13:46:32

2

我建议使用添加服务引用。

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

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

2

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


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

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

如何使用此代码将用户名和密码传递给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 

我发现我不能得到你所提到的代理类以上将用户名和密码传递给此处