sandeep
sandeep

Reputation: 77

how to call webservicemethod in windows service

Basically my idea is to develop a proxy which will run in windows

I have created windows service application which running successfully and i have integrated a web service code in the windows service application running in windows service.

How to call that web service method when the client hits my url?

How to form the url which can call web service method to get the method return value?

Upvotes: 0

Views: 5045

Answers (1)

illegal-immigrant
illegal-immigrant

Reputation: 8224

OK, I'll try to answer.

  • Let's assume you want to call REST web service. What do you need? A HttpClient and (probably) JSON/XML Serializer. You can use built-in .NET classes, or a library like RestSharp

Sample calling REST web service using RestSharp

var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", 123); // replaces matching token in request.Resource

// easily add HTTP Headers
request.AddHeader("header", "value");

// add files to upload (works with compatible verbs)
request.AddFile(path);

// execute the request
RestResponse response = client.Execute(request);
var content = response.Content; // raw content as string

// or automatically deserialize result
// return content type is sniffed but can be explicitly set via RestClient.AddHandler();
RestResponse<Person> response2 = client.Execute<Person>(request);
var name = response2.Data.Name;

// easy async support
client.ExecuteAsync(request, response => {
    Console.WriteLine(response.Content);
});

// async with deserialization
var asyncHandle = client.ExecuteAsync<Person>(request, response => {
    Console.WriteLine(response.Data.Name);
});

// abort the request on demand
asyncHandle.Abort();

You are not required to use RestSharp, no. For simple cases HttpWebRequest (+DataContractJsonSerializaer or Xml analogue) will be just perfect

  • Having SOAP web service? Follow the instructions provided here

Upvotes: 2

Related Questions