Skip to main content

Microsoft Silverlight

Unanswered Question Using WCF service in Silverlight applicationRSS Feed

(0)

adii_Sathye
adii_Sathye

Member

Member

30 points

20 Posts

Using WCF service in Silverlight application

I would like to know how can I use WCF service in my Silverlight Alpha 1.1 .NET application? Any samples or pointers would be really helpful.

Reply.

mikeb123
mikeb123

Member

Member

100 points

47 Posts

Re: Using WCF service in Silverlight application

you can call a wcf service but you can't host one

kieron
kieron

Member

Member

4 points

2 Posts

Re: Using WCF service in Silverlight application

 Hi, I don't suppose you know of any sample code to actually do that?

DotNetAddict
DotNetAd...

Member

Member

80 points

53 Posts

Re: Using WCF service in Silverlight application

At the moment, because Silverlight lacks a true XML document implementation (it will get one when support for LINQ to XML's XDocument arrives), and there is no XML serialization support either, your best bet for consuming a WCF service is to make it a POX (Plain Old XML) service. In this fashion, you can simply hit the web service with BrowserHttpWebRequest and open a stream, which contains XML, that you can then process with an XmlReader.

 I did this myself when I wrote a Silverlight application that consumes an Astoria (WCF POX service surrounding an ADO.NET Entity Framework Entity Data Model) service. You can read the blog entry about that experience here:

 http://dotnetaddict.dotnetdevelopersjournal.com/silverlight_astoria.htm

The .NET Addict
http://dotnetaddict.dotnetdevelopersjournal.com

mikeb123
mikeb123

Member

Member

100 points

47 Posts

Re: Re: Using WCF service in Silverlight application

This talk shows an example calling a wcf service.

http://silverlight.net/learn/learnvideo.aspx?video=144

kieron
kieron

Member

Member

4 points

2 Posts

Re: Re: Using WCF service in Silverlight application

Thanks for the quick response guys, much appricated! 

andy.wang
andy.wang

Member

Member

104 points

58 Posts

Re: Using WCF service in Silverlight application

http://silverlight.net/QuickStarts/Remote/UsingREST.aspx 

As you said, we can use webservice like this case to read remote XML,right? But i have tried it and return an exception,"You can not access cross-domain by BrowserHttpWebRequest ". what's the problem. i have fouced on this problem several days. can you explain what's the problem is? Or can you show me an correct way to access remote XML files?

 

thanks

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Using WCF service in Silverlight application

hello.

well, the problem is that the current version only allows local web service calls. if you try to make a cross-domain call, you'll face the exception you mention.

andy.wang
andy.wang

Member

Member

104 points

58 Posts

Re: Using WCF service in Silverlight application

hi, what did you mean the local web service call? I host my web service on my own machine, but i still can not call it. I added web reference http://localhost:4244/WebService1.asmx  (this is a regular asp.net web service) to silverlight project. But the the exception occured.

Buy  the way, the web service you meant was the Ajax web service or regular web service? Since there is no people in our team is familiar with Ajax, so we don't like to use Ajax and javascript. If we can use C# to solve the problem, it's the best case for us.

 

Thanks

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Using WCF service in Silverlight application

hello.

what i'm saying is that the page and the service must be in the same application. If your web service is on a site that is running on a different port, it think that call is a cross-domain call.

xlj1000
xlj1000

Member

Member

26 points

13 Posts

Re: Re: Using WCF service in Silverlight application

There seems a MIX sample called "Fantansy Baseball" that demos how to call WCF service.

No you don't need to understand AJAX & javascript to consume an AJAX webservice in c#. All you need in Ocars is creating a regular web service , and uncommenting one line of code.

 Thanks

andy.wang
andy.wang

Member

Member

104 points

58 Posts

Re: Using WCF service in Silverlight application

I think my code is in the same domain and same application.

My step is :

1. create a silverlight project (this is not a web application)

2. create a asp.net webservice project

3. add webservice as an webreference to silverlight project.

 

Is this mean they are in the same application?

 

thanks

andy.wang
andy.wang

Member

Member

104 points

58 Posts

Re: Re: Using WCF service in Silverlight application

Hello xlj1000

I got the sample you provided and it runs well in my computer, thanks for you knidly help.

By the way, Which kind of that service is? A WCF service? How to create it?

Sorry to ask you such simple question, but i am a beginner.

 

 

thanks

xlj1000
xlj1000

Member

Member

26 points

13 Posts

Re: Using WCF service in Silverlight application

No they are two applications, but it should be fine as long as they are at same domain.(e.g. same server but different virtual directory).
To make a single application, you need to create a web project with the webservice and then import the silverlight project.

andy.wang:

I think my code is in the same domain and same application.

My step is :

1. create a silverlight project (this is not a web application)

2. create a asp.net webservice project

3. add webservice as an webreference to silverlight project.

 

Is this mean they are in the same application?

 

thanks

mikeb123
mikeb123

Member

Member

100 points

47 Posts

Re: Using WCF service in Silverlight application

If you right click on the web site bin folder, you should be have an option to add the silverlight project as a reference (if the silverlight project is in the same solution).VS Orcas takes care of moving the xaml file and the silverlight dll into the web site for deployment and it also keeps changes in sync as well as debugging support from between the web site and the silverlight managed code.

andy.wang
andy.wang

Member

Member

104 points

58 Posts

Re: Using WCF service in Silverlight application

Hello xlj1000

Seems the sample you gave me doesn't work well. The data(player info) is read from hard code instead of web site.

xlj1000
xlj1000

Member

Member

26 points

13 Posts

Re: Using WCF service in Silverlight application

andy.wang:

Hello xlj1000

Seems the sample you gave me doesn't work well. The data(player info) is read from hard code instead of web site.

 

The sample works fine for me.

You need to set the FantasyBaseballWeb as the startup project and run it, instead of the silverlight project called FantasyBaseball.

 Good luck.

andy.wang
andy.wang

Member

Member

104 points

58 Posts

Re: Using WCF service in Silverlight application

FantasyBaseballWeb  is not a service? can it be used to call silverlight project? If so, the result page will be a webservice page,right?

 

xlj1000
xlj1000

Member

Member

26 points

13 Posts

Re: Re: Using WCF service in Silverlight application

My bad.

There are two type of services in the project and by default it is pointed to use the JSON webservice.

Here is the steps to change to use the WCF service:

1. Delete the "localhost" webreference from Fantasybaseball project.

2. Add web reference and pick BaseballData.svc(not BaseballData.asmx)and make sure the webreference name is still "localhost".

Thanks

 

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Using WCF service in Silverlight application

hello.

i think that this won't work. you'll get a reference but it won't compile into the final dll.

xlj1000
xlj1000

Member

Member

26 points

13 Posts

Re: Re: Using WCF service in Silverlight application

luisabreu:

hello.

i think that this won't work. you'll get a reference but it won't compile into the final dll.

You do get a few compile errors, but it can be fixed easily.

In page.xaml.cs:

1. Replace all localhost.BaseballData with localhost.BaseballService.

2. Change:

baseballService.Url = appRoot +"BaseballData.asmx";

 to

baseballService.Url = appRoot +"BaseballData.svc";

 

 

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Using WCF service in Silverlight application

hello again.

I'm not able to make that work. On VS orcas, when I add a reference to a wcf project (even after configuring it to use JSON) i see it there, on the web references folder but the proxy isn't compiled into the final dll.

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Using WCF service in Silverlight application

btw, this is my best attemtp at calling wcf services from a silverlight app:

http://msmvps.com/blogs/luisabreu/archive/2007/06/12/calling-a-wcf-service-from-your-silverlight-application.aspx

xlj1000
xlj1000

Member

Member

26 points

13 Posts

Re: Re: Using WCF service in Silverlight application

luisabreu:

on the web references folder but the proxy isn't compiled into the final dll.

 

Not sure exactly what you mean by that, but my guess is that you will need .net 3.5 version of WCF(comes with orcas).

The Ajaxbinding stuff seem not there in 3.0.

Any Microsoft guy can confirm this?

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Using WCF service in Silverlight application

hello.

yes, i'm using orcas and what i'm saying is that when you add a reference to a web service, you're supposed to get a new type on the final dll that is injected automatically during the compilation process. Now, even though i'm not absolutely positive about this, i think that the current version of VS orcas will only generate the proxy if you're calling an asmx web service.

I think this is only a limitation of the current version of the silverlight slwsdl.exe tool (hope i got the name right) because i was able to build a proxy by hand as I've said in the previous post.

xlj1000
xlj1000

Member

Member

26 points

13 Posts

Re: Re: Using WCF service in Silverlight application

Still not sure what you are trying to say

I did the following simple test ion my VS orcas: 

1. Create a WCF service project and silverlight project in the same solution

2. Do a adding webreference in the silverlight project to the WCF project. I can see the web reference added fine.

I then make a build and check the dll from SL project with reflector, I can see the types from WCF service are there.

BTW, the WCF service in Fantasy Baseball sample works fine. I set a breakpoint inside it and it breaks.

 

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Using WCF service in Silverlight application

hello.

thanks for insisting on this :) if it weren't you, i wouldn't have installed a virtual machine and tested what  you're saying.

