How does one test logic that performs a webrequest in C#? - c#

I'm in the process of writing a C# equivalent of a Java web service client. Like the Java logic, the C# logic interacts with a web service.
In the Java world, I would test this by using something like jersey to host servlets that mocked the API and recorded interactions with the API client. Is testing in C# a similar process? If so, what how do I do this?

Related

Interaction between web and a C# .dll

I have to call a method of a complex .dll C# library when I get a "request".
Since I don't want to program the network part (socket, protocol, etc.), I'm thinking of using an existing Web server (IIS or Apache) to handle this part (and use HTTP as protocol).
Is it possible to load the .dll in memory and call a method of it from a web server? How to do it?
If yes, is it better to use IIS on Windows, Apache or Windows, or Apache on Linux?
Is it mandatory to call this method of the C# .dll using C#/ASP? Or can it be with PHP?
It has to be very scalable, and the .dll library uses the .NET Framework.
Of course I'm thinking of exec function in PHP, but this would start a C# program again and again for each single request, which is not very good.
I would like to load the C# .dll once in memory, and have it running on the web server directly if possible, so that only "calls" to a method happen when I get a specific HTTP request.
My question has similarities to this one, however I don't believe our problems are the same.
Is it possible to load the .dll in memory and call a method of it from a web server? How to do it?
Yes, for example by programming a service layer around it using WCF or the ASP.NET Web API. Given the quirky (to say the least) interoperation of SOAP services and clients, I'd go with the latter.
You can then call your SOAP/REST/giveitaname service through PHP's cURL or any other applicable method, and your C# service will then call the code in the DLL (this part you'll have to build).
You can host both types of project easily in IIS, making it accessible through HTTP.

Creating a C# Windows Service to host a WCF service integrated with a legacy C++ backend

We are building a C# Windows Service to host a WCF service integrated with a legacy C++ backend that will service requests made to the WCF service.
Is it possible to construct the C# front-end so that it accepts a minimum SOAP request (possibly a default query in the envelope) and then pass the entire SOAP message (or at least the XML contents of the envelope) to the C++ backend for interpretation so that the details of the interface don't have to be duplicated in C# or exposed to clients of the service (which would know which requests are available and the data structures required for each)?
We would also need to be able to construct partial XML in the C++ backend which the C# front-end would then insert into a SOAP envelope to make the response.
Is it sensible to target using SOAP for this or would it simplify things to use a simpler XML structure (home-grown or alternate standard) that can be passed in its entirety to the backend where the entire response would be constructed?
Thanks for reading, any advice welcomed :)

.Net UI and Java Middle Ware

Hi We are in process of building new application which will have .Net UI and Java Middle Ware (Services) what. As I am fairly new to Java i would like to explore my options in terms of what are good practices so that .net uis can talk to java middle tiers. Java middle tier will eventually call to database...
Thanks
In addition to designing something that WCF understands, you might consider designing a ReSTful service using standard HTTP semantics.
For the Java side, you can use:
Jersey or
RESTlet
On the consumer side in .NET, you can use:
RestSharp or
HttpClient
Build your java middle ware with some sort of service that WCF can communicate with and you should be good to go
Here is a simple example
http://blogs.msdn.com/b/bursteg/archive/2008/07/19/how-to-call-a-java-ee-web-service-from-a-net-client.aspx
Any client that can send an HTTP request, REST or SOAP, can communicate with an HTTP listener or service, regardless of which language each one is implemented in. That's why HTTP based services have been such a winner, beating out Corba, RMI, COM, etc.
Simple and open win.

Testing a web service wrapper

I have an assembly which wraps the functionality of an external live web service.
How do I go about testing this web service effectively. Should I create a stub of the web service or should I write tests that send and receive live data to the web service?
The problem I have with the second approach is that if I send and expect real data from the web service then I can't assert the results each time as they might change!
If the wrapper only forwards the calls to the web services with no conditional logic at all, there’s no point in creating tests against it that don’t go through the real web service. In this case you should create one test for each operation which should only test the ability to reach the web service and come back without unexpected errors. The data returned really don’t matter. This is an integration test between your wrapper and the web service.
If your wrapper includes conditional logic, then it may be a good idea to create tests that exercise all the paths. It’ll be easier to test these cases if you do not depend on the real web service.
For testing the client code (the code that calls the wrapper), you should stub the wrapper or stub the web service. This will give you the control you need to guarantee the client always receives the same output given the same input.

C# & PHP: Simple example of PHP Web Service used by C#

Could anyone give me a small and simple example of how to do this? Or some good pointers on how to get started.
I would like to create a C# client that can send a file or some text or xml or whatever, to a web service or something similar written in PHP, where the PHP Web Service stores it in a file or a database or something like that. Just not sure how to get started.
I guess the first step would be to create the php web service. And then it perhaps would be quite easy to use it in C#, since I could probably pretty much use the "Add Web Reference" button in vs and then go from there?
You can take a look at this tutorial showing how to develop a web service using php. The .NET client will be pretty straightforward as you mentioned.
I'm not versed in the C# aspect of your question, which you intend to be the consumer. But if you don't mind using a library to expedite the process of getting the webservice up and running, you can set one up very quickly using Zend Framework. Check out the documentation for setting up a Zend_Rest_Server.
Just a thought... if the service only has to support simple operations like "upload a file", perhaps avoid WSDL all together? I assume PHP can handle raw requests - so you can use the oh-so-complex .NET client:
using (WebClient client = new WebClient())
{
client.UploadFile("http://some/upload.php", "foo.bar");
client.UploadString("http://some/upload.php", "some text");
}
etc. Likewise, you can often easily use POX / REST without the complexity of a formal web-service contract. The above code just does a simple HTTP request with the file-contents/string in the body.
you are right. all you need to do is to create a simple web service in PHP, which accepts the request/xml/file and then stores it in a database. then you can use the webservice using any technology, C# for sure.
in order to write a web service in PHP, I think it is better to choose your webservice type in the first step. it can be a REST web service, or SOAP, or XML-RPC. it depends on the level of complexity of your application.
if your application is as simple as just throwing a string to the webservice or a file URL, so then the webservice can store that string, or fetch the file from the URL and stores it in database, I recomment REST. because it is simpler. as simple as just writing a simple PHP script that uses it's HTTP parameters as input.
but incase you want it to be more secure or have more complex needs, It is easy to have SOAP services in PHP. if you wanted to work with SOAP, PHP has an extension (php-soap) which you can install and use the built-in functionality of PHP-SOAP. if you are using an older version of PHP that does not support the extension, or your application will be hosted where php-soap is not installed, there is pure PHP implementaion of SOAP protocol, named nusoap. it is open source and works very well.
http://sourceforge.net/projects/nusoap/
I have made work in PHP NuSOAP webservice and my C# mobile client fetching data from web services.Below is the link
http://khanmubeen.wordpress.com/2010/11/18/web-services-with-php-nusoap-c-mobile-client/
In case you like to know more from me welcome to inbox me at mubeen44us at gmail.com

Categories