Page view counter
Silverlight and WCF
Last post 05-14-2008 2:33 AM by vijaynrm. 13 replies.
Sort Posts:
05-15-2007 5:08 AM
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?
 

jelly

Loading...
Joined on 05-15-2007
Posts 5
05-15-2007 6:59 PM
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

Mike Rousos [MSFT]

Loading...
Joined on 05-07-2007
Posts 2
05-16-2007 3:35 AM
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

jelly

Loading...
Joined on 05-15-2007
Posts 5
05-16-2007 4:11 AM
Re: Silverlight and WCF

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

Zhenlan Wang

Loading...
Joined on 05-01-2007
Redmond, WA
Posts 35
05-16-2007 4:50 AM
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?

jelly

Loading...
Joined on 05-15-2007
Posts 5
05-16-2007 5:29 AM
luisabreu

Loading...
Joined on 04-30-2007
Posts 612
05-16-2007 5:34 AM
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

 

luisabreu

Loading...
Joined on 04-30-2007
Posts 612
05-16-2007 6:47 AM
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?

jelly

Loading...
Joined on 05-15-2007
Posts 5
02-25-2008 10:39 PM
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
************************************************************

suyog kale

Loading...
Joined on 08-02-2007
pune-india
Posts 96
02-25-2008 11:00 PM
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 2
My blog (rss feed)

Bill Reiss

Loading...
Joined on 05-01-2007
Tampa, FL
Posts 848
05-12-2008 11:28 AM
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

 

HossamZain

Loading...
Joined on 05-12-2008
Posts 1
05-12-2008 4:15 PM
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.

MyMindsOwn

Loading...
Joined on 09-01-2005
Posts 16
05-13-2008 3:09 PM
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

Jeff Cao - MSFT

Loading...
Joined on 05-13-2008
Seattle
Posts 24
05-14-2008 2:33 AM
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 

 

 

vijaynrm

Loading...
Joined on 11-22-2007
Kochi, India
Posts 10
Microsoft Communities