so, on VPC installation, i was able to add a reference to the WCF service (which was configured to use JSON) and call it from my C# silverlight code. On the other hand, if you have VS 2005 and Orcas on the same machine, you might not get it working (that was what was happening to me yesterday). In those scenarios, i added the web reference but never got intellisense for the the localhost default namespace nor did i get the compiled proxy on my silverlight dll.

 

adii_Sathye
adii_Sathye

Member

Member

30 points

20 Posts

Re: Re: Re: Using WCF service in Silverlight application

Can i get a very detailed step by step guide on how to call a Web/WCF service from silverlight project.

andy.wang
andy.wang

Member

Member

104 points

58 Posts

Re: Re: Re: Using WCF service in Silverlight application

I have succeded to run a silverlight with seb service. the step is like :

 

1> Create a silverlight project.

2> Create a webapplication priject and create a webservice page in it. This webservice will be used as a proxy.

3> Create a webservice project and define your method in it.

4> Call the later created web service in the first created web service page. please remember to uncomment the following line

[System.Web.Script.Services.ScriptService]  (both the proxy and later webservice)

and add [ScriptMethod(UseHttpGet=true)] in the method which you define to use  (both the proxy and later webservice).

 

5> Call the later web service in the proxy webservice and call the proxy webservice in you silverlight project.

 

