Problems communicating with WCFHost (Silverlight)

Problems communicating with WCFHost (Silverlight)

Old forum URL: forums.lhotka.net/forums/t/6349.aspx


Jack posted on Thursday, February 05, 2009

I am trying to run my first Silverlight App and I'm running into issues communicating with my WCF host.

I have the rolodex solution up and running and I mostly tried cut/paste from there.

I also have a WCFHost website running for a winforms test app using the same libraries and I can do my data retrieval through that.

My first attempt was to add a new Silverlight ServiceReference so I could share.  I struggled and ended up with two services, one on wsHttpBindings and another with basicBindings as Silverlight wouldn't support the other.

I had been trying to create a service reference with the built in Add Service Reference dialogs but that seemed to create a lot of compile time issues so I reverted to creating a new wcf website and copied/pasted the serviceReference.ClientConfig from Rolodex.

I am able to open the site and I can even generate a config from the svcutil.exe app which looks almost just like the Rolodex one.

However as soon as I try to retrieve my data I get the following error:

An error occurred while trying to make a request to URI 'http://localhost:1686/EpmWCFHostSilverlight/WcfPortalSilverlight.svc'. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. Please see the inner exception for more details.

{System.Security.SecurityException ---> System.Security.SecurityException: Security error.
   at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
   at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)}

Any ideas?

thanks
jack

RockfordLhotka replied on Thursday, February 05, 2009

The most common source of trouble here is that Visual Studio keeps switching the default startup project to the Silverlight client app, rather than the hosting web app.

You must have your startup app be the web app, otherwise you'll end up going "cross domain" to two different portal on localhost.

Jack replied on Thursday, February 05, 2009

It’s not that – I double checked a few times.  I’m getting the Silverlight App opening and I can debug a few lines of code.  I’m basically trying to simulate what the CompanyEditor.axml page is doing except I hard-coded my id#.

 

  void Page_Loaded(object sender, RoutedEventArgs e)

        {

            CslaDataProvider provider = this.Resources["DataEntrySessionDataProvider"] as CslaDataProvider;

            provider.FactoryParameters.Add(4609);

            provider.FactoryMethod = "GetDataEntrySession";

            provider.Refresh();

        }

 

As soon as I do the Refresh() it jumps to here:

 

public Csla.WcfPortal.WcfResponse EndFetch(System.IAsyncResult result) {

                object[] _args = new object[0];

                Csla.WcfPortal.WcfResponse _result = ((Csla.WcfPortal.WcfResponse)(base.EndInvoke("Fetch", _args, result)));

                return _result;

            }

 

 

 

From: RockfordLhotka [mailto:cslanet@lhotka.net]
Sent: Thursday, February 05, 2009 1:01 PM
To: jaddington@alexandergracie.com
Subject: Re: [CSLA .NET] Problems communicating with WCFHost (Silverlight)

 

The most common source of trouble here is that Visual Studio keeps switching the default startup project to the Silverlight client app, rather than the hosting web app.

You must have your startup app be the web app, otherwise you'll end up going "cross domain" to two different portal on localhost.



skagen00 replied on Thursday, February 05, 2009

I was ramming my head against a wall with a problem communicating to the host too. When I was establishing my projects based off the silverlight ones, I set the assembly name for each project (in properties of the project) to be what I was used to- CompanyName.Application.Client and CompanyName.Application.Server (for my client/silverlight class library and my server class library, respectively).

These need to be the same!! CompanyName.Application.Business is the route I took.

It's hardly obvious but I hope that's your problem!

Chris

 

Jack replied on Thursday, February 05, 2009

I solved my issue but I do not really understand how.  Any explanation would be appreciated.

 

What I did was take the wcfHostWeb project from Rolodex and copied it into my solution.  I tweaked the port so it didn’t conflict with Rolodex and it is working…

 

So what is the difference?  I believe I created a website vs. a webapp or is it just a webservice using WCF?  I don’t really have a clue after banging my head so much– I’m using the WCF website I copied from PTracker for a winapp test window and the wcf proxy works fine.

 

If I had a WPF and a Silverlight implementation should I have separate WCF project/site?  Or can I have two different services?

 

I don’t even want to start having to think about what the different binding options are… I had wsHttp for nonSilverlight and now basicHttpBindings for Silverlight. 

 

Too many new things J

 

jack

 

From: skagen00 [mailto:cslanet@lhotka.net]
Sent: Thursday, February 05, 2009 2:31 PM
To: jaddington@alexandergracie.com
Subject: Re: [CSLA .NET] RE: Problems communicating with WCFHost (Silverlight)

 

I was ramming my head against a wall with a problem communicating to the host too. When I was establishing my projects based off the silverlight ones, I set the assembly name for each project (in properties of the project) to be what I was used to- CompanyName.Application.Client and CompanyName.Application.Server (for my client/silverlight class library and my server class library, respectively).

These need to be the same!! CompanyName.Application.Business is the route I took.

It's hardly obvious but I hope that's your problem!

Chris

 



RockfordLhotka replied on Thursday, February 05, 2009

I don’t know what all you did, so can’t offer an explanation.

 

The rules are relatively simple though.

 

