在 .NET 中使用 WEB SERVICE 的CallBacks机制 (3)

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;
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值