[英]How to get a web service object?
我正在使用Web服务在C#中实现一些代码,但是我唯一的参考是他们用来加载测试的Java代码。
Java通过调用此对象来获取对象调用
lotService=(LotService) ic.lookup("mes-webservices/lotService/remote");
其中IC是InitialContext对象。
我需要在C#上执行相同的调用,但我不知道如何。 是否有一种简单的方法(就像此java方法一样)在C#中完成?
您可以通过在Web服务中添加服务引用来在C#中执行类似的操作。 我假设您的Web服务和使用客户端都在.NET中。
伪代码将是
LocationWebService objService = new LocationWebService(); // this is proxy class of web service created when you add web reference
string result = objService.GetLocationName(4); //call web method
步骤如下:
这就对了。
首先,右键单击您的项目,然后选择“添加服务参考”。
一旦拥有它,就需要创建服务客户端对象。 无论您在上面如何命名服务引用,您都将在项目中使用一个新类型(我认为,该服务引用名称的末尾是“ Client”。例如:如果服务为FooService,则将有一个名为FooServiceClient的客户端类型可用。)
要实例化,您需要一个绑定。 您可以通过编程方式创建它:
var binding = new BasicHttpBinding()
{
CloseTimeout = new TimeSpan(0, 1, 0),
OpenTimeout = new TimeSpan(0, 1, 0),
ReceiveTimeout = new TimeSpan(0, 10, 0),
SendTimeout = new TimeSpan(0, 1, 0),
AllowCookies = false,
BypassProxyOnLocal = false,
HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
MaxBufferSize = 65536,
MaxBufferPoolSize = 524288,
MaxReceivedMessageSize = 65536,
MessageEncoding = WSMessageEncoding.Text,
TextEncoding = Encoding.UTF8,
TransferMode = TransferMode.Buffered,
UseDefaultWebProxy = true
};
binding.ReaderQuotas.MaxDepth = 32;
binding.ReaderQuotas.MaxStringContentLength = 8192;
if (isHttps)
binding.Security = new BasicHttpSecurity() { Mode = BasicHttpSecurityMode.Transport };
然后,您需要一个端点。 像这样创建:
var endpoint = new EndpointAddress(serviceUri);
然后只需实例化服务客户端:
var serviceClient = new FooServiceClient(binding, endpoint);
您可以从服务客户端实例调用服务方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.