Skip to main content

Microsoft Silverlight

Unanswered Question Silverlight and WCFRSS Feed

(0)

jelly
jelly

Member

Member

6 points

5 Posts

Silverlight and WCF

I noticed from Scott Guthrie's blog that he claims that Silverlight can be used to call WCF web services hosted within ASP.NET.  I think the critical part of this statement must be that the web service must be hosted within ASP.NET since I can't find any way of directly interacting with WCF from Silverlight due to the subset of the CLR available.  Has anyone else tried to do this, or know where there might be a nice example of doing this?

 A bit later...

OK, I've managed to make some progress.  I can create a WCF service which is hosted within ASP.NET.  I can create a service reference and call the service from a Windows Forms application, so I'm happy that the service is running OK.  However, I can't call the service from Silverlight.  If I add it as a web reference and then try to call my method on it I get a (very helpful) error which states 'Error invoking service'.  If I create a BrowserHttpWebRequest http://localhost/test/test.svc/Hello, or try the same thing in a browser, the response I get is:

 

- <Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none">
-   <Code>
      <Value>Sender</Value>
-     <Subcode>
        <Value xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:DestinationUnreachable</Value>
   </Subcode>
 </Code>
-   <Reason>
    <Text xml:lang="en-US">The message with To 'http://localhost/test/test.svc/Hello' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.</Text>
 </Reason>
</Fault>

I think this is something to do with the SOAP headers not being created correctly?

Has anyone else seen this problem, or have any ideas how to fix it?
 

Mike Rousos [MSFT]
Mike Rou...

Member

Member

4 points

2 Posts

Re: Silverlight and WCF

This is possibly due to the fact that, for security, Silverlight pages can only call web services that live on the same host as they do. For example, I have a web service I host at http://MikesComputer/... I can only call webservices there if the Silverlight page calling them lives on http://MikesComputer as well. Also, in my experience, referring to the web reference as localhost is not good enough. The Silverlight page will need to refer to the host's name so that Silverlight can be sure that it's the same as its own host.

In my own coding, I fixed this by removing my web reference to http://localhost webservices and adding references to those same web services as http://MikesComputer (which works because that's also the server hosting my Silverlight page).

Hope that this helps!
Mike

jelly
jelly

Member

Member

6 points

5 Posts

Re: Silverlight and WCF

I don't think that's the problem since I've tried both localhost and the machine name.  I can also access 'normal' webservices all right, this only becomes a problem when I try and access a WCF service (using a .svc file).

 John

Zhenlan Wang
Zhenlan ...

Member

Member

80 points

35 Posts

Re: Silverlight and WCF

You need to make the WCF service using JSON as communication data format.

jelly
jelly

Member

Member

6 points

5 Posts

Re: Silverlight and WCF

There doesn't appear to be a lot of information on how to do this!  I assume that it's either a matter of attributing the service or editing the configuration file?

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Silverlight and WCF

hello again.

sorry, wrong link. i think this is what you want:

http://blogs.msdn.com/mwinkle/archive/2007/02/28/wcf-and-wf-in-quot-orcas-quot.aspx

 

jelly
jelly

Member

Member

6 points

5 Posts

Re: Silverlight and WCF

I'm not sure that I'm going about this in the right way.  I assume that if I am hosting my WCF service in IIS then the configuration settings are held in the web.config file?  I've attempted to edit my web.config to match the example in the link above and the closest I've got (that doesn't cause errors) is:

<?xml version="1.0"?>
<configuration>
  <
system.serviceModel>

    <bindings>
      <
webHttpBinding>
        <
binding name="jsonBinding" messageEncoding="Json" />
      </
webHttpBinding>
   
</bindings>

    <behaviors>
      <
endpointBehaviors>
        <
behavior name="jsonBehavior">
          <
enableWebScript/> 
        </
behavior>
      </
endpointBehaviors>
      <
serviceBehaviors>
        <
behavior name="myBehavior">
          <
serviceMetadata httpGetEnabled="true"/>
        </
behavior>
      </
serviceBehaviors>
    </
behaviors>

    <services>
      <
service behaviorConfiguration="myBehavior" name="MyService">
        <
endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <
endpoint address="" binding="webHttpBinding" bindingConfiguration="jsonBinding" behaviorConfiguration="jsonBehavior" contract="IMyService" />
      </
service>
    </
services>

  </system.serviceModel>

</configuration>

However, this then has no effect and I continue to get my error that the service cannot be processed at the receiver, due to an AddressFilter mismatch at the EndPoint dispatcher.  It suggests that I should check that the sender's and receiver's EndPoint addresses agree.  How would I be able to do that?

suyog kale
suyog kale

Member

Member

188 points

98 Posts

Re: Silverlight and WCF

hi jelly,

i am also trying to call WCF service from Silverlight application, WCF service hosted on same domain,

problem is when i return large data from Service it gives me MaxRecivedMessageSize is greater than 64Kb

so can you help me if you found any help, i tryed to set  MaxRecivedMessageSize in web.config as well as app.config but it stilll not working

plz guide me, its very urgent for me 

(If this has answered your question, please click on "Mark as Answer")
Thanks & Rgds,
Suyog Kale
Senior Software Engineer
************************************************************

Bill Reiss
Bill Reiss

Contributor

Contributor

4818 points

913 Posts

Silverlight MVP

Re: Silverlight and WCF

You're really probably better off waiting for the Beta, since you'll probably have to rewrite it anyway, and the issues you're running into may be fixed in the Beta. It's not like you can deploy a 1.1 application, so you might as well wait.


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

HossamZain
HossamZain

Member

Member

2 points

1 Posts

Re: Re: Silverlight and WCF

you just need to add clientaccesspolicy.xml to your WCF service

check :  http://spellcoder.com/blogs/hossam/archive/2008/05/12/13475.aspx

 

MyMindsOwn
MyMindsOwn

Member

Member

52 points

22 Posts

Re: Silverlight and WCF

You don't need to connect using JSON serialization, SOAP will work just fine, but you can't use the ChannelFactory to communicate with your service, you must add a service reference. You might be having some cross domain issues, judging from the "DestinationUnreachable" message. To be sure that's not it you can follow the instructions from one of the previous responses.

Jeff Cao - MSFT
Jeff Cao...

Member

Member

201 points

29 Posts

Re: Re: Silverlight and WCF

Do note that we currently only support BasicHttpBinding on Silverlight side.

(please "mark as answer" if this post answered your question. Thank you!)

Jeff Cao
Software Engineer | Silverlight Web Services Team | Microsoft

vijaynrm
vijaynrm

Member

Member

70 points

10 Posts

Re: Re: Re: Silverlight and WCF

This seems to be a cross-domain issue with silverlight.

A silverlight control hosted at http://MyMachine/mycontrol.aspx can only access services on that same domain by default , for example:  http://MyMachine/service.svc, not a service at http://OtherMachine/service.svc.

To enable this, the service must explicitly opt-in to allow cross-domain access, for that we need to add a clientaccesspolicy.xml file at the root of the domain where the service is hosted to configure the service to allow cross-domain-access

clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from>
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>


For more details :
http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx

- Vijay Narayan 

 

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities