WebService服务参数问题,在线等!!

国风 2009-04-20 09:37:14
代码如下:
//自定义类
public class MyClass
{
public MyClass();

public string message { get; set; }
public string otherdata { get; set; }
}

//服务
[WebMethod]
public int testservice(MyClass parameters)
{
....//代码省略
return 0;
}

客户端调用时报:未将对象引用设置到对象的实例。
请问如何将MyClass实例化,或者是否有其他方法!
急!!!
...全文
453 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
slimboy123 2009-07-20
  • 打赏
  • 举报
回复
MyClase要序列化

在web服务类前面加上
[System.Web.Services.Protocols.SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]

示例:

namespace TestDemo
{
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Serialization;


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.test.com")]
public partial class MyClass
{

private string messageField;
private string otherdataField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string message
{
get
{
return this.messageField;
}
set
{
this.messageField = value;
}
}


/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string otherdata
{
get
{
return this.otherdataField;
}
set
{
this.otherdataField = value;
}
}
}


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.Web.Services.WebServiceAttribute(Namespace = "http://www.test.webservice.com")]
[System.Web.Services.WebServiceBindingAttribute(Name = "TestHttpBinding", Namespace = "http://www.test.com")]
[System.Web.Services.Protocols.SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
public partial class TestService : System.Web.Services.WebService
{
/// <remarks/>
[System.Web.Services.WebMethodAttribute()]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace = "http://www.test.com", ResponseNamespace = "http://www.test.com", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlElementAttribute("out", IsNullable = true)]
public int testservice([System.Xml.Serialization.XmlElementAttribute(IsNullable = true)] MyClass in0)
{
if(in0 == null)
{
return 0;
}
else
{
return 1;
}
}
}
}
国风 2009-04-20
  • 打赏
  • 举报
回复
System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Bare)
我设置了Action后,上面的错误是没了,但客户端调用时又出现:
服务器未能识别 HTTP 标头 SOAPAction 的值错误
dawnbear 2009-04-20
  • 打赏
  • 举报
回复
传的时候要传 MyClass 对象啊 那边要先实例化

webservice是通过传递xml来传输数据 所以你不管传什么 都会序列化成xml 所以他是必须要有序列化标识[serializable]
suners 2009-04-20
  • 打赏
  • 举报
回复
上面说的都挺好的 】

你在找点例子看看 就明白了 要么让你们老大告诉你
国风 2009-04-20
  • 打赏
  • 举报
回复
我是从wsdl文件导出**interface.cs后,派生接口类生成service服务的,但现在报另外一个错误:
System.Web.Services.Protocols.SoapException: 方法 notifySmsDeliveryReceipt

和 notifySmsReception 使用相同的 SOAPAction“”。当 XML Web services 的

RoutingStyle 为 SoapAction 时,SOAPAction 值在 XML Web services 的所有方法中

必须是唯一的。可以通过 SoapDocumentMethod 或 SoapRpcMethod 属性的 Action 参

数更改 SOAPAction,也可以指定 XML Web services 上 RequestElement 的

RoutingStyle。
在 System.Web.Services.Protocols.SoapServerType..ctor(Type

type, WebServiceProtocols protocolsSupported)


System.Web.Services.Protocols.SoapServerProtocol.Initialize()


System.Web.Services.Protocols.ServerProtocol.SetContext(Type type,

HttpContext context, HttpRequest request, HttpResponse response)


System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type,

HttpContext context, HttpRequest request, HttpResponse response, Boolean&

abortProcessing)
hm020 2009-04-20
  • 打赏
  • 举报
回复
楼主如果是客户端调用服务 看这个.
[Quote=引用 10 楼 wangping_li 的回复:]
你是客户端调用服务吧?先将MyClass标识为可序列化的(即:[Serializable])
然后你在传testservice方法的参数时先:
服务对象.MyClass parameter=new 服务对象.MyClass();
parameter.message =值;
parameter.otherdata=值

然后将parameter作为参数传给testservice就行了
[/Quote]

zhoulehua 2009-04-20
  • 打赏
  • 举报
回复
UP
jietuan 2009-04-20
  • 打赏
  • 举报
回复
web service ,是以channel作为媒介来传递数据,且这些数据是以流的形式进行,因此你用到的类必须要能进行序列化!
冷月孤峰 2009-04-20
  • 打赏
  • 举报
回复
1、将你的类(MyClass)标识为可序列化的([Serializable])
2、实例化:MyClass MC=new MyClass();
wangping_li 2009-04-20
  • 打赏
  • 举报
回复
你是客户端调用服务吧?先将MyClass标识为可序列化的(即:[Serializable])
然后你在传testservice方法的参数时先:
服务对象.MyClass parameter=new 服务对象.MyClass();
parameter.message =值;
parameter.otherdata=值

然后将parameter作为参数传给testservice就行了
Z_L_H 2009-04-20
  • 打赏
  • 举报
回复
UP
ZM27080401 2009-04-20
  • 打赏
  • 举报
回复
myclass这个类要定义属性是可序列化的,其次你贴出来这前台网站和webservice里面哪里有过对象的实例化和调用这个方法的代码,才好看啊
bulong0721 2009-04-20
  • 打赏
  • 举报
回复
System.Xml.Serialization Namespace

SoapElementAttribute
SoapEnumAttribute
SoapIgnoreAttribute
SoapIncludeAttribute
fengrx 2009-04-20
  • 打赏
  • 举报
回复
同意2楼。
yo_yokel 2009-04-20
  • 打赏
  • 举报
回复
肯定是 栈没有赋值造成的,声明的变量没有初始化的原因。
Garnett_KG 2009-04-20
  • 打赏
  • 举报
回复
MyClass 加上Serializable属性.


[Serializable]
public class MyClass
{
public MyClass();

public string message { get; set; }
public string otherdata { get; set; }
}

我不懂电脑 2009-04-20
  • 打赏
  • 举报
回复
MyClass myClass = new MyClass();
深海之蓝 2009-04-20
  • 打赏
  • 举报
回复
首先 添加web引用,然后再程序中 new 一下就可以了
blestcc 2009-04-20
  • 打赏
  • 举报
回复
那函數怎么是在類的外面?
lcm_xiaoya 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 llwinnner 的回复:]
首先 添加web引用,然后再程序中 new 一下就可以了
[/Quote]
2楼这种方法最简单了。

另外一种方法是,你用反射获取WebService服务端函数public int testservice(MyClass parameters)信息 ,然后用反射获取 函数的参数MyClass类型,获取这个MyClass类型后,再用反射动态生成MyClass对象实例。 (可参考发射的一些资料) 用这种方法调用WebService的话,通用性很强。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