Above is the complete step, hope it is helpful.

 

adii_Sathye
adii_Sathye

Member

Member

30 points

20 Posts

Re: Re: Re: Re: Using WCF service in Silverlight application

Can you please describe on how you added the reference to your proxy service in your Silverlight application. I am still facing the same problem Sad.

Did u hosted your project in IIS?

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Re: Re: Using WCF service in Silverlight application

hello.

regarding the previous post, i believe those steps are ony necessaryf because the wcf service is on a remote host. if the wcf and silverlight page are hosted on the same web app you do not need to do that because the 3.5 beta version is already capable of using JSON with wcf services.

using wcf from your silverlight app is simple of the service and the silverlight control are on the same web app. in those cases, just add a reference to the web service by using the add web reference option that is show when you right click your silerlight project on VS

adii_Sathye
adii_Sathye

Member

Member

30 points

20 Posts

Re: Re: Re: Re: Re: Using WCF service in Silverlight application

On the same app means: two projects (Silverlight and Web application) in the same solution? or something else?

I added reference to my web service in silverlight project by right clicking and selecting Ad web reference. but, it doesn't work. I am figuring out why!

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Re: Re: Re: Using WCF service in Silverlight application

helllo.

same project means that the silverlight page (the page that hosts the control) and the wcf service end up on the same project. your scenario is the normal one. the problem is that when you add a reference, youi might end up with an incorrect url (url that points to the internal serveer). in that case, you should set the URL of the proxy from code.

adii_Sathye
adii_Sathye

Member

Member

30 points

20 Posts

Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

In my case I have deployed both my Silverlight applicaiton and Webservice in IIS inside same virtual directory as my Silverlight page at : http://localhost:35629/CallWebService/TestPage.html

and Web service at : http://localhost:35629/CallWebService/WebService1.asmx

So now both are at same static port, then it should work fine. but its not working here.

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

hello.

you're using asmx service. did you configure the web.app and added the attributes to the service to that it uses json?

adii_Sathye
adii_Sathye

Member

Member

30 points

20 Posts

Re: Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

Do u mean these attributes :

[System.Web.Script.Services.ScriptService]

[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]

ya i have put those above the webservice and web method

anything else you want me to check to make sure everything is correct

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

hello again.

did you change the default asmx handler on the web.config file?

adii_Sathye
adii_Sathye

Member

Member

30 points

20 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

What are the changes to be made and where? can you please describe on it.

andy.wang
andy.wang

Member

Member

104 points

58 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

<webServices>
   <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
   </protocols>
  </webServices>

 

add the code above in your config file, it will be OK

adii_Sathye
adii_Sathye

Member

Member

30 points

20 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

It gives me syntax error while putting that code above in file.

I added the protocols tag inside the

<system.web.extensions>

<scripting>

 

<webServices>

Then i get the following error while invoking the service

Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Unrecognized configuration section system.web.extensions/scripting/webServices/protocols.

 

 

andy.wang
andy.wang

Member

Member

104 points

58 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

put it under <system.web>

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

hello.

you need to add this to the <configSections> element:

<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
   <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
    <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
    <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
     <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
     <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
     <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
    </sectionGroup>
   </sectionGroup>
  </sectionGroup>

 

and then you need to add this to the <httpHandlers> element:

<remove verb="*" path="*.asmx"/>
 <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

btw, the easiest way to get the correct config file is to download the ajax asp.net installer and then create a new ajax asp.net application

adii_Sathye
adii_Sathye

Member

Member

30 points

20 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

I have already got the correct config file with me.

anything else I need to look into?

adii_Sathye
adii_Sathye

Member

Member

30 points

20 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Using WCF service in Silverlight application

Now i am not getting the cross domain call exception, now the exception is "Error invoking service".

Does anybody know the solution for it?

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities