I use the following code to invoke a webservice from wsdl dynamically, which is working perfectly:(see docs)
Now I want to have the raw data from the response. I found the following:
(Docs)
But to use that example I need a place to add the method attribute of the extension. Because I invoke the webservice method through reflection, I don't have the method definition in my code.
On the previous webpage they are talking about creating a soapextension to capture the raw messages.
But where do I need to apply the extension in the first code, to have a dynamic webservice invoker with the possibility to request the raw data?
I had to convert my code to use WCF instead of the legacy webservice support in C#.
It's also possible to create a class with methods defined in the WSDL file. This allows me to invoke the webservice dynamically like I already did. On the following site I found some example code: http://blogs.msdn.com/b/vipulmodi/archive/2006/11/16/dynamic-programming-with-wcf.aspx
To get the raw data the only thing I needed to do was adding a behaviour to the endpoint of the service. I learned how to do that on the following page: http://mbsguru.blogspot.be/2012/11/capturing-and-using-raw-soap-messages.html
Related
I was recently looking at postsharp to try to auto-encode all parameters on an asmx webservice without rewriting the whole webservce, this from my thinking would go along this route.
1.Create a MethodInterceptApsect class from Postsharp to handle the parameters.
2.Apply the Aspect to my Webservice class
3.In the aspect, change / encode all values of type string
In my test application this worked, but it failed when it came to using the javascript webservice, this from my understanding is because of the way asmx calls are handled, is there a way this can be achieved using postsharp? Intercepting webservice calls and changing parameter values before they are sent to the service?
I'm trying to find a way to read in a WSDL file (I will not have the source of the service) that requires a custom type as input.
I am currently trying to test with this file http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
So in this I am dynamically calling the ConversionRate method. I input two strings for the "ToCurrency" and "FromCurrency" fields needed and then I use String.Format and create a class named the same as the expected one "Currency" in this case and I've made the to/from currency as public strings in the class I create and set them to the two input strings I gave in the beginning. When I try to invoke the service this way I get an error like cannot convert type Currency to Currency.
I assume this is because the class I've created is not the same as the one implemented in the code for the service I'm calling.
My question is this, can I create a class of the appropriate type to successfully pass in and invoke the method using only the data I get from the WSDL file?
Use Microsoft's wsdl.exe link.
wsdl.exe http://www.webservicex.net/CurrencyConvertor.asmx?wsdl
The tool can be found in your Microsoft SDK folder under program files. If all you want is to consume the web service, then Jen's suggestion is best imho.
You can have the WSDL tool automatically generate proxy classes to work with the service: How to consume a Web service in visual studio from only the wsdl
I'm playing around with WCF and wondering about the following. I'd like to be able to get a list of all available service methods and parameters associated with these methods.
Now, I've tried to work with the ServiceDescription namespace, but it seems flawed. For one .svc it works, for another it doesn't.
What would be an okay way to approach this? Any tips?
You could just download the WSDL of the webservice and parse that (it is XML, see http://www.w3.org/TR/wsdl) since the WSDL contains all information like interfaces/methods/parameters etc.
Some helpful resources including source code for doing what you want:
http://webservicestudio.codeplex.com/
http://wizdl.codeplex.com/
http://soap-sec.sourceforge.net/
From .NET(C#) code, we are invoking a Java Web Service.
Its a SAOP request. The Java WebServices are developed using Axis 1.4.
Following is the sample code that makes the java web service request:
private string GetUserInfo(string strEID)
{
string strEmpData = string.Empty;
GetEmpInfo.EmpProxyService objEmp;
try
{
objEmp = new GetEmpInfo.EmpProxyService();
strEmpData = objEmp.searchByEID(strEID);
}
catch (WebException ex)
{
}
objEmp.Dispose();
return strEmpData;
}
Now, we have a change request which requires passing some additional information - a name_value pair to the java webservice.
How can we achieve this ?
Can I pass the information in HTTP/SOAP headers?
Changing the method signature and adding the additional info to pass the info is not at all the good idea I guess.
EDIT: Its basically we want to add the logging inforamtion of who are all consuming the web services. Once the java webservice request is processed successfully, we will log the usage information along with the source of the request (from webappln/windows appln/flex client).
We want the clients to send its unique id to identify it. Since this has nothing to do with the business logic, can we add it meta-data info...say in headers.
If you have control over the service signature, I would actually suggest that you change the signature of this web service, or add another method that takes the additional arguments. When you're using a high-level language like C# or Java, the tendency is for the web service framework to abstract the entire SOAP stack away from you, and leaves you dealing with just the plain objects that eventually get serialized to make the method call. With only the argument objects exposed, it can be tricky to try to inject additional stuff into the SOAP message, if it's not part of the actual method signature.
There are usually ways to manipulate the SOAP message by hand, but I would probably shy away from that if possible, as editing the SOAP message by hand goes against the point of using a serialization-driven framework. That said, if you have no control over the service method, and the group in control of it needs you to pass additional data outside of the soap objects, you might be stuck messing with the SOAP message by hand.
If you want to add some future proofing to your services, I would suggest passing a full-fledged object rather than a single string or primitive value. In your object, you could include a key-value data store like a HashMap or Dictionary so that additional data can be passed without changing the signature or schema of the web service. With key-value data, documentation becomes important because it's no longer clearly specified data types or parameters.
You can use SOAP headers but I would rather not go that route since the headers have no business meaning. Rather change the signature and use request and response objects.
SearchByEIDResponse GetEmpInfo.EmpProxyService.searchByEID(SearchByEIDRequest)
Ths makes any changes less painfull and prevents huge parameter lists.
How you pass information to a web service is dependent on the methods that web service exposes. What language that service is written in is inconsequential to you as the consumer. IF the Java web service requires a name value pair to retrieve some data than a method signature will expose that.
objEmp.searchByEID(strEID, strVal1, strVal2);
That said you as Eben indicates you are better off using request and response objects to keep your parameter lists short. When to use these more complex types comes with experience i.e. don't use a request object from the get go if you need to only pass a single string value, but use a request object if you need to pass 50 string values.
If you have multiple webservices and don't want to change all methods (which is reasonable), a SoapExtension is the way to go http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapextension.aspx
You write your soap extension class on the client, declare it in the web.config and you're done.
I have an asp.net MVC2 application that needs to call a web service from the controller. How do I do this? It is a RESTful service that returns Json data.
I cant seem to find a good example.
Thanks
You call it the same way you would do in any ASP.net application, it is not connected to MVC at all.
Either add a reference and use that (easiest) or go with the manual method: here is a guide, see at towards the end (fig. 14 in particular) for consuming such services:
http://msdn.microsoft.com/en-us/magazine/dd943053.aspx
I have written my own ActictiveResource client framework, which allows the consumer to specifiy the http provider and the serialisation provider. The generic activeResource class has the four main verbs (get,put,post,delete) as methods which it calls against a specified resource url, passed in at cunstruction. the fololwing is an example of getting a product item from teh service:
ActiveResource<Product> arProduct = new ActiveResource<Product>(jsoSerializer,liveHttpProv,"https://company/product/1452");
//Get verb
Product prod = arProduct.Get();
Of course you can also use the other verbs on the object to put, post and delete.
arProduct.Post(prod);
The code, basically, wraps up the underlying http post,put, get functions and takes care of the serialiasation of the payload to objects. It has been a very useful component which I have used over and over again. The code can be easily called from a controller, it may be worth using a IOC container (I am using Th eUnity block) to instatiate you providers
Hope this helps
I would put together a simple class that acts as a "client" that makes a web-request from the URL, and then return the response as a string.
From there you can deserialize the JSON data using either the JSON serialization that ships with WCF, or the most excellent JSON.Net library. You will need to create a simple data class that is structured in the same way as the JSON data your expecting back.
You could also combine the two and have your client class return the deserialized object directly.