No WCF service response - C# - c#

I have added a service reference, directed to a WSDL which loaded all existing WSDLs from the location. I am trying to request data by calling one of the methods like this:
protected void Page_Load(object sender, EventArgs e)
{
string fLastname, folder, status, header, responsible;
int date;
PyramidServices.ServiceSoapClient client = new PyramidServices.ServiceSoapClient();
string activity = client.GetUserActivity("xxxx", "xxxx", out fLastname, out folder, out status, out date, out header, out responsible);
}
When debugging, the vars are all null. . I have a hard time understanding trace messages since this is not my field. I don't know what to look for, so here is pretty much all the information I get when tracing (Sorry for the Swedish version!):
Sending:
Receiving:
Answer # Channel:
Stop:
[EDIT]
I tested the method using WCF Test Client and received following:
So it's kind of obvious that I am doing something wrong with the code when parsing the data from the response? My question is, how do I accomplish this?

With a lot of help from #TomW, we finally found out what the problem was. The code for the WSDL is created in a system that lets me add XML nodes surrounding the data like below:
<wrap>
<data1></data1>
<data2></data2>
<data3></data3>
</wrap>
instead of:
<data1></data1>
<data2></data2>
<data3></data3>
<data1></data1>
<data2></data2>
<data3></data3>
<data1></data1>
<data2></data2>
<data3></data3>
Therefor, the code generated by Visual Studio in Service Reference did not like the way I wrapped the data with unwanted tags. After removing the tag from my code in the system, I received exactly what I wanted with the code at the top.

Related

How can i edit a HTTP a request C# using fiddlercore

What I want to be able to do: Edit HTTP Requests before they are sent off to the server
User navigates to a webpage of their choice in their browser > They encounter a request they wish to edit > they edit the request and then that gets sent to the server instead of the original one.
What I have done so far: I have captured the request, now I need help finding the code to edit it. Here is my code for capturing the request so far:
Fiddler.FiddlerApplication.BeforeRequest += sess =>
{
//Code to detect user specified URL here
}
Is it possible for me to edit the request before it is actually sent? If it can be done using the FiddlerCore API only then I'd be grateful, although I am willing to download more binaries if required.
Additional notes: I have tried streamwriters, binary writers, copy the respose into a memory stream edit it then copy it back, none of those methods work for me. Also when I try some methods my app just hangs and doesn't respond to things like pressing the X.
Maybe I'm just bad at explaining what I'm trying to achieve seems the only good answer I have has been about reponses :/
If the request reads the string "hello world" then I'd like the user to be able to change the REQUEST to say "hello there"
Such a noobish mistake I made, I thought that RequestBody was read only! Turns out I could have simply edited the response like this:
session.RequestBody = myBytes;
Really annoyed at myself for this!
In the demo app, adding the delegate is shown as:
Fiddler.FiddlerApplication.BeforeResponse += delegate(Fiddler.Session oS) {
// Console.WriteLine("{0}:HTTP {1} for {2}", oS.id, oS.responseCode, oS.fullUrl);
// Uncomment the following two statements to decompress/unchunk the
// HTTP response and subsequently modify any HTTP responses to replace
// instances of the word "Microsoft" with "Bayden". You MUST also
// set bBufferResponse = true inside the beforeREQUEST method above.
//
//oS.utilDecodeResponse(); oS.utilReplaceInResponse("Microsoft", "Bayden");
};

best practise for ASP.NET Lifecycle in regards to data loading?

I have a question regarding the best practises in terms of data loading with ASP.NET and C#
As an overview, we have a web page that uses a supporting web service to obtain an fairly large XML file. That file is then translated into client side JavaScript for use of loading ListViews.
protected string clientSideJavaScripts;
protected void Page_Init(object sender, EventArgs e)
{
// code goes here to submit a request and receive an XML Response from the web service
// code goes here to set clientSideJavaScripts = translated Java Script from the XML Document
// other code goes here
}
The ASP file has something along the lines of:
<head>
<title></title>
<script type="text/javascript>
<%=clientSideJavaScripts%>
</scrpit>
</head>
...
However additionally there are several buttons on the form that cause postbacks. These are not directly related to the javascript above.
Everything works well, however I believe (with my limited knowledge of the ASP.NET lifecycle), that that webservice is getting called every postback. So every time anyone clicks on of the few asp:buttons on the webpage, it is doing the overhead of loading that file regardless of the nature of the button, since a postback will cycle through the stages of the lifecyce from Init up to and past the postback event handling.
The obvious thing to do would be to not call the webservice on postback. I.E:
protected string clientSideJavaScripts;
protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// code goes here to submit a request and receive an XML Response from the web service
// code goes here to set clientSideJavaScripts = translated Java Script from the XML Document
}
// other code goes here
}
However the result of that is that the server side string 'clientSideJavaScripts' no longer gets instanciated, and therefore client side no longer gets the required javascript after tje first postback.
I am sure that I am missing an obvious (and probably common) method to deal with the persistancy of the javascript in a case like this, however I don't know of it and therefore I am looking to anyones advice regarding this. What would be the best practise to ensure that the Webservice is only called once?
I am also using AJAX and JQuery Mobile, if that makes any difference
Any advice would be greatly appreciated.
Many thanks

Google Finance, how to get the JSON data streamed?

I tried to explain this earlier, but obviously failed!
So, if you have a google finance graph open, for instance:
http://www.google.com/finance?q=INDEXNASDAQ:.IXIC
I would like to somehow use the (HttpWebRequest) object in C# so that I can grab the small data which google sends to the page to update the graph.
A friend mentioned this was JSON?
I was trying to use the following code example, but even when i set the keep alive property to 'true', it still wouldnt work:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.keepalive.aspx#Y369
You also need to change the example's line that sets the Connection property to Close. Comment out this line (along with keeping the keep-alive property set to true):
myHttpWebRequest2.Connection = "Close";
You do that and your example should run fine.
Regarding getting the data and using HttpWebRequest to work with it, you can do that. The data returned isn't JSON - it looks like straight text and I'm guessing Google's javascript is parsing it out. (I haven't inspected the javascript on Google Finance's page, but that's my guess.)
Using Fiddler, the response from this URL:
http://www.google.com/finance/getprices?q=.IXIC&x=INDEXNASDAQ&i=120&p=10m&f=d,c,v,o,h,l&df=cpct&auto=1&ts=1307994768643
looks like this:
EXCHANGE%3DINDEXNASDAQ
MARKET_OPEN_MINUTE=570
MARKET_CLOSE_MINUTE=960
INTERVAL=120
COLUMNS=DATE,CLOSE,HIGH,LOW,OPEN,VOLUME
DATA=
TIMEZONE_OFFSET=-240
a1307994120,2641.12,2641.12,2639.96,2640.01,0
1,2638.76,2642.14,2638.76,2641.13,0
2,2638.95,2640.54,2638.74,2638.79,0
3,2639.85,2640.01,2638.08,2638.95,0
4,2640.07,2640.87,2639.31,2639.88,0
5,2640.31,2640.48,2639.42,2640.08,0
6,2641.09,2641.09,2640.3,2640.31,0
A little cryptic, but you can see how the COLUMNS line lines up with the data at the bottom. Also, the f querystring parameter seems to be indicating which columns to return (d=date, c=close,v=volume,o=open,h=high,l=low).
EDIT: I should mention that the URL I used is being sent from the finance graph page to get updated data - you can see this URL being requested at regular intervals using a tool like Fiddler. The response data that I pasted above is also output by the sample application from MSDN.
But commenting out that one line in the example from MSDN and a little fiddling with Fiddler should give you the data and clues you need to parse the return that comes from that URL.
I hope this helps!
PS - my first line in my modified MSDN example looks like this:
HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create("http://www.google.com/finance/getprices?q=.IXIC&x=INDEXNASDAQ&i=120&p=10m&f=d,c,v,o,h,l&df=cpct&auto=1&ts=1307994768643");
I made a similar change to the other WebRequest call a little further down in the example...other than that, I didn't change anything else in the example.

How do i catch page cannot load message?

Hi i hope someone can help,
In basic terms i am trying to stop the embedded browser, in my Windows Forms app, from navigating to the 'This program cannot display the webpage' page and instead display my own error page.
The C# application is a Web Browser embedded in my Windows Forms, its purpose is allowing the user to click on the provided buttons that navigate the browser to predefined WebService URL's. If the Service is not running on the Machine then instead of the browser saying that it was unable to load i need to to navigate to my custom page instead.
I have looked around and as yet had no luck in finding a solution apparently HttpStatusCodes are a way to go but i have no idea how to use them.
Code Snippet:
private void currentMachineToolStripMenuItem_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://localhost:2021/wsservice/status");
}
As you can see, currently very simple program.
You can try this:
Private void WebBrowser1_NavigateError(Object sender, EventArgs e)
{
WebBrowser1.Navigate( App.Path + "\retry.htm");
}
Taken from this link -
You would need to use an HttpWebRequest and an HttpWebResponse object to do
this. Create the HttpWebRequest with the desired URL, using the
WebRequest.Create(url) method. Then use the GetResponse() method to get the
HttpWebResponse. The HttpWebResponse will have the status code returned from
the server. This will tell you if the URL exists or not. 404 indicates "Page
not Found." 200 indicates "Success." Etc.
http://msdn.microsoft.com/en-us/library/8y7x3zz2(vs.71).aspx
Also another link which might help you - http://www.vcskicks.com/check-website.php
You can check the page content for a word that indicates that the page did not load correctly
if(webBrowser1.Document.InnerHtml.Contains("error"))
You can check too the returned document url, find the resource address that webBrowser use to show error page:
if (webBrowser1.Document.Url.ToString().Contains("res://ieframe.dll/dnserrordiagoff.htm") )
...

Silverlight 3 WCF Service `CommunicationException` Server returned error: NotFound

I have a Silverlight 3 application, which 95% of the time is successfully requesting data from a WCF Service (in the same webapp) and displaying it.
This happens infrequently, usually if I hit the service a bunch of times quickly, but sometimes it'll happen on a single lone request.
Every once in a while, if I request a lot of transactions in a short period, I get one of two exceptions, they both occure in the Reference.cs file in the EndMyMethod(System.IAsyncResult result).
There are a few methods, and the exceptions occure on any number of them. The first one, is a TimeoutException() which I understand and it makes sense, the second one, which I totally don't get is the "CommunicationException() was unhandled by user code: The remote server returned an error: NotFound."
I've put try..catch blocks both arround the .MyMethodAsync() and in the handler for MyMethodCompleted both to no avail, as the exception occurs in the generated Reference.cs file.
Any help is greatly appreciated.
update
Reference.cs -- generated by "Add Service Reference"
public System.IAsyncResult BeginTogglePicked(string ID, string toggle, System.AsyncCallback callback, object asyncState)
{
object[] _args = new object[2];
_args[0] = ID;
_args[1] = toggle;
System.IAsyncResult _result = base.BeginInvoke("TogglePicked", _args, callback, asyncState);
return _result;
}
public void EndTogglePicked(System.IAsyncResult result)
{
object[] _args = new object[0];
// This is the line where the Exception is Thrown
base.EndInvoke("TogglePicked", _args, result);
}
Calling Code -- pickedIDs is a list of Strings, and userIDSelecting is a string defined at the top of the procedure. The Event Handler mdc_TogglePIckedCompleted is empty at the moment.
MapDataClient mdc = new MyDataClient();
mdc.TogglePickedCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(mdc_TogglePickedCompleted);
foreach (string id in pickedIDs)
{
mdc.TogglePickedAsync(id, userIDSelecting, mdc);
}
Update 2
This is the "InnerException" from the CommunicationException: System.Net.WebException: The remote server returned an error: NotFound.
Not sure if this is any more helpful, since it doesn't give any extra details. As I said, this happens intermitently not every time I call a service method. I'd also like to point out that the same call will work sometimes and not others, I'm starting to think this issue is because IIS is failing to respond to my service calls, thoughts?
Update 3
When I mean intermiently, I mean truel intrmitent. This may only occur a single time in a user's session, and it may only occur on one of fifty sessions. Its not an all-or-nothing sitation. The calling application is hosted within the same "webite" as the WCF Service, so I don't think a clintaccesspolicy.xml is the issue, but I could be wrong.
The message that you are getting are probably a red herring :-(
When internal WCF service exceptions are thrown, these will ALWAYS manifest themselves as Server Not Found exceptions in the Silverlight UI. This is because the HTTP response is of type 500. The best article I read on this was from David Betz - http://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2 (this was written for SL2, but the concepts still holds for SL3. Also, some of his approaches are for purists - e.g. "NEVER" using the Add Service Reference features from VS - you don't have to follow all his advice ;-) )
Anyway, back to your question, you need to convert the response type to 200 and parse the exception in the message. This can be done using a MessageInspector (in the service and SL app).
There are quite a few articles on how
to do this on the net:
http://www.liquidjelly.co.uk/supersearch/?q=silverlight%20messageinspector&lang=en-GB.
A working example can be downloaded
from CodePlex:
http://code.msdn.microsoft.com/silverlightws/Release/ProjectReleases.aspx?ReleaseId=1660
(download link at the bottom of the
page "Message Inspectors")
Some of these approaches can seem quite daunting - take some time to understand this - the concept is crucial for WCF <--> SL applications, and it makes sense once you get it :-)
We've used this with a lot of success since the start of the year, so if you need anymore help with this just let me know.
Can I recommend always, always having Fiddler running when you are working with Silverlight and WCF?
Is your service returning exception details to the client? By default it does not. You could add the following attribute to your service class.
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class MyService ...
You may find you're getting some kind of server-side exception that is not visible to the client.
Make sure you have a clientaccesspolicy.xml file. Otherwise you may get that error cause the policy file cannot be found.
I had exactly the same problem as you - absolute nightmare, it would work sometimes and then just stop.
After reading your post earlier I kept looking for clientaccesspolicy info and found this (can't remember where), but I use it and it now works fine!
Hope the same is good for you :) My file was missing the extra detail on the allow-from section.
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="http://*" />
<domain uri="https://*" />
</allow-from>
<grant-to>
<resource include-subpaths="true" path="/"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>

Categories