我們有Windows應用程序。預計將連接不同的肥皂網服務。服務URL被動態添加到數據庫中。我嘗試了「添加Web引用」羽毛,但問題是它只接受一個URL。.NET soap客戶端調用
任何一個可以建議不同的方法?或者鏈接到源
我們有Windows應用程序。預計將連接不同的肥皂網服務。服務URL被動態添加到數據庫中。我嘗試了「添加Web引用」羽毛,但問題是它只接受一個URL。.NET soap客戶端調用
任何一個可以建議不同的方法?或者鏈接到源
qasanov的發現的來源: http://blogs.msdn.com/kaevans/archive/2006/04/27/585013.aspx
你有一個Web引用添加到您想要連接到每個服務。該引用生成用於連接到該服務的代理類。所以,你想要使用的每一個不同的服務都需要自己的引用。
只需設置代理的Url屬性即可。見Ways to Customize your ASMX Client Proxy。
我建議使用添加服務引用。
但是,您仍然只能在設計時設置一個地址。
因此,您需要從數據庫中讀取url,並在您使用代理時設置代理的地址。
我在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.");
}
}
}
如何使用此代碼將用戶名和密碼傳遞給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
我發現我不能得到你所提到的代理類以上將用戶名和密碼傳遞給此處
-1:請閱讀該問題。 – 2009-08-04 13:23:36
我的確讀過這個問題。他連接到多個Web服務,每個只接受一個URL。我忽略了哪一部分? – Amy 2009-08-04 13:46:32