return WsdlFromFile(source);
// try to get from the file system
}
public string WsdlFromFile(string fileFullPathName)
{
FileInfo fi = new FileInfo(fileFullPathName);
if(fi.Extension == "wsdl")
{
FileStream fs = new FileStream(fileFullPathName, FileMode.Open,
FileAccess.Read);
StreamReader sr = new StreamReader(fs);
char[] buffer = new char[(int)fs.Length];
sr.ReadBlock(buffer, 0, (int)fs.Length);
return new string(buffer);
}
throw new Exception("This is no a wsdl file");
}
// make assembly for specified wsdl text
public Assembly AssemblyFromWsdl(string strWsdl)
{
// Xml text reader
StringReader wsdlStringReader = new StringReader(strWsdl);
XmlTextReader tr = new XmlTextReader(wsdlStringReader);
ServiceDescription sd = ServiceDescription.Read(tr);
tr.Close();
// WSDL service description importer
CodeNamespace cns = new CodeNamespace("RKiss.WebServiceAccessor");
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, null, null);
sdi.ProtocolName = _protocolName;
sdi.Import(cns, null);
// source code generation
CSharpCodeProvider cscp = new CSharpCodeProvider();
ICodeGenerator icg = cscp.CreateGenerator();
StringBuilder srcStringBuilder = new StringBuilder();
StringWriter sw = new StringWriter(srcStringBuilder);
icg.GenerateCodeFromNamespace(cns, sw, null);
_srcWSProxy = srcStringBuilder.ToString();
sw.Close();
// assembly compilation.
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Xml.dll");
cp.ReferencedAssemblies.Add("System.Web.Services.dll");
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
cp.IncludeDebugInformation = false;
ICodeCompiler icc = cscp.CreateCompiler();
CompilerResults cr = icc.CompileAssemblyFromSource(cp, _srcWSProxy);
if(cr.Errors.Count > 0)
throw new Exception(string.Format("Build failed: {0} errors",
cr.Errors.Count));
return _ass = cr.CompiledAssembly;
}
// Create instance of the web service proxy
public object CreateInstance(string objTypeName)
{
Type t = _ass.GetType("RKiss.WebServiceAccessor" + "." + objTypeName);
return Activator.CreateInstance(t);
}
// invoke method on the obj
public object Invoke(object obj, string methodName, params object[] args)
{
MethodInfo mi = obj.GetType().GetMethod(methodName);
return mi.Invoke(obj, args);
}
}
WebServiceEventArgs class.
WebServiceEventArgs 类用来传递调用状态的。 这有个帮你理解概念的例子。这个类状态依赖于这个应用程序,但是只有一个字段被定义为_state。 这个字段是一个callback
Cookie;另外,用XML方式有两种方法去序列化/反序列化。 下面我使用显式和隐式操作符来操作抛出/传入字符串。
// The callback state class
[Serializable]
public class WebServiceEventArgs : EventArgs
{
private string _name;
private string _state;
private int _param;
//
public string name
{
get{ return _name; }
set{ _name = value; }
}
public string state
{
get{ return _state; }
set{ _state = value; }
}
public int param
{
get{ return _param; }
set{ _param = value; }
}
public static implicit operator string(WebServiceEventArgs obj)
{
StringBuilder xmlStringBuilder = new StringBuilder();
XmlTextWriter tw = new XmlTextWriter(new StringWriter(
xmlStringBuilder));
XmlSerializer serializer = new XmlSerializer(
typeof(WebServiceEventArgs));
serializer.Serialize(tw, obj);
tw.Close();
return xmlStringBuilder.ToString();
}
public static explicit operator WebServiceEventArgs(string xmlobj)
{
XmlSerializer serializer = new XmlSerializer(
typeof(WebServiceEventArgs));
XmlTextReader tr = new XmlTextReader(new StringReader(xmlobj));
WebServiceEventArgs ea =
serializer.Deserialize(tr) as WebServiceEventArgs;
tr.Close();
return ea;
}
}