C# 调用Webservice并传递序列化对象
C#动态调用WebService注意要点
1.动态调用的url后面注意一定要加上?WSDL
例如:string _url = "http://服务器IP:端口/CITI_TRANS_WH/wsTransData_InWH.asmx?WSDL";
---------------------------------------------------------------------------------------------------
2.WebService中传递List泛型对象
[WebMethod]
public List<TestModel> TestTransDataByClass(int _max)
注意TestModel必须是可以序列化的类
//必须可序列化
[Serializable]
public class TestModel
{
public int No
{
get;
set;
}
public string Des
{
get;
set;
}
}
---------------------------------------------------------------------------------------------------
3.WebService中不能直接传递输出dictionary<string,object>这样的泛型对象,必须自定义一个类来输出,这个类同样也是可以序列化的
[Serializable]
public class MyDictionary
{
public List<TestModel> Table1
{
get;
set;
}
public List<TestModel2> Table2
{
get;
set;
}
}
---------------------------------------------------------------------------------------------------
4.动态调用WebService的类封装
public static class InvokeWebServiceDynamic
{
/// <summary>
/// 动态调用WebService
/// </summary>
/// <param name="_url">web服务url地址</param>
/// <param name="_methodName">web服务中的方法</param>
/// <param name="_params">传递给方法的参数</param>
/// <returns></returns>
public static object InvokeWebMethod(string _url ,string _methodName,
params object[] _params)
{
WebClient client = new WebClient();
//String url = "http://localhost:3182/Service1.asmx?WSDL";//这个地址可以写在Config文件里面
Stream stream = client.OpenRead(_url);
ServiceDescription description = ServiceDescription.Read(stream);
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();//创建客户端代理代理类。
importer.ProtocolName = "Soap"; //指定访问协议。
importer.Style = ServiceDescriptionImportStyle.Client; //生成客户端代理。
importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties |
CodeGenerationOptions.GenerateNewAsync;
importer.AddServiceDescription(description, null, null); //添加WSDL文档。
CodeNamespace nmspace = new CodeNamespace(); //命名空间
nmspace.Name = "TestWebService"; //这个命名空间可以自己取
CodeCompileUnit unit = new CodeCompileUnit();
unit.Namespaces.Add(nmspace);
ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters parameter = new CompilerParameters();
parameter.GenerateExecutable = false;
parameter.OutputAssembly = "MyTest.dll";//输出程序集的名称
parameter.ReferencedAssemblies.Add("System.dll");
parameter.ReferencedAssemblies.Add("System.XML.dll");
parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
parameter.ReferencedAssemblies.Add("System.Data.dll");
CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
if (result.Errors.HasErrors)
{
// 显示编译错误信息
}
Assembly asm = Assembly.LoadFrom("MyTest.dll");//加载前面生成的程序集
Type t = asm.GetType("TestWebService.wsTransData_InWH"); //前面的命名空间.类名,类必须是webservice中定义的
object o = Activator.CreateInstance(t);
MethodInfo method = t.GetMethod(_methodName);//GetPersons是服务端的方法名称,你想调用服务端的什么方法都可以在这里改,最好封装一下
object item = method.Invoke(o, _params); //注:method.Invoke(o, null)返回的是一个Object,如果你服务端返回的是DataSet,这里也是用(DataSet)method.Invoke(o, null)转一下就行了
//foreach (string str in item)
// Console.WriteLine(str); //上面是根据WebService地址,模似生成一个代理类,如果你想看看生成的代码文件是什么样子,可以用以下代码保存下来,默认是保存在bin目录下面
//TextWriter writer = File.CreateText("MyTest.cs");
//provider.GenerateCodeFromCompileUnit(unit, writer, null);
//writer.Flush();
//writer.Close();
return item;
}
}
---------------------------------------------------------------------------------------------------
5.通过反射提取web方法返回的自定义类数据
说明:
<1>.WebService方法TestTransDataByDic返回自定义的MyDictionary对象
<2>.它包含两个属性table1,table2
<3>.类定义代码如下
[Serializable]
public class MyDictionary
{
public List<TestModel> Table1
{
get;
set;
}
public List<TestModel2> Table2
{
get;
set;
}
}
<4>.客户端调用代码
private void InvokeDic_Click(object sender, EventArgs e)
{
//要注意加?WSDL
//string _url = "http://localhost:58764/wsTransData_InWH.asmx?WSDL";
int _count = int.Parse(txtCount.Text);
object o = InvokeWebServiceDynamic.InvokeWebMethod(_url, "TestTransDataByDic",
new object[] { _count });
PropertyInfo _propertyTable1 = o.GetType().GetProperty("Table1");
PropertyInfo _propertyTable2 = o.GetType().GetProperty("Table2");
//读取Table1属性中的数据
object[] _table1Items = (object[])_propertyTable1.GetValue(o, null);
if(_table1Items.Length>0)
{
lstData1.Visible = false;
lstData1.Items.Clear();
//反射出对象TestModel的属性
PropertyInfo _propertyNo = _table1Items[0].GetType().GetProperty("No");
PropertyInfo _propertyDes = _table1Items[0].GetType().GetProperty("Des");
for (int i = 0; i < _table1Items.Length; i++)
{
//根据属性取值
string _no = _propertyNo.GetValue(_table1Items[i], null).ToString();
string _des = _propertyDes.GetValue(_table1Items[i], null).ToString();
string _format = string.Format("第{0}条:{1}", _no, _des);
lstData1.Items.Add(_format);
}
lstData1.Visible = true;
}
//读取Table2属性中的数据
object[] _table2Items = (object[])_propertyTable2.GetValue(o, null);
if (_table2Items.Length > 0)
{
lstData2.Visible = false;
lstData2.Items.Clear();
//反射出对象TestModel2的属性
PropertyInfo _propertyMyFNo = _table2Items[0].GetType().GetProperty("MyFNo");
PropertyInfo _propertyMyFDes = _table2Items[0].GetType().GetProperty("MyFDes");
for (int i = 0; i < _table1Items.Length; i++)
{
//根据属性取值
string _no = _propertyMyFNo.GetValue(_table2Items[i], null).ToString();
string _des = _propertyMyFDes.GetValue(_table2Items[i], null).ToString();
string _format = string.Format("第{0}条:{1}", _no, _des);
lstData2.Items.Add(_format);
}
lstData2.Visible = true;
}
MessageBox.Show("OK");
}
---------------------------------------------------------------------------------------------------
6.客户端传递序列化对象给webserice方法
/// <summary>
///
/// </summary>
/// <param name="_dicGet">是一个客户端传过来的序列化的对象</param>
/// <returns></returns>
[WebMethod]
public string TestInsertData(byte[] _dicGet)
{
//反序列化对象
object _dicGetOK = SqlHelper.DeserializeObject(_dicGet);
return "ok";
}
注意:
<1>.创建一个.NET类库,把要传输的对象做成一个结构或类放在类库(假设为ClassLib.dll)中。如:
<2>.然后在客户端程序和webservice项目中都引用ClassLib.dll
<3>.上面两步的目的是让客户端序列化的对象,在webservice服务端能正常反序列化,不会出现反序列化时找不到命名空间的问题
---------------------------------------------------------------------------------------------------
7.修改webserivce最大传输的长度
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="ConStr" connectionString="$(ReplacableToken_ConStr-Web.config Connection String_0)"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="2147483647" />
</system.web>
</configuration>
8.修改webservice的超时时间
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
executionTimeout="120" 超时时间120秒
maxRequestLength="2147483647" 最大请求长度
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="ConStr" connectionString="Data Source=192.128.58.248;Initial catalog=Citibank_test;Uid=sa;pwd=kicpassword"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="2147483647" executionTimeout="240" />
</system.web>
</configuration>
9.序列化,反序列化方法
public static byte[] SerializeObject(object pObj)
{
if (pObj == null)
return null;
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, pObj);
memoryStream.Position = 0;
byte[] read = new byte[memoryStream.Length];
memoryStream.Read(read, 0, read.Length);
memoryStream.Close();
return read;
}
public static object DeserializeObject(byte[] pBytes)
{
object newOjb = null;
if (pBytes == null)
{
return newOjb;
}
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(pBytes);
memoryStream.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
newOjb = formatter.Deserialize(memoryStream);
memoryStream.Close();
return newOjb;
}
C# 调用Webservice并传递序列化对象的更多相关文章
- Android剪切板传递数据传递序列化对象数据
一.剪切板的使用介绍 1. 剪切板对象的创建 使用剪切板会用到,ClipboardManager对象,这个对像的创建不可以使用构造方法,主要是由于没有提供public的构造函数(单例模式),需要使用A ...
- CXF WebService中传递复杂对象(List、Map、Array)
转自:https://wenku.baidu.com/view/047ce58ed0d233d4b14e69eb.html 现在开始介绍传递复杂类型的对象.如JavaBean.Array.List.M ...
- jQuery调用WCF服务传递JSON对象
下面这个示例使用了WCF去创建一个服务端口从而能够被ASP.Net页面通过jQuery的AJAX方法访问,我们将在客户端使用Ajax技术来 与WCF服务进行通信.这里我们仅使用jQuery去连接Web ...
- Webservice SOAP传输序列化总结 以及webservice之序列化以及反序列化实例
一.所有Webservice中传递的对象都必须能够序列化,这个是作为在网络之间传输的必要条件.XML WebService和SOAP标准支持的数据类型如下: 1.基本数据类型. 标准类型,如:int ...
- C# 调用WebService的方法
很少用C#动态的去调用Web Service,一般都是通过添加引用的方式,这样的话是自动成了代理,那么动态代理调用就是我们通过代码去调用这个WSDL,然后自己去生成客户端代理.更多的内容可以看下面的两 ...
- 【转】C# 调用WebService的方法
很少用C#动态的去调用Web Service,一般都是通过添加引用的方式,这样的话是自动成了代理,那么动态代理调用就是我们通过代码去调用这个WSDL,然后自己去生成客户端代理.更多的内容可以看下面的两 ...
- Android通过ksoap2这个框架调用webservice大讲堂
昨天有人问我Android怎么连接mysql数据库,和对数据库的操作呀,我想把,给他说说json通信,可是他并不知道怎么弄,哎算了吧,直接叫他用ksoap吧,给他说了大半天,好多零碎的知识,看来还是有 ...
- Jquery调用Webservice传递Json数组
Jquery由于提供的$.ajax强大方法,使得其调用webservice实现异步变得简单起来,可以在页面上传递Json字符串到Webservice中,Webservice方法进行业务处理后,返回Js ...
- cxf 调用 webservice服务时传递 服务器验证需要的用户名密码
cxf通过wsdl2java生成客户端调用webservice时,如果服务器端需要通过用户名和密码验证,则客户端必须传递验证所必须的用户名和密码,刚开始想通过url传递用户名和密码,于是在wsdl文件 ...
随机推荐
- EasyUI DataGrid和Pagination
连接一台EasyUI项目驱动学习 DataGrid数据表格及Pagination分页一起介绍 一.通过<table>标记创建DataGrid,嵌套<th>标签定义列表 < ...
- 高速压缩跟踪(fast compressive tracking)(CT)算法分析
本文为原创,转载请注明出处:http://blog.csdn.net/autocyz/article/details/44490009 Fast Compressive Tracking (高速压缩跟 ...
- UVa10986_Sending email(最短)(白皮书图论的话题)
解决报告 思路: 裸裸的最短路. #include <iostream> #include <cstring> #include <cstdio> #include ...
- KindEditor参数具体解释
width 编辑器的宽度.能够设置px或%.比textarea输入框样式表宽度优先度高. 数据类型: String 默认值: textarea输入框的宽度 演示样例: K.create('#id', ...
- 基于android 社会的app短信分享 发送回调事件的实现
摘要 前一段时间.由于项目的需要,采用ShareSDK该共享功能.其中包含 短信股吧.和呼叫系统,以分享要与成功处理服务器交互的消息后,(我不在乎在这里,收到.仅仅关心发出去了).可是ShareSDk ...
- [HA]负载均衡:HAPROXY与KEEPALIVED强强联合
第一步:更改系统控制配置文件,同意分配虚拟IP(VIP) /etc/sysctl.conf net.ipv4.ip_nonlocal_bind=1 <pre style="word-w ...
- 经验36--C#无名(大事,物...)
有时候,方便代码,它会使用匿名的东西. 1.匿名事件 args.CookieGot += (s, e) => { this ...
- CSV文件格式分析器执行:从字符串Split至FSM
本文乃Siliphen原创,转载请注明出处:http://blog.csdn.net/stevenkylelee/article/details/38309147 本文分为5小节,基本上就是我刚接触C ...
- 编写高质量代码改善C#程序的157个建议[1-3]
原文:编写高质量代码改善C#程序的157个建议[1-3] 前言 本文主要来学习记录前三个建议. 建议1.正确操作字符串 建议2.使用默认转型方法 建议3.区别对待强制转换与as和is 其中有很多需要理 ...
- cocos2d-x3.2中map的基本操作和使用
在游戏开发中,我们有时候会用到map,而map的使用方法我简单给大家介绍一下.Map是c++的一个标准容器,她提供了非常好一对一的关系,在一些程序中建立一个map能够起到事半功倍的效果,总结了一些ma ...