Skip to main content

Microsoft Silverlight

Answered Question How To Call WebServiceRSS Feed

(0)

fasihakbar590
fasihakb...

Member

Member

2 points

6 Posts

How To Call WebService

Hi all, I want to call a web service of dotnet 2.0 in silverlight beta 2. But receiving TargetInvocationException.

Anyone help plz. 

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP

Re: How To Call WebService

Hi,

can you post some code?

Have a nice day,

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

fasihakbar590
fasihakb...

Member

Member

2 points

6 Posts

Re: How To Call WebService

This is my Webservice code:

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

public Service () {

//Uncomment the following line if using designed components 

//InitializeComponent(); 

}

[WebMethod]

public string HelloWorld() {

return "Hello World 2.0";

}


}

 

  And This is my  code to call webservice in my test.xaml.cs : 

  
void ListingControl_Loaded(object sender, RoutedEventArgs e)
{
            BasicHttpBinding bind = new BasicHttpBinding();
            EndpointAddress endpoint = new EndpointAddress("http://localhost:4289/WebService2.0/Service.asmx");
            Service2.ServiceSoapClient asmx = new CADWEb.Service2.ServiceSoapClient();
            asmx.HelloWorldCompleted += new EventHandler(asmx_HelloWorldCompleted);
            asmx.HelloWorldAsync();
}
void asmx_HelloWorldCompleted(object sender, CADWEb.Service2.HelloWorldCompletedEventArgs e)
        {
            tbTest.Text = e.Result.ToString();
        }
 Note : May be you would say that cross domain policy should be applied because of changing domain of service and website. I have used clientaccesspolicy.xml and crossdomain.xml but still receiving the same exception. But both of these xml files helped for the web service calling on Dot Net 3.5.

 Thanks,

M.Fasih Akbar

mr.saif
mr.saif

Member

Member

417 points

147 Posts

Re: How To Call WebService

Did ClientAccesspolicy.xml include http-request-headers="*" attribute in allow-from element. bcz it is mendaory now in silverlight 2 beta 2.

here is complete xml for clientAccesspolicy.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>

Regards,
Muhammad Saifullah

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP

Re: Re: How To Call WebService

Hi,

as reported by mr.saif, it's a cross-domain issue.

Also take a look at http://silverlight.net/forums/t/21222.aspx

Have a nice day,

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

fasihakbar590
fasihakb...

Member

Member

2 points

6 Posts

Re: Re: How To Call WebService

Note: I have to call the WebService (ASMX)  of Dot Net 2.0 in the SilverLight Beta 2 Version

Actually I received two Exceptions :

1) ProtocolException (404 not found)

and then :

2) TargetInvocationException

These exceptions came only when calling a webservice(asmx) of dot net 2.0 . I mean of older version.And I need to call the said service of older version.

Your reply is fine for the webservice of Dot Net 3.5 WebService (ASMX) and my clientaccesspolicy.xml is same as posted by Mr.Saif and I have used the crossdomain.xml file its code is as under.

<!DOCTYPE cross-domain-policy SYSTEM "http://localhost:4289/WebService2.0/Service.asmx">

<cross-domain-policy>

<allow-http-request-headers-from domain="*" headers="*"/>

</cross-domain-policy>

 

My WebService of dot net 3.5 (ASMX) is working fine with crossdomain policy files ......but I am again putting stress to call of dot net 2.0.

Now I think you may be clear about my problem.

Thanks.

M.Fasih Akbar

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP

Re: Re: Re: How To Call WebService

Hi,

I've tested your code targeting .net 2.0 under VS2008, and it correctly works with Silverlight using these clientaccesspolicy.xml and crossdomain.xml files inserted in the root of the project.

ClientAccessPolicy.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>

CrossDomain.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="*"/>
</cross-domain-policy>

 

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

pmkr
pmkr

Member

Member

17 points

31 Posts

Re: How To Call WebService

Hi,

I think you need to do some settings to solve this error. Go to your webservice  propoerties and click on web. In the server tab select "Use IIS webserver". Set project url and create virtual directory. I hope this helps. Let me know if you have difficulty.

 Thanks 

 

sladapter
sladapter

All-Star

All-Star

17181 points

3,133 Posts

Answered Question

Re: How To Call WebService

Not all 404 error results from cross-domain issue. Not all services need to have clientaccesspolicy.xml (or crossdomain.xml) file either. Depending on the setup and how you call it. 

First where is your Web service? If the service is under the same Web project/Web Site as your HTML/ASPX page that host the Silverlight. You do not necessarily need clientaccesspolicy.xml if you set your service call correctly.

First let us be clear what is a cross-domain call:

Case1: Both service and page running under Dev Server

Page URL: http://localhost:1234/SilverlightTestPage.html

Service URL : http://localhost:1234/Service.svc 

This is not a cross-domain call.

Case 2: Both page and service running under IIS under the same web

Page URL: http://localhost/MyWeb/SilverlightTestPage.html

Service URL : http://localhost/MyWeb/Service.svc 

this is not a cross-domain call.

Case 3:  Both page and service running under Dev Server

Page URL: http://localhost:1234/SilverlightTestPage.html

Service URL : http://localhost:2345/Service.svc 

This is a cross-domain call because the Port number is different.

When you have situation like this, usually is because you have the Service under a Web Project other than the Web project that host your page. In this case, you need to have clientaccesspolicy.xml be placed under the Service project folder while you debug using Dev Server. That folder is the Service domain root under Dev server. After deploy to IIS, you need the clientaccesspolicy.xml under wwwroot folder.

