Mr. Adobo
Mr. Adobo

Reputation: 825

How to get a web service object?

I'm working on implementing some code in C# using a web service, but my only reference is a Java code they used to load test.

Java gets the object calling by calling this

lotService=(LotService) ic.lookup("mes-webservices/lotService/remote");

where IC is an InitialContext object.

I need to do this same call on C# but I have no idea how. Is there a simple way just like this java method to do it in C#?

Upvotes: 1

Views: 4695

Answers (3)

Basavaraj Kabuure
Basavaraj Kabuure

Reputation: 351

Below are the steps:

  1. Add service refrence in your project
  2. Create ServiceClient instance
  3. By using above created instance call methods it is exposing

That is it.

Upvotes: 1

Nick Gotch
Nick Gotch

Reputation: 9407

First right-click your project and select "Add Service Reference."

Once you have it you need to create the service client object. Whatever you named your service reference above you'll have a new type available in your project (named, I think, the service reference name appended with "Client" on the end. Example: if the service is FooService, you'll have a client type called FooServiceClient available.)

To instantiate, you need a binding. You can create it programmatically:

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 };

Then you need an endpoint. Create like so:

var endpoint = new EndpointAddress(serviceUri);

Then just instantiate the service client:

var serviceClient = new FooServiceClient(binding, endpoint);

You can call your service methods from the service client instance.

Upvotes: -1

Sunny
Sunny

Reputation: 4809

You can do similar thing in C# by adding service reference to web service. I assume your webservice and consuming client are both in .NET.

Psuedo code would be

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

Upvotes: 2

Related Questions