WCF Rest Service, Request Handling - wcf

I have a WCF service using webHTTPBinding. The service can be consumed by a client using some C# program or JavaScript.
The service is running fine and also working exactly I wanted. One of the method in service looks like
public string JSONGetSomedata(List<MyCustomObject> item)
{
// some code here, which is working fine....
}
When this service is accessed using a client or JavaScript, It works fine provided that I'm passing passing data to method as expected.
Now, The case where I'm consuming mentioned service using JavaScript, if I pass nothing or pass item=123 then request does not reach to WCF method at all. And it throws error like
Is there any way by which I can return some friendly error or other Json/XML data...?
I'm not sure how to do it...
Note here that, I'm looking for solution on how to return Json/HTML instead of this error message.

Related

Windows Phone - WCF - cannot refresh webservice call

I have a WCF web service, and a windows phone application.
The phone app's home page has a WCF ("GET") call I would like to refresh every 30 seconds.
No matter what happens to the data on the back end... the call to the WCF service will always return the data from the original call to the WCF service.
If i go to another page, and make the same call I will get the different modified data.
Is there some kind of caching on "GET" calls on the phone side?
Debugger
I do not think it is even going to the WCF to make the call.
My debugger looks like it's not even hitting the WCF again when I try to refresh. The HTTPWebRequest just spits out the oroginal GET call if i am on that same page.
Details
WCF - webHTTPBinding (REST)
I found the answer...
There must be some Magic Caching on windows phone going.
I recall reading on some other thread a while back and tried it.... and it worked !!
Basically i'm just adding a unique id that doesn't matter to he URI string. In the case below.. i decided to use a GUID... this way each call uses a unique different URI for the REST call. It seems to work.
string uri = RESTCon.BaseString + "RESTMethodCallName?id={0}";
w.DownloadStringAsync(new Uri(String.Format(this.uri, Guid.NewGuid().ToString())));

Accessing .NET WCF services from Android

I have a .NET WCF service (not a web service) with many methods, some accepting and returning complex data types. I use these services from my Windows Phone 7 apps. It all works great and it's easy.
Now I'm evaluating the feasibility of porting some of my apps to Android, but I can't figure out how to invoke my WCF services from an Android client.
I have a working example I found in Invoke webservices from Android.
But this looks to be accessing a "Web Service", not a WCF Service.
My service is at http://www.deanblakely.com/Service2.svc, and it contains a simple method named "SimpleTest" that just returns the string "Alive".
Using the code in the linked article, I put http://www.deanblakely.com/Service2.svc in SOAP_ADDRESS and SimpleTest in OPERATION_NAME. But I have no idea what to put in SOAP_ACTION and WSDL_TARGET_NAMESPACE. I don't even know if this approach is valid.
In .NET, Visual Studio builds us a "Service Reference" and everything just works.
I also don't understand the following two lines of code...
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
With WCF services, the call is async so we make the call to SimpleTestAsync and leave a callback for the async return. These two lines of code appear to be synchronous, no?
When communicating with WCF services from a non-Windows client, you are basically treating it as an XML Web Service. If you configure your WCF service to use a basicHttp binding, it will act just like any other web service, as far as Java is concerned.
Normally, to call a WCF service from Java, you use wsimport to create a custom set of proxy and data classes, similar to the way a service reference works. Android doesn't have all the libraries needed for those classes, but I did find this URL:
http://code.google.com/p/androidclientgenerator-wsimport/
That is a proxy class generator specifically for Android. Instead of using the code on that web page, you may want to download this proxy generator; you simply have to pass it the URL to your service's WSDL page and it will create typed Java classes for everything. If you have complex types being passed back and forth, this is probably a much better option.
However, if you want to continue with the sample code, you'll need to fill in those variables you've identified. The variables are just the typical parameters for a SOAP envelope. These are defined in the WSDL for your service, and are primarily based on the namespace that you have defined for your service. (They are largely independent of the actual URL that your service lives at, with one exception). You specify the namespace in the WCF service contract:
[ServiceContract(Namespace = "http://namespaces.deanblakely.com/wcf")]
Note that the namespace URL doesn't need to point to a real resource, though frequently they do. It doesn't even need to be a URL (I often use urns); it just needs to be a unique string of characters. For now lets assume you assigned the above namespace to your service.
The WSDL_TARGET_NAMESPACE is just the namespace, exactly as above. The OPERATION_NAME is the name of the method you want to call, for example SimpleTest. The SOAP_ACTION is the combination of namespace an operation; in your case that would be http://namespaces.deanblakely.com/wcf/SimpleTest. In your WSDL you would see this described in an operation tag:
<wsdl:operation name="SimpleTest">
<soap:operation soapAction="http://namespaces.deanblakely.com/wcf/SimpleTest" style="document"/>
The SOAP_ADDRESS is the only one that actually points to your service file, e.g. http://www.deanblakely.com/Service2.svc.
Hopefully that will get you started calling in to your web service; if not feel free to stop back and get more assistance.
EDIT: Missed the part about async calls.
Yes, the method described on that web page is synchronous. WCF service methods are synchronous by default, with the option to include asynchronous calls when generating a service reference in Visual Studio. wsimport generates async-ready proxies, so using the Android client generate may also help you out in this area.