Case 4: Page running under Dev Server, but calling service under IIS

Page URL: http://localhost:1234/SilverlightTestPage.html

Service URL : http://localhost/MyWeb/Service.svc 

This is a cross-domain call,  clientaccesspolicy.xml file is need to be placed in wwwroot

Case 5: both running under IIS

Page URL: http://MyServer/MyWeb/SilverlightTestPage.html

Service URL : http://localhost/MyWeb/Service.svc 

Even MyServer = localhost, this still is a cross-domain call. clientaccesspolicy.xml file is need to be placed under wwwroot.

Now we know what a cross-domain call is. Next we need to understand where the Service URL is comming from.

By default, when you add a service Reference, a endpoint is added in the ServiceReferences.ClientConfig file under your Silverlight project. The endpoint has a URL. That URL is used by the Dev Server, it has port number in it (something like http://localhost:PortNo/Service.svc) . When you deploy your Service to IIS. You can not use that URL. You need to change it to a real URL before your final build.

While develop, the Port number could also dynamically changed.  When you shut down your Dev Server and restart, it could start with a new Port number (unless you fix it in the Project property page). So in order for the Service URL in sync with the current Port number, you need to update your Service Reference from time to time. Otherwise, you will get 404 error. 

The best way to avoid all these trouble is to put your Service under the same web site as your page when ever you can (unless you are calling 3rd party service). Then build your Service URL dynamically:

Change the code to call your service:

var ws = new MyService.MyServiceClient();  // This is the default way to create the ServiceClient; The URL will be read from the ClientConfig.


System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress(new Uri(Application.Current.Host.Source, "../MyService.svc"));

var ws = new MyService.MyServiceClient(MyServiceConfigurationName, address);

This way, you do not need to mess up with the ClientConfig file to update the service URL. The Service URL built this way is always the same domain call no matter where you run it (either from Dev Server or IIS). You do NOT need that clientaccesspolicy.xml file.

Other possible causes for the 404 error:

 1) Service throw a exception,  but you did not catch it in your Service code.

Because Silverlight currently does not support FaultException. So any uncatched exception in your service code will result a 404 error instead of a meanigingfull error. The best way to avoid this is put try/catch code in the service code. Either log that error or pass that error message back to Silverlight using a Out parameter in your Service function, so you have a way to know what is going on in your Service in case something went wrong.

Your service function Code:

public YourObject DoWork(SomeParams, out string ServiceError)

{

     try

    {

        ServiceError = null;

        ...//your code
      }

      catch(Exception err)

     {

          ServiceError = err.Message;

     }


In Silverlight code, you can check e.Error and e.ServiceError in your ServiceCallCompleted event handler.

 2) The data passed between Silverlight and Service exceed certain size. For example, when you pass a string larger than 8K chars back to Service, you will get a 404 error. You need to change the default configuration to handle large data.

See this thread for more information if you know it's data size issue: http://silverlight.net/forums/p/21513/76994.aspx#76994

 

 

 

 

 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

fasihakbar590
fasihakb...

Member

Member

2 points

6 Posts

Answered Question

Re: How To Call WebService

Thanks

For Your Help All of the people who participated for my problem.

Especially to Mr.Sladapter . U r great Sladapter.

Your cross domain policy detailed description helped me greatly and will help alot to others who will face this issue.

I am greatful to u again.

I have also click the hyperlink "Mark as answer" for U (Sladapter). Confirm the mark then let me know if it is not marked for ur points.

Thanks To All,

M.Fasih Akbar

sladapter
sladapter

All-Star

All-Star

17181 points

3,133 Posts

Re: How To Call WebService

 You need to mark the post that helped you solve your problem as answer . Not necessarily the final post.

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

societymedia
societym...

Member

Member

5 points

2 Posts

Re: How To Call WebService

fasihakbar590:
Especially to Mr.Sladapter . U r great Sladapter.
 

Yes slapadter, you are great! After farting arounding thinking i had a crossdomain prob for about 2 hours i found this thread. The thing i learned is that not all 404s are cross domain... I had an issue in my service code.

Thanks

Do whatever it takes to develop better than the day before!

Roc1
Roc1

Member

Member

41 points

23 Posts

Re: How To Call WebService

sladapter,

Application.Current.Host.Source is returning my xap file -- Thus the endpointAddress = file:///C:/svn/main/trunk/Dashboard/Dashboard/Bin/DashboardData.svc instead of http://localhost ...

Do you have any idea what I am doing wrong?  Thank you, RC

 

sladapter
sladapter

All-Star

All-Star

17181 points

3,133 Posts

Re: How To Call WebService

That's because you are not running your page using Web server. You are using File system. Make sure your Web Project is your start up project (not your Silverlight project) and your Test page is your Start up page. In your browser you should see your page running from http://localhost:xxx/YYY, not File:///C:xxx.

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

Je8
Je8

Member

Member

54 points

50 Posts

Re: How To Call WebService

This is an excellent summary, many thanks. I've just been through the (sometimes very painful) process of calling existing web services from SL2 apps and this information would have made it much quicker proposition.

Best wishes.

Jerry

Roc1
Roc1

Member

Member

41 points

23 Posts

Re: How To Call WebService

sladapter,

You fixed my problem. Thanks for your response!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities