Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a simple ASP.NET Web Service:

C#
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
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [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.

C#
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:

C#
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:

XML
<?xml version="1.0" encoding="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:

XML
<?xml version="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?
Posted
Updated 30-Apr-12 1:13am
v2

1 solution

 
Share this answer
 
Comments
adriancs 30-Apr-12 7:16am    
Hey, dude. Thanks you very much for the hint. My problems is solved now. The service can be accessed. I have updated my question in which about access the web service from localhost. Can you please give some more guide? I have extended my question above. Really thanks for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900