此方法只要知道SERVICE地址与要执行的方法名与参数即可调用webservice,
注意这个只是在普通工程中才可用。silverlight调用方法请往后看:
C# 代码
public static object InvokeWebService(string url, string classname, string methodname, object[] args)
{
object obj = GetWebServiceClassObject(url,classname);
Type t = obj.GetType();
System.Reflection.MethodInfo mi = t.GetMethod(methodname);
return mi.Invoke(obj, args);
}
public static object GetWebServiceClassObject(string url, string classname)
{
string @namespace = "ServiceBase.WebService.DynamicWebLoad";
if (classname == null || classname == "")
{
classname = GetClassName(url);
}
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");
ServiceDescription sd = ServiceDescription.Read(stream);
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
CodeNamespace cn = new CodeNamespace(@namespace);
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();
CompilerParameters cplist = new CompilerParameters();
cplist.GenerateExecutable = false;
cplist.GenerateInMemory = true;
cplist.ReferencedAssemblies.Add("System.dll");
cplist.ReferencedAssemblies.Add("System.XML.dll");
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
cplist.ReferencedAssemblies.Add("System.Data.dll");
CompilerResults cr = csc.CompileAssemblyFromDom(cplist, ccu);
if (true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new StringBuilder();
foreach (CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@namespace + "." + classname, true, true);
object obj = Activator.CreateInstance(t);
return obj;
}
private static string GetClassName(string url)
{
string[] parts = url.Split('/');
string[] pps = parts[parts.Length - 1].Split('.');
return pps[0];
}
下面来看一下动态调用WCF服务:
这是我在一个项目中,为了不去配置服务地址,直接解析当前访问路径,加上服务的后面部分生成与SILVERLIGHT同一域名下的服务:
这个方法的前提是要先引用这个服务,这是silverlight下用的,普通程序中也差不多
C# 代码
public DMSTypeConfigWcfService.DMSTypeConfigServiceClient DmsTypeConfigWcfService
{
get
{
if (_DmsTypeConfigWcfService == null)
{
string url = Application.Current.Host.Source.Scheme + "://" + Application.Current.Host.Source.DnsSafeHost + ":" +
Application.Current.Host.Source.Port + TypeConfigWcfUri; //这里就是获得当前URL再加上服务路径,当然你也可以配置在一个地方。
EndpointAddress address = new EndpointAddress(url);
BinaryMessageEncodingBindingElement binary = new BinaryMessageEncodingBindingElement();
HttpTransportBindingElement transport = new HttpTransportBindingElement();
CustomBinding binding = new CustomBinding(binary, transport);
_DmsTypeConfigWcfService = new DMSTypeConfigWcfService.DMSTypeConfigServiceClient(binding, address);
}
return _DmsTypeConfigWcfService;
}
}
silverlight下调用webservice与WCF类似。主要是把CustomBinding 改为Http的一个binding
代码如下:FenbiSl.WimUpFile.UpFileServiceSoapClient就是引用进来的WEBSERVICE服务对象
url 是与silverlight在同一域名下.所以我这样就可以做到更换域名或部署时不需要重新改配置...
C# 代码
string url = Application.Current.Host.Source.Scheme + "://" + Application.Current.Host.Source.DnsSafeHost + ":" +
Application.Current.Host.Source.Port + "/wim/UpFileService.asmx";
EndpointAddress address = new EndpointAddress(url);
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
UpFileClient = new FenbiSl.WimUpFile.UpFileServiceSoapClient(binding, address);