I need to use a JSON REST Service inside my Silverlight Application so that I can display information from the REST service in my Silverlight App. Below is my code but the ClientDownloadStringCompleted() is never stepped into and I'm not sure why. The REST service does have data and is working. I just can't seem to access it so I can parse out the data I need. View of the JSON Service
string jsonlink = "http://hou-prodserver2/ArcGIS/rest/services/sar/ScheduledRig/MapServer/81/query?text=&geometry=&geometryType=esriGeometryPoint&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&objectIds=&where=MAXDATE%3E%271%2F1%2F1900%27&time=&returnCountOnly=false&returnIdsOnly=false&returnGeometry=false&maxAllowableOffset=&outSR=&outFields=MAXDATE&f=pjson";
var client = new WebClient();
client.DownloadStringCompleted += ClientDownloadStringCompleted;
client.DownloadStringAsync(new Uri(jsonlink, UriKind.Absolute));
Below is Method I'm trying to use to get access to the data but it never gets used
private void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
//handle the response.
string result = e.Result;
}
Make sure that service site has one or both of the following files at the root of the site:
crossdomain.xml
clientaccesspolicy.xml
If your Silverlight app is not on the same exact domain as the service, those files need to exist to allow it to connect. If they don't Silverlight fails silently.
Related
We are using WCF and Data contracts - I am not understanding how to send a request to the application layer for a specific set of data. I want to get data for a file, but I can't get it. It is not http or Webrequest either. Please help.
I have tried Webrequest and many other options and there is nothing online to help. We are using an N-tier architecture and I am trying to figure out how to send requests to the application layer from the UI so that I can get the data sent back to me.
// generate summary of the Project Selected, if it is index 0 (CNS 41) retrieve file from application layer
// otherwise, retrieve the file for CJ74
private void btnGenerateSummary_Click(object sender, RibbonControlEventArgs e)
{
if(PMDropDown.SelectedItemIndex == 0)
{
//send request to application layer for CNS41
SapFileInfo request = new SapFileInfo();
SapFileInfo response = request.getResponse();
}
else
{
//send request to application layer for CJ74
}
}
I want to get a response from the application layer with the data from the database, which I will write into a file that will go into excel and I will format it using open XML 2.5.
I have been contemplating on a dilemma for hours. I have a Visual Studio Solution that contains a WCF, WebForms, UWP, Xamarin and a SharedLibrary Projects.
I intend to use the WCF project as the backend which talks to the database and process Email and SMS integration and feed the other apps.
OPTION A
Currently, The WCF is hosted on an Azure App Service which makes it accessible via POST, GET, etc from the url which is: https://mywcfprojectlink.azurewebsites.net/service1.svc/GetUsers
With such arrangements, I can perform a POST request to get data from the apps:
string response = string.Empty;
string url = "https://mywcfprojectlink.azurewebsites.net/service1.svc/GetUsers";
try
{
var values = new Dictionary<string, string>
{
{ "data", Encryption.EncryptString(dat.ToString()) } //dat is incoming method param
};
string jsonString = JsonConvert.SerializeObject(values);
var cli = new WebClient();
cli.Headers[HttpRequestHeader.ContentType] = "application/json";
response = cli.UploadString($"{url}", jsonString);
var result = JsonConvert.DeserializeObject<string>(response);
topic.InnerText = Encryption.DecryptString(result.ToString());
}
catch (Exception)
{
return string.Empty;
}
The method above is a simple one as I have other ones where I Deserialize with Models/Classes.
OPTION B
I equally have access to the methods defined in service1 by adding the project reference to my WebForms which surprisingly is also compatible with xamarin but not with UWP. Nevertheless, I am interested in the WebForms scenario. Below is an example method:
using BackEnd;
//Service1 service1 = new Service1();
//var send = service1.GetUsers(dat.ToString()); //dat is incoming method param
//topic.InnerText = send;
Obviously, using the Option B would eliminate the need to encrypt, decrypt, serialize or deserialize the data being sent. However, I have serious performance concerns.
I need to know the better option and if there is yet another alternative (probably an Azure Resource), you can share with me.
If you decide to use https endpoint of the Azure website, option A is secure because of SSL encryption. So you don't have to encrypt/decrypt it by yourself. The only tip is to create a proper authorization mechanism. For example use TransportWithMessageCredential. An example is provided in below article https://www.codeproject.com/Articles/1092557/WCF-Security-and-Authentication-in-Azure-WsHttpBin
Ok, maybe this is the dumbest question ever but I really can't understand what's happening :D
I have this simple code:
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri url = new Uri("http://www.something.com/");
WebClient wc = new WebClient();
wc.DownloadStringAsync(url);
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string result = e.Result;
}
It works well, except for one case (obviously the one that I need), when the address of the server is "ts4.travian.it". In this case I get this error: "The remote server returned an error: NotFound."
The strange thing is that if I write a small console application with basically the same code, it works...any idea?
EDIT: To be more specific, the server return error 403 - Forbidden, but if I try the same code in a Console Application, it works perfectly...don't know what to think..I'm debugging the application on a Nokia Lumia 800
I found the problem finally. The Silverlight framework automatically set the Referer header of the HttpWebRequest, and the server "ts4.travian.it" refused it because it was not correct.
Did you forget to provide the output for that one case?
Edit:
Status code '403' implies that there' something the server doesn't like in your app. Who knows maybe it checks your UA string and sees that you're using IE Mobile i guess. Than he desides not to allow you see the content. Hm?
Ok, its been a while since I've worked with a Web References. I need a refresher. I think I have about 80% of the code I need to get a response going but I'm missing something. Maybe you can help me :)
Given:
A web method called GetSomething in the list of methods when pointing to a .wsdl url.
This produces a few classes/objects:
GetSomethingRequest
GetSomethingCompletedEventHandler
GetSomethingCompletedEventArgs
myComplexType
Which I use to create this code:
void someMethodToTestResponse()
{
GetSomethingRequest request = new GetSomethingRequest();
// fill in the request
request.myComplexType.Property1 = "Blah";
request.myComplexType.Property2 = "Kachoo";
GetSomethingCompletedEventHandler handler = GetSomethingCompleted_Response;
//.... ok now what?
//handler.Invoke(???)
// at this point I'm supposed to send an object for source (request maybe?)
// and a new instance of GetSomethingCompletedEventArgs but that class is
// asking for stuff that makes me think that is not the right idea.
}
void GetSomethingCompleted_Response(object source, GetSomethingCompletedEventArgs args)
{
// get the result
var result = args.Result;
}
What am I doing wrong? What am I missing? Thanks in advance.
You don't need web service source codes. The web service can be implemented in Java. Creating service reference woks the same, as we really don't know what is on the other side.
So, try Add Service Reference in VS2008 and enter the url to working web service. VS will examine the wsdl on server and generate needed classes for you.
From than on, you just call the service as some ordinary method call. Meaning you don't have to fiddle with requests and http and such details. All that is hidden from you. Except in app.config where many WCF settings can be changed.
Ok, I figured out that I needed to find a Service type class. See this SO Post where it mentions:
private com.nowhere.somewebservice ws;
The issue was that the class they provide wasn't intellisensing for me and I figured it wasn't what I was looking for.
Here is how I would solve my problem:
blah.webservice.SomeMainServiceClass service = new SomeMainServiceClass();
GetSomethingRequest request = new GetSomethingRequest();
// fill in the request
request.myComplexType.Property1 = "Blah";
request.myComplexType.Property2 = "Kachoo";
object myResponse = service.GetSomething(request);
Ok, here is the situation. I would like to deploy a silverlight application to an enterprise portal. Users will access the application by logging in to the portal and navigating to the page where it is hosted. That's the easy part.
This silverlight 4 application is designed to be run in Out of Browser mode (OOB). My question is, is it possible to have the Silverlight OOB update process retrieve updates from behind the enterprise portal's authentication?
When I call App.Current.CheckAndDownloadUpdateAsync();, how do I supply credentials so that this HTTP request will succeed?
Any ideas? Is the update process extensible?
Thanks for your help.
With Silverlight 4 this should be a possible scenario
In both classes WebClient and WebRequest you can use Credentials..
private void DownloadAdditionalThings()
{
WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);
var client = new WebClient();
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username", "password");
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://blog.gfader.com/"));
}
private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string result = e.Result;
}