C#错误连接到SAP中的Web服务

时间:2014-08-29 09:31:15

标签: c# .net web-services soap sap

我在SAP中有一个Web服务,我必须创建一个C#客户端来处理Web服务。我创建了客户端,但收到来自C#的错误。错误是这样的:

Object reference not set to an instance of an object.

我的客户来源是:

Uri uri = new Uri("http://address");
var address = new EndpointAddress(uri, EndpointIdentity.CreateSpnIdentity("teste"));
ServiceReference2.ZWS_PEP_ENVIO_MRZClient wsclient = new ServiceReference2.ZWS_PEP_ENVIO_MRZClient();
//  wsclient.Endpoint.Binding.Scheme;
wsclient.ClientCredentials.UserName.UserName = "user_name";
wsclient.ClientCredentials.UserName.Password = "password";
wsclient.Endpoint.Address = address;
wsclient.Open();


ServiceReference2.ZwsPepEnvioMrz request = new ServiceReference2.ZwsPepEnvioMrz();
request.Descricao = descricaoField;
request.Mrz1 = mrz1Field;
request.Mrz2 = mrz2Field;
request.Numeroprocesso = numeroprocessoField;
request.Sn = snField;
request.Statusdatapreparation = statusdatapreparationField;
request.Versaodocumento = versaodocumentoField;

wsclient.ZwsPepEnvioMrz(request);
ServiceReference2.ZwsPepEnvioMrzResponse1 response = new ServiceReference2.ZwsPepEnvioMrzResponse1();
Resultado = response.ZwsPepEnvioMrzResponse.ToString();
textBox1.Text = Resultado;


wsclient.Close();

程序终止于

Resultado= response.ZwsPepEnvioMrzResponse.ToString()

任何想法为什么?

1 个答案:

答案 0 :(得分:0)

这不起作用:

wsclient.ZwsPepEnvioMrz(request);
ServiceReference2.ZwsPepEnvioMrzResponse1 response = new  ServiceReference2.ZwsPepEnvioMrzResponse1();

您无法自己创建新的响应对象,并希望它包含任何数据。响应对象应该是Web服务调用的结果。我无法检查这个类型是特定于您的Web服务,但如果ws方法ZwsPepEnviroMrz返回ServiceReference2.ZwsPepEnvioMrzResponse1类型的对象,您应该尝试这样做:

ServiceReference2.ZwsPepEnvioMrzResponse1 response = wsclient.ZwsPepEnvioMrz(request);

现在response变量应该包含一些数据和

Resultado = response.ZwsPepEnvioMrzResponse.ToString();

应该有用。