How to call a web service with a configurable URL - c#

I wrote a web-service. I wrote a website. I want the website BLL code to call the web-service.
I have a config table with this service URL. I inject the web-service URL to the calling code. What web client or socket in C# should I use that can receive a dynamic web-service URL?
I thought to use:
WebClient webClient = new WebClient();
UTF8Encoding response = new UTF8Encoding();
string originalStr = response.GetString(webClient.DownloadData(BLLConfig.Current);
But maybe there is more elegant way?
I'm loading the configs at run time from a DB table.
Here is how I tried to use a web-reference in Visual Studio:
using (var client = new GetTemplateParamSoapClient("GetTemplateParamSoap"))
{
TemplateParamsKeyValue[] responsArray = client.GetTemplatesParamsPerId(CtId, tempalteIds.ToArray());
foreach (var pair in responsArray)
{
string value = FetchTemplateValue(pair.Key, pair.Value);
TemplateComponentsData.Add(pair.Key, value);
}
}

You can add the URL of the web service as a Web Reference in Visual Studio and then set the Service.URL property to the value from the config

.NET has lots of built-in support for consuming web services... after adding the service reference to your project it generates the necessary code... whcih you can use as is - if you need to configure the URL the generated client class has a URL property which you can set accordingly... for an excellent walkthrough see http://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/ and see SOAP xml client - using Visual Studio 2010 c# - how?

Related

Consume Swagger definition to generate c# client

I'm trying to consume the Swagger sample Petstore definition .json file at -
https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/json/petstore.json
I used VS 2017 > Add > Add REST Api Client functionality. The import is successful and the Swagger Petstore proxy classes are successfully generated and added to my project.
Now I want to write C# code that is actually able to call the Petstore API.I'm unable to find any concrete examples that leverage the proxy classes above.
The way I have worked with REST services has been using the Web Client class in C# -
string sendText = "abc";
WebClient client = new WebClient();
c.Headers[HttpRequestHeader.ContentType] = "text/xml";
result = c.UploadString(url, sendText);
How do I write code that calls the PUT GET POST etc. operation on the Pet Store API?

c#: How to consume webservice methods whose address is unknown at compile-time

I am consuming a webservice from a Java server. The webservice provides me with some methods that need. Up till now I have been using the method where I added the reference of the webservice in the project explorer, typed the address of the webservice and compiled it. But now I need it to pick up the address of the service at runtime from an xml file or something! is that possible?
There is Url property in generated proxy object that you can set at runtime. Covered in Creating the Web Service Proxy article on MSDN.
Sample from the article (shows how to also set credentials, you may also need to set Proxy):
var rs = new ReportExecutionService();
rs.Url = "http://<Server Name>/reportserver/reportexecution2005.asmx?wsdl";
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
Note that above answer is for case when you don't know exact Url of server till runtime, but you have WSDL/sample server available at design time and able to generate proxy via add web service in VS (or manually).
Alternatively you can call service via other classes implementing "HTTP GET" like HttpClient and configure anything you want, but will need to do your own parsing of results.

Consume WCF Rest Service (JSON) using C# ASP.NET MVC

I'm having difficulty consuming a WCF REST service, which returns JSON, in a C# ASP.NET MVC application. I'm trying to consume the service in a Controller. I have a ASP.NET MVC project and a service project in the same solution. I've created an entry in my local IIS which points to the service project (i.e. http://localhost/SampleService/).The WCF Service works because I can access the URL and the correct JSON is returned.
Does anyone have any code samples on consuming the JSON via a Controller from a RESTful WCF Service?
You can use the DataContractJsonSerializer:
Here's an example:
var client = new WebClient();
var data = client.DownloadData("http://localhost/SampleService/GetJsonMessage");
var stream = new MemoryStream(data);
var obj = new DataContractJsonSerializer(typeof(string));
var result = obj.ReadObject(stream).ToString();
In your controller you can do this to view the result
return Content(result.ToString())
I used WebChannelFactory and it worked great.
You can either use the built-in DataContractJsonSerializer, or the JSON.NET library's JsonSerializer.
I prefer the latter, because it is more robust. Sometimes the DataContractJsonSerializer cannot deserialize a JSON object.
Sample code:
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(jsonText);
To download the library, go to http://json.codeplex.com/

Call a Web Service

I have connected to a Web service by adding it as a web reference,
the wsdl and xsd files are now visable,
im using visual studio,
How can I get a response from the Web service,
i.e. send some data and get an error message back.
Thanks for your time.
Once you add a web service reference, Visual Studio will generate a strongly typed client proxy class which you could instantiate and invoke the web method:
using (var proxy = new SomeClientProxy())
{
var result = proxy.SomeMethod("foo", "bar");
}
Just Create the object and use its method as you normally do with regular class objects
1- YourWebService obj = new YourWebService();
2- call methods on this object.
obj.YourWebServiceMethod();

Connect to Unknown SOAP Web Service

I would like to build an app in C# that connects to an Apache AXIS web service and performs the following operations via SOAP.
Login in to the server.
POST string data to server
Receive and display server response
Here's the tough part. I do not have access to the server, nor do I know where the .JWS file is located on the server. I was able to get to the WSDL file in my web browser, so I know a "Login" operation exists as well as an operation to take in data.
I have tried accessing the web service via URL, but I keep getting this message:
Hi there, this is an AXIS service!
Perhaps there will be a form for
invoking the service here...
In summary, is there anyway I can connect to this web service when all I have is the URL of the WSDL file? Are web services accessible via URL?
Thank you
Use WCF, and generate client proxies to the web service using the svcutil.exe tool.
running svcutil.exe http://url.to/webservice?WSDL the_wsdl.wsdl /language:C# should generate proxy classes you can use in your C# project, and you'd call the service e.g. like
BasicHttpBinding myBinding = new BasicHttpBinding(); //might not even need these
// 2 lines if you ran svcutil.exe directly on the web service URL
EndpointAddress myEndpoint = new EndpointAddress("http://url.to/webservice");
TestClient client = new TestClient(myBinding,myEndpoint); //the generated classes
// svcutil.exe created
client.SomeOperation(42); // call an SomeOperation of the web service
Thanks for everyone's help. Looking back on this question, I can see how severely confused I was. Here is the solution I followed.
Assuming you know the URL of the WSDL file of the service you wish to connect to then just do the following.
Boot up Visual Studio
On the top toolbar navigate to Data -> Add New Data Source then choose Service in the new dialog
In the address bar, enter the URL of the wsdl file (EXAMPLE: http://server.com/services/displayName?wsdl)
Near the bottom of the dialog, change the namespace to something relevant to the project (EXAMPLE: sampleService)
Now Visual Studio should compile the client proxies for you that you can use to access the web services on your server. To access one of the services, all you need to do is create a new object from the class.
//Example
sampleService.ClassName test = new sampleService.ClassName();
test.displayName("Jack");
See http://msdn.microsoft.com/en-us/library/bb552364.aspx for a starting point

Categories