Using WCF 'Message' to call external service

I have a class, in which I have a service reference (WCF) to an ASMX web service.
This obviously generates local proxy methods such as
string DoSomething(string someParameter, string someOtherParameter)
I have a method that receives a WCF message class already representing a call to this service, which I simply need to forward
I could of course use XmlDictionaryReader to extract the information from the WCF message, deserialise into the proxy classes and pass those into the proxy method, but as these will simply get serialised back it seems very wasteful
How can I call the service using the already serialised message? (I assume I will need to modify the soap action on the incoming message)
There's a two-part series on how to build a WCF router on MSDN - maybe that helps? Seems like that's more or less what you're trying to do - use a WCF service to basically route a message on to a second service (ASMX in your case).
Marc

Getting a WCF service to map POST'd parameters to arguments

I have a legacy service I'm looking to update to WCF and one of it's behaviours is to allow clients to POST a request that has something like:
MyService.asmx/ProcessDocument
With Post data looking like:
request=<big block of xml>
Now in the ASMX days this service accepted a single string parameter i.e:
public void ProcessDocument(string request) {
}
So far I have only gotten this to work in WCF by using a Stream as of the advice in this post here:
http://www.dennydotnet.com/post/2008/09/16/WCF-REST-and-POST-Lets-Dance!.aspx
A Stream will work, there are just more steps involved to make it work for something that seems to it should be supported out of the box.
I am pretty new to WCF - what am I missing?
OK, this sample got me to most of where I needed to go:
http://msdn.microsoft.com/en-us/library/bb943485.aspx
I now have it working as required.

View underlying SOAP message using vb.net

I have a VB.NET web service that calls a third party web service. How can I view the SOAP message generated by .NET before it is sent to the third party web service and how can I see the SOAP response before it is serialized by .NET.
When creating a standalone EXE, I see the Reference.vb file that is automatically generated, but don't see a similar file when my project is a web service. I have found lots of C# code to do this, but none in VB.NET.
Edit - Fiddler and TCP loggers are great, but will not work for my purposes. I need to be able to access the raw SOAP messages from within the application so I can log them or modify them. I need to do more than just see the messages going back and forth.
You can use fiddler or a tcp sniffer to filter and identify all outgoing and incoming traffic on your host.
This is if you want to see the xml request and response.
How about using an extension to allow you to examine the SOAP message?
Accessing Raw SOAP Messages in ASP.NET Web Services
http://msdn.microsoft.com/en-us/magazine/cc188761.aspx
I was trying to do the same thing and this seems to work for me:
Dim message As String = OperationContext.Current.RequestContext.RequestMessage.ToString()
I didn't think it would be that easy since most of the time ToString() returns the name of the class, but I tried it out and low and behold.
I know you asked this back in January so if since then you've figured out a better way let me know.
Please note that if you're catching the exception in a class that implements IErrorHandler then you have to perform this operation from within the ProvideFault() method instead of the HandleError() method because the context is closed before it gets to call the HandleError() method.
Hope this helps.