Silverlight can talk to the site it came from with no extra effort.

 

If it needs to talk to another site, that site must have a cross domain policy file to allow the communication.

 

This is true for WCF, sockets, HTTP or any other server communication from Silverlight, and is a very necessary security precaution.

 

In development environments this can be confusing, because it is easy to forget that each instance of Cassini is a different site – http://localhost:1234 is different from http://localhost:6543.

 

And because Visual Studio will create a temporary website to host/debug your Silverlight app if the app is set as the startup project, it is terribly easy to accidentally end up in a cross domain scenario you didn’t expect. I realize you said this wasn’t your issue – but it is the most common issue.

 

The Rolodex app has two web sites: the one to host the SL app, and a separate one to host the server-side data portal components. The data portal host has a clientaccesspolicy.xml file to allow the SL app to interact with it. This was a conscious choice in implementing Rolodex, because it illustrates what I think will be a very common (perhaps the most common) scenario, where the web server and application server are separate machines.

 

Rocky

Jack replied on Friday, February 06, 2009

I definitely didn’t have the clientaccesspolicy.xml file or crossdomain.xml in the beginning but I did try copying them over – I just thought since I had the WCF working for the proxy from a winapp it would be a piece of cake J

 

I will likely not worry too much about it until it becomes time to deploy and then concentrate on making sure I understand.

 

Maybe it was the server address thing, although I was aware of it from the forum, I had the Rolodex working fine, and I was checking my taskbar to see the addresses.  Like anything when you are frantic and banging your head it’s likely that just starting from scratch would have done the trick.  It gets so confusing when you have 3 web.configs open at once plus alt-tab back to everything in VS for Rolodex and google + the CSLA forums open J.

 

Going forward – If I am using the WCF proxy from different UI’s, can they all communicate through the same webapp or is that just asking for trouble?

 

Thanks

 

jack

 

From: Rockford Lhotka [mailto:cslanet@lhotka.net]
Sent: Thursday, February 05, 2009 8:21 PM
To: jaddington@alexandergracie.com
Subject: RE: [CSLA .NET] RE: Problems communicating with WCFHost (Silverlight)

 

I don’t know what all you did, so can’t offer an explanation.

 

The rules are relatively simple though.

 

Silverlight can talk to the site it came from with no extra effort.

 

If it needs to talk to another site, that site must have a cross domain policy file to allow the communication.

 

This is true for WCF, sockets, HTTP or any other server communication from Silverlight, and is a very necessary security precaution.

 

In development environments this can be confusing, because it is easy to forget that each instance of Cassini is a different site – http://localhost:1234 is different from http://localhost:6543.

 

And because Visual Studio will create a temporary website to host/debug your Silverlight app if the app is set as the startup project, it is terribly easy to accidentally end up in a cross domain scenario you didn’t expect. I realize you said this wasn’t your issue – but it is the most common issue.

 

The Rolodex app has two web sites: the one to host the SL app, and a separate one to host the server-side data portal components. The data portal host has a clientaccesspolicy.xml file to allow the SL app to interact with it. This was a conscious choice in implementing Rolodex, because it illustrates what I think will be a very common (perhaps the most common) scenario, where the web server and application server are separate machines.

 

Rocky



RockfordLhotka replied on Friday, February 06, 2009

The Silverlight data portal and .NET data portal are not the same. You can host both of them in the same virtual root, but they require different endpoints, and probably different WCF bindings.

 

The Silverlight data portal uses the MobileFormatter, and is designed specifically to support Silverlight clients. There is no .NET proxy for interacting with this type of host.

 

The .NET WcfHost in the data portal uses the NetDataContractSerializer, which doesn’t exist in Silverlight. It supports Windows clients (of all interface types), but won’t work with a Silverlight client.

 

Silverlight only works with the basicHttp binding in WCF, so the Silverlight WcfHost must use this binding.

 

Normally in .NET you’d use wsHttp binding because it offers more capabilities around security. I typically use this binding for Windows clients. basicHttp will work fine too, but you lose the more modern feature options.

 

Rocky

 

From: Jack Addington [mailto:cslanet@lhotka.net]
Sent: Friday, February 06, 2009 7:46 AM
To: rocky@lhotka.net
Subject: RE: [CSLA .NET] RE: Problems communicating with WCFHost (Silverlight)

 

I definitely didn’t have the clientaccesspolicy.xml file or crossdomain.xml in the beginning but I did try copying them over – I just thought since I had the WCF working for the proxy from a winapp it would be a piece of cake J

 

I will likely not worry too much about it until it becomes time to deploy and then concentrate on making sure I understand.

 

Maybe it was the server address thing, although I was aware of it from the forum, I had the Rolodex working fine, and I was checking my taskbar to see the addresses.  Like anything when you are frantic and banging your head it’s likely that just starting from scratch would have done the trick.  It gets so confusing when you have 3 web.configs open at once plus alt-tab back to everything in VS for Rolodex and google + the CSLA forums open J.

 

Going forward – If I am using the WCF proxy from different UI’s, can they all communicate through the same webapp or is that just asking for trouble?

 

Thanks

 

Jack

 

Copyright (c) Marimer LLC