Tim
Tim

Reputation: 20360

How can I use web service from Silverlight app?

I am trying to get data form a web service inside a silverlight app. Unfortunately the silverlight app (Bing map app) just hangs when trying to connect.

I use the same code in a console app and it works just fine.

Is there anything special I need to do in silverlight to get it to work? I don't get any exceptions - it just hangs.

I based my service and client code off of this example http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication

Problems and Questions:

1. Why can't I set breakpoints in my sliverlight code?

2. How can I successfully call WCF service from a silverlight app? (links to SIMPLE working examples would be great - all the ones I seem to find seem to be quite advanced (RIA, Duplex, etc) Many of these also show xml and other non C# "code" - frankly I don't know what those do and how they relate to the projects, code and services.

(Clearly I am quite ignorant about WCF and silverlight)

As per request for code:

[ServiceContract]
public interface ILGSMapServer
{
    [OperationContract]
    List<double> GetLatitudes();
}    


public class TreeWorkClient
{ 
ChannelFactory<ILGSMapServer> httpServer;        
public ILGSMapServer httpProxy;        

public TreeWorkClient()
{
    httpServer = new ChannelFactory<ILGSMapServer>(new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/GetLatitudes"));
    httpProxy = httpServer.CreateChannel();
}        

public List<TreeWorkItem> GetLocations()
{
    List<double> lats = httpProxy.GetLatitudes();


    //...  do stuff in code
    return ret;
}

}

Upvotes: 0

Views: 903

Answers (3)

Valentin Kuzub
Valentin Kuzub

Reputation: 12093

I believe you need to have this attribute on WCF service for SL to consume it:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

As for debugging - you can debug Silverlight, try using IE for that, its most natural browser for SL debugging (sadly).

Once you start debugging it will be more clear whats wrong when you catch cross domain exception or some other.

Upvotes: 0

MerickOWA
MerickOWA

Reputation: 7612

Given your example code you should be seeing the

System.InvalidOperationException: The contract 'ILGSMapServer' contains synchronous operations, which are not supported in Silverlight. Split the operations into "Begin" and "End" parts and set the AsyncPattern property on the OperationContractAttribute to 'true'. Note that you do not have to make the same change on the server.

You'd need to change your service contract to the following

  [ServiceContract]
  public interface ILGSMapServer {

    [OperationContract( AsyncPattern = true )]
    IAsyncResult BeginGetLatitudes( AsyncCallback callback, object context );
    List<double> EndGetLatitudes( IAsyncResult result );
    }

This also means you'll need to do something completely different in your GetLocations() function as this function will return before the results from the Web have been returned.

Try taking a look at the examples here.

Other options involve using the "Add Service Reference" rather than manually defining it in code.

Upvotes: 1

Mike Goodwin
Mike Goodwin

Reputation: 8880

I agree with John Saunders - it would be easier to answer this if you published the client code.

However as a guess, a common problem with calling services from Silverlight applications is the restriction Silverlight puts on cross domain calls.

In summary, if your service is at a different domain from the site-of-origin of the Silverlight application, you need to create a client access policy file at the service location.

See this for details:

http://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx

Upvotes: 2

Related Questions