接受一个任务,使用c# 调用javaServer 接口,使用vs生成客户端代码后,发现没有header描述代码。但是接口中确有header验证,于是自定义一个header。以下是操作过程。
第一步在windows开始菜单找到vs开发人员工具
在弹出的控制台输入wsdl C:\Users\Hzf\Desktop\JavaServer.wsdl /out:C:\Users\Hzf\Desktop\0721\JavaServer.cs
C:\Users\Hzf\Desktop\JavaServer.wsdl 是你的借口文档位置,out 表示输出后面是vs自动帮我们生成的 webService客户端调用代码。(注意:生成的时候可能会有错误信息提示,基本可以忽略。。。)。
至此 客户端代码已经完成生成。
第二步添加自定义header
新建控制程序模拟调用服务客户端,将生成的JavaServer.cs代码拷贝到新建的控制台项目中
代开代码,添加自定义header,定义
CustomSoapHeader继承System.Web.Services.Protocols.SoapHeader,这里面的两个属性字段就是接口的header关键字
public class CustomSoapHeader : SoapHeader
{
public string ClientId { get; set; }
public string OperationCode { get; set; }
}
定义完成后需要在刚才生成的代码中的接口方法上增加header特性标记,并且需要在定义该方法的类中定义header属性字段。
在接口方法上定义header特性。
在接口方法中定义header属性字段
特别需要注意的是,接口方法中定义的特性SoapHeader的memberName需要与定义的CustomSoapHeader属性字段相同,否则会报Exception: 头属性/字段 comwyncaesbprovidersaplims.header 缺失或者不是公共的。
第三步 模拟调用
完成。。。
以下是完整代码,
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:4.0.30319.42000
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
//
// 此源代码由 wsdl 自动生成, Version=4.8.3928.0。
//
public class CustomSoapHeader : SoapHeader
{
public string ClientId { get; set; }
public string OperationCode { get; set; }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name = "com.wynca.esb.provider.sap.limsHttpBinding", Namespace = "http://esb.primeton.com/sap-adaptor")]
public partial class comwyncaesbprovidersaplims : System.Web.Services.Protocols.SoapHttpClientProtocol
{
public CustomSoapHeader SoapHeader { get; set; }
private System.Threading.SendOrPostCallback ZPP_LIMS_CREATE_BATCH_CHARACTOperationCompleted;
/// <remarks/>
public comwyncaesbprovidersaplims()
{
this.Url = "http://127.0.0.2:9090/com.wynca.esb.provider.sap.lims";
}
/// <remarks/>
public event ZPP_LIMS_CREATE_BATCH_CHARACTCompletedEventHandler ZPP_LIMS_CREATE_BATCH_CHARACTCompleted;
/// <remarks/>
[System.Web.Services.Protocols.SoapHeader("SoapHeader")]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace = "http://esb.primeton.com/sap-adaptor", ResponseNamespace = "http://esb.primeton.com/sap-adaptor", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlElementAttribute("RETURN", IsNullable = true)]
public RETURN ZPP_LIMS_CREATE_BATCH_CHARACT([System.Xml.Serialization.XmlElementAttribute(IsNullable = true)] ZIMPORT ZIMPORT)
{
object[] results = this.Invoke("ZPP_LIMS_CREATE_BATCH_CHARACT", new object[] {
ZIMPORT});
return ((RETURN)(results[0]));
}
[System.Web.Services.Protocols.SoapHeader("header")]
/// <remarks/>
public System.IAsyncResult BeginZPP_LIMS_CREATE_BATCH_CHARACT(ZIMPORT ZIMPORT, System.AsyncCallback callback, object asyncState)
{
return this.BeginInvoke("ZPP_LIMS_CREATE_BATCH_CHARACT", new object[] {
ZIMPORT}, callback, asyncState);
}
[System.Web.Services.Protocols.SoapHeader("header")]
/// <remarks/>
public RETURN EndZPP_LIMS_CREATE_BATCH_CHARACT(System.IAsyncResult asyncResult)
{
object[] results = this.EndInvoke(asyncResult);
return ((RETURN)(results[0]));
}
[System.Web.Services.Protocols.SoapHeader("header")]
/// <remarks/>
public void ZPP_LIMS_CREATE_BATCH_CHARACTAsync(ZIMPORT ZIMPORT)
{
this.ZPP_LIMS_CREATE_BATCH_CHARACTAsync(ZIMPORT, null);
}
[System.Web.Services.Protocols.SoapHeader("header")]
public void ZPP_LIMS_CREATE_BATCH_CHARACTAsync(ZIMPORT ZIMPORT, object userState)
{
if ((this.ZPP_LIMS_CREATE_BATCH_CHARACTOperationCompleted == null))
{
this.ZPP_LIMS_CREATE_BATCH_CHARACTOperationCompleted = new System.Threading.SendOrPostCallback(this.OnZPP_LIMS_CREATE_BATCH_CHARACTOperationCompleted);
}
this.InvokeAsync("ZPP_LIMS_CREATE_BATCH_CHARACT", new object[] {
ZIMPORT}, this.ZPP_LIMS_CREATE_BATCH_CHARACTOperationCompleted, userState);
}
private void OnZPP_LIMS_CREATE_BATCH_CHARACTOperationCompleted(object arg)
{
if ((this.ZPP_LIMS_CREATE_BATCH_CHARACTCompleted != null))
{
System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
this.ZPP_LIMS_CREATE_BATCH_CHARACTCompleted(this, new ZPP_LIMS_CREATE_BATCH_CHARACTCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
}
}
/// <remarks/>
public new void CancelAsync(object userState)
{
base.CancelAsync(userState);
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://esb.primeton.com/sap-adaptor/rfc")]
public partial class ZIMPORT
{
private ZIFS_001 zCONTROLField;
private ArrayOfZPPS_005T[] zDATAField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public ZIFS_001 ZCONTROL
{
get
{
return this.zCONTROLField;
}
set
{
this.zCONTROLField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ZDATA", IsNullable = true)]
public ArrayOfZPPS_005T[] ZDATA
{
get
{
return this.zDATAField;
}
set
{
this.zDATAField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://esb.primeton.com/sap-adaptor/rfc")]
public partial class ZIFS_001
{
private string gUIDField;
private string iFDICField;
private string iFNUMField;
private string iFOPTField;
private string oRSYSField;
private string tRSYSField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string GUID
{
get
{
return this.gUIDField;
}
set
{
this.gUIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string IFDIC
{
get
{
return this.iFDICField;
}
set
{
this.iFDICField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string IFNUM
{
get
{
return this.iFNUMField;
}
set
{
this.iFNUMField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string IFOPT
{
get
{
return this.iFOPTField;
}
set
{
this.iFOPTField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string ORSYS
{
get
{
return this.oRSYSField;
}
set
{
this.oRSYSField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string TRSYS
{
get
{
return this.tRSYSField;
}
set
{
this.tRSYSField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://esb.primeton.com/sap-adaptor/rfc")]
public partial class RETURN
{
private string gUIDField;
private string zDATAField;
private string bUSNOField;
private string iFMSGField;
private string iF_STATUField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string GUID
{
get
{
return this.gUIDField;
}
set
{
this.gUIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string ZDATA
{
get
{
return this.zDATAField;
}
set
{
this.zDATAField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BUSNO
{
get
{
return this.bUSNOField;
}
set
{
this.bUSNOField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string IFMSG
{
get
{
return this.iFMSGField;
}
set
{
this.iFMSGField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string IF_STATU
{
get
{
return this.iF_STATUField;
}
set
{
this.iF_STATUField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://esb.primeton.com/sap-adaptor/rfc")]
public partial class ZPPS_005S
{
private string tZMSField;
private string aTINNField;
private string cHARGField;
private string mATNRField;
private string wERKSField;
private string hSDATField;
private string aTBEZField;
private string bLANK1Field;
private string bLANK10Field;
private string bLANK2Field;
private string bLANK3Field;
private string bLANK4Field;
private string bLANK5Field;
private string bLANK6Field;
private string bLANK7Field;
private string bLANK8Field;
private string bLANK9Field;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string TZMS
{
get
{
return this.tZMSField;
}
set
{
this.tZMSField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string ATINN
{
get
{
return this.aTINNField;
}
set
{
this.aTINNField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string CHARG
{
get
{
return this.cHARGField;
}
set
{
this.cHARGField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string MATNR
{
get
{
return this.mATNRField;
}
set
{
this.mATNRField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string WERKS
{
get
{
return this.wERKSField;
}
set
{
this.wERKSField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string HSDAT
{
get
{
return this.hSDATField;
}
set
{
this.hSDATField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string ATBEZ
{
get
{
return this.aTBEZField;
}
set
{
this.aTBEZField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BLANK1
{
get
{
return this.bLANK1Field;
}
set
{
this.bLANK1Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BLANK10
{
get
{
return this.bLANK10Field;
}
set
{
this.bLANK10Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BLANK2
{
get
{
return this.bLANK2Field;
}
set
{
this.bLANK2Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BLANK3
{
get
{
return this.bLANK3Field;
}
set
{
this.bLANK3Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BLANK4
{
get
{
return this.bLANK4Field;
}
set
{
this.bLANK4Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BLANK5
{
get
{
return this.bLANK5Field;
}
set
{
this.bLANK5Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BLANK6
{
get
{
return this.bLANK6Field;
}
set
{
this.bLANK6Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BLANK7
{
get
{
return this.bLANK7Field;
}
set
{
this.bLANK7Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BLANK8
{
get
{
return this.bLANK8Field;
}
set
{
this.bLANK8Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string BLANK9
{
get
{
return this.bLANK9Field;
}
set
{
this.bLANK9Field = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://esb.primeton.com/sap-adaptor/rfc")]
public partial class ArrayOfZPPS_005T
{
private ZPPS_005S zPPS_005SField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public ZPPS_005S ZPPS_005S
{
get
{
return this.zPPS_005SField;
}
set
{
this.zPPS_005SField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
public delegate void ZPP_LIMS_CREATE_BATCH_CHARACTCompletedEventHandler(object sender, ZPP_LIMS_CREATE_BATCH_CHARACTCompletedEventArgs e);
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ZPP_LIMS_CREATE_BATCH_CHARACTCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
{
private object[] results;
internal ZPP_LIMS_CREATE_BATCH_CHARACTCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
base(exception, cancelled, userState)
{
this.results = results;
}
/// <remarks/>
public RETURN Result
{
get
{
this.RaiseExceptionIfNecessary();
return ((RETURN)(this.results[0]));
}
}
}