Configuring Csla DataPortal / WCF / Endpoints

Configuring Csla DataPortal / WCF / Endpoints

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


esaulsberry posted on Friday, August 21, 2009

I'm wondering what the right way is to set up the WCF configuration for a Silverlight project. I've got it working with a reference to the dev box in ServiceReferences.ClientConfig, but of course I have to put a different address in and recompile when deploying.

<endpoint address="http://localhost/AgService/WcfPortal.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding_IWcfPortal"
contract="Csla.WcfPortal.IWcfPortal" name="CslaWcfPortal" />

I've tried setting the end point at startup, but I don't seem to get the pieces just right and of course the call fails.

Uri cslaEndpointUri = new Uri(Application.Current.Host.Source, "/AgService/WcfPortal.svc");
Csla.DataPortalClient.WcfProxy.DefaultUrl = cslaEndpointUri.AbsoluteUri;

sergeyb replied on Friday, August 21, 2009

You actually do not need to recompile this reference. Once you have created your XAP file for deployment and deployed it to the target server, you can just unzip the content of the XAP file (you can rename it to .zip extension first), update your ClientConfig, then zip it back in, and rename the file back to XAP extension. This is a typical deployment procedure anyway because you do not know in advance what the server address is.

Sergey Barskiy
Principal Consultant
office: 678.405.0687 | mobile: 404.388.1899

Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

-----Original Message-----
From: esaulsberry [mailto:cslanet@lhotka.net]
Sent: Friday, August 21, 2009 4:52 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Configuring Csla DataPortal / WCF / Endpoints

I'm wondering what the right way is to set up the WCF configuration for a Silverlight project. I've got it working with a reference to the dev box in ServiceReferences.ClientConfig, but of course I have to put a different address in and recompile when deploying.

contract="Csla.WcfPortal.IWcfPortal" name="CslaWcfPortal" />

I've tried setting the end point at startup, but I don't seem to get the pieces just right and of course the call fails.

Uri cslaEndpointUri = new Uri(Application.Current.Host.Source, "/AgService/WcfPortal.svc");
Csla.DataPortalClient.WcfProxy.DefaultUrl = cslaEndpointUri.AbsoluteUri;

esaulsberry replied on Friday, August 21, 2009

Idealy I'd be able to pick it up at run time. Thanks for the tip though.

RockfordLhotka replied on Monday, August 24, 2009

If you want it at runtime you may need to have the host web page provide the value to the SL runtime via script.

In other words, the web server could render the address into the web page so the web page has the correct value - and it would then use script to provide that value to the SL runtime.

esaulsberry replied on Monday, August 24, 2009

Thanks for responding.

I can get the address pushed down into the SL app, it's how to override the config settings on the portal that's causing me trouble.

I've been trying variations on this to do it but I haven't been successfull.
private void SetCslaBinding()
{
string endpointFromWeb = HtmlPage.Window.Invoke("GetEndpointHost", null) as string;
ServerHost = endpointFromWeb;
Uri cslaEndpointUri = new Uri(Application.Current.Host.Source, "/AgService/WcfPortal.svc");

Csla.DataPortalClient.WcfProxy.DefaultUrl = cslaEndpointUri.AbsoluteUri;
System.ServiceModel.BasicHttpBinding cslaBinding = new System.ServiceModel.BasicHttpBinding();
cslaBinding.Name = "basicHttpBinding_IWcfPortal";
cslaBinding.MaxBufferSize = int.MaxValue;
cslaBinding.MaxReceivedMessageSize = int.MaxValue;

Csla.DataPortalClient.WcfProxy.DefaultBinding = cslaBinding;
Csla.DataPortalClient.WcfProxy.DefaultEndPoint = "/AgService/WcfPortal.svc";
}

RockfordLhotka replied on Monday, August 24, 2009

Is there a bug in WcfProxy that is preventing that from working? The intent is that you can set the properties explicitly and override the config file values.

esaulsberry replied on Monday, August 24, 2009

More likely I'm doing something wrong. Don't know what though.

Bah. Now I know what it is. Nothing like having to explain the problem for seeing it.

What I was doing was providing the wrong value for
Csla.DataPortalClient.WcfProxy.DefaultEndPoint = "CslaWcfPortal";

The value I was giving was more the address. What threw me I think was I expected to have to supply an object that provided the information that's in the config file for the endpoint, such as the contract, etc. It's always blindingly obvious... once you figure it out.

Thanks
Elton

Jack replied on Tuesday, August 25, 2009

Elton,

Would you mind summarizing for a FAQ how you implemented this?  I'd like to put something together such that I can push the selection of the environment to log into down to the first page the user sees.

Thanks

jack

esaulsberry replied on Tuesday, August 25, 2009

The way I push a string from the html to the client is like this:
I have a hidden field on the page that contains the SliverLight control.


This is populated when the page loads:
protected void Page_Load(object sender, EventArgs e)
{
hdnEndpoint.Value = Request.ServerVariables["HTTP_HOST"].ToString();
}
There is a JavaScript function to provide this to the SilverLight app:

function GetEndpointHost() {
var hostField = document.getElementById('hdnEndpoint');
if (hostField) {
return hostField.value;
}
else {
return ('no host');
}
}

There's probably a way to do this just with JavaScript, but I was unwilling to spend any time finding it.

In the startup of the SilverLight app (App.xaml.cs):
public partial class App : Application
{
public static string ServerHost { get; set; }
...
private void Application_Startup(object sender, StartupEventArgs e)
{
ControlContainer cont = new ControlContainer();
LoginPage lp = new LoginPage(cont);
cont.SetContent(lp);
this.RootVisual = cont;

SetCslaBinding();

}

private void SetCslaBinding()
{
string endpointFromWeb = HtmlPage.Window.Invoke("GetEndpointHost", null) as string;
ServerHost = endpointFromWeb;
Uri cslaEndpointUri = new Uri(Application.Current.Host.Source, "/AgService/WcfPortal.svc");

Csla.DataPortalClient.WcfProxy.DefaultUrl = cslaEndpointUri.AbsoluteUri;
System.ServiceModel.BasicHttpBinding cslaBinding = new System.ServiceModel.BasicHttpBinding();
cslaBinding.Name = "basicHttpBinding_IWcfPortal";
cslaBinding.MaxBufferSize = int.MaxValue;
cslaBinding.MaxReceivedMessageSize = int.MaxValue;

Csla.DataPortalClient.WcfProxy.DefaultBinding = cslaBinding;
Csla.DataPortalClient.WcfProxy.DefaultEndPoint = "CslaWcfPortal";
}


I think it was unnecessary for me to pull down the server name from the html page, as Application.Current.Host.Source seems to provide it as well.

Uri cslaEndpointUri = new Uri(Application.Current.Host.Source, "/AgService/WcfPortal.svc");

In your case, this would probably all move to something that responded to the user's choice...

Hope this helps.

Jack replied on Tuesday, August 25, 2009

Thank you.

-----Original Message-----
From: esaulsberry [mailto:cslanet@lhotka.net]
Sent: August-25-09 9:37 AM
To: jaddington@alexandergracie.com
Subject: Re: [CSLA .NET] RE: Configuring Csla DataPortal / WCF / Endpoints

The way I push a string from the html to the client is like this:
I have a hidden field on the page that contains the SliverLight control.


This is populated when the page loads:
protected void Page_Load(object sender, EventArgs e)
{
hdnEndpoint.Value =
Request.ServerVariables["HTTP_HOST"].ToString();
}
There is a JavaScript function to provide this to the SilverLight app:

function GetEndpointHost() {
var hostField = document.getElementById('hdnEndpoint');
if (hostField) {
return hostField.value;
}
else {
return ('no host');
}
}

There's probably a way to do this just with JavaScript, but I was unwilling
to spend any time finding it.

In the startup of the SilverLight app (App.xaml.cs):
public partial class App : Application
{
public static string ServerHost { get; set; }
...
private void Application_Startup(object sender, StartupEventArgs e)
{
ControlContainer cont = new ControlContainer();
LoginPage lp = new LoginPage(cont);
cont.SetContent(lp);
this.RootVisual = cont;

SetCslaBinding();

}

private void SetCslaBinding()
{
string endpointFromWeb =
HtmlPage.Window.Invoke("GetEndpointHost", null) as string;
ServerHost = endpointFromWeb;
Uri cslaEndpointUri = new Uri(Application.Current.Host.Source,
"/AgService/WcfPortal.svc");

Csla.DataPortalClient.WcfProxy.DefaultUrl =
cslaEndpointUri.AbsoluteUri;
System.ServiceModel.BasicHttpBinding cslaBinding = new
System.ServiceModel.BasicHttpBinding();
cslaBinding.Name = "basicHttpBinding_IWcfPortal";
cslaBinding.MaxBufferSize = int.MaxValue;
cslaBinding.MaxReceivedMessageSize = int.MaxValue;

Csla.DataPortalClient.WcfProxy.DefaultBinding = cslaBinding;
Csla.DataPortalClient.WcfProxy.DefaultEndPoint =
"CslaWcfPortal";
}


I think it was unnecessary for me to pull down the server name from the html
page, as Application.Current.Host.Source seems to provide it as well.

Uri cslaEndpointUri = new Uri(Application.Current.Host.Source,
"/AgService/WcfPortal.svc");

In your case, this would probably all move to something that responded to
the user's choice...

Hope this helps.

Copyright (c) Marimer LLC