I have a simple ASP.NET Web Service:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace WebService1
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Get()
{
member mem = new member();
mem.hello = "hello hello hello";
mem.kalon = "fafasdf";
mem.name = "wfasdfawe";
return new JavaScriptSerializer().Serialize(mem);
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
public class member
{
public string hello { get; set; }
public string name { get; set; }
public string kalon { get; set; }
}
}
I'm facing problem to retrieve the string from SilverLight by using WebClient. Can you enlighten me? I uses the below code to get the string from the WebService.
WebClient wc = new WebClient();
public MainPage()
{
InitializeComponent();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("http://www.MyWeb.com/Service1.axms/Get");
wc.DownloadStringAsync(uri);
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
MessageBox.Show(e.Error.ToString());
MessageBox.Show(e.Result);
}
And I receive error:
System.Security.SecurityException ---> System.Security.SecurityException: Security error. at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest.<>c_DisplayClass5.b_4(Object sendState) at System.Net.Browser.AsyncHelper.<>c_DisplayClass4.b_1(Object sendState) --- End of inner exception stack trace --- at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
I have tried this too, still getting the same error:
Uri uri = new Uri("http://www.MyWeb.com/Service1.axms?op=Get");
wc.DownloadStringAsync(uri);
I am currently not sure what is missing.
-------------------------------------------------------
Update 1
Hi, special thanks to Abhinav S. I can now access the web service by configuring the cross domain and using another method to access the service after reading the links provided by Abhinav S.
Thanks You, Very Much !
Another question:
How to access the web service in my computer? I running a website at http://localhost:3042/ and want to access the service at http://www.myWeb.com/
I have written 2 new xml files (clientaccesspolicy.xml and crossdomain.xml) and put it at the same directory as the web service.
The content of clientaccesspolicy.xml:
="1.0"="utf-8"
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
and another xml crossdomain.xml:
="1.0"
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type"/>
</cross-domain-policy>
When I access the service (http://www.myWeb.com) from http://localhost:3043/Test.aspx
I receive this error message:
An error occurred while trying to make a request to URI 'http://www.societyhubs.com/sv/Service1.asmx'. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details.
What is the next step to configure, to enable the access from localhost?