Using relative URLs to access WCF services in Silverlight

If you are developing a silverlight application with a WCF service, chances are that you will encounter a Service Not Found error due to any of the following reasons:

  • The VS Development Server port changed
  • You deployed your application on IIS for testing.
  • You deployed your application on any other computer.

Now there are two apparent choices:

  • Modify the URL in ServiceReferences.ClientConfig file
  • Use “relative” URLs instead of directly instantiating the service client.

The first solution is to modify ServiceReferences.ClientConfig file and correct the URL of the service. However, this is difficult once the application is deployed since the config file is contained inside the .xap file. I will present the second solution here.

Replace you service client instantiation (the following code):

 MyServiceClient svc=new MyServiceClient();

with a method call GetMyServiceClient() as:

 MyServiceClient svc = GetMyServiceClient();

And define GetMyServiceClient() as:

private MyServiceClient GetMyServiceClient()
  Binding binding = new System.ServiceModel.BasicHttpBinding();
  EndpointAddress endpoint = new EndpointAddress(
	new Uri(Application.Current.Host.Source, "../MyService.svc"));
  MyServiceClient service = new MyServiceClient(binding, endpoint);
  return service;

The above code dynamically builds the service URL from application path. It is a good practice to have service initialization code in one place and you can also control certain other settings (e.g. max buffer size, timeout settings) for your service in that method.

If you need to use the configurations from ServiceReferences.ClientConfig file and modify just the url, you may use another overload that takes the configuration name as parameter:

private MyServiceClient GetMyServiceClient()
  EndpointAddress endpoint = new EndpointAddress(
	new Uri(Application.Current.Host.Source, "../MyService.svc"));
  MyServiceClient service = new MyServiceClient("CustomBinding_MyService", endpoint);
  return service;

SSRS: Another way to use DataSet fields in page header/footer

I previously blogged about a method to refer to DataSet fields in page header/footer here. This time, I will present another way to achieve this.

Although SSRS does not allow us to use DataSet fields in page headers but it allows us to refer to report items. So we could place a textbox (that takes its value from a DataSet field) anywhere in our report’s body and set its Hidden property to true. Then, we could easily refer to that textbox in the page header with an expression like: =ReportItems!TextBox1.Value and we are done. Note that the textbox that is being referred should be present on every page, or otherwise the header will print empty value.

SSRS: Using DataSet fields in page header/footer

SQL Server Reporting Services does not allow you to refer to DataSet fields in page header/footer. Sometimes, you really need this feature. For example, You are displaying some data related to a Project and the Project is to be selected from the user via a report parameter. Now typically, you want to display the Project Name in the header of every page. Here’s a hack on accomplishing this:

  • Create a dataset dsProjectName in which you select the desired information (in this case, the ProjectName column) of the selected Project. A simple query for this could be:
    Select ProjectName from Project where ProjectID = @ProjectID
  • Create a new internal parameter ProjectName, set its default value to get the data from dataset dsProjectName and then set the value field to ProjectName. Also, make sure this new parameter is below the ProjectID parameter to allow cascading.

Adding a parameter

You can now use this new ProjectName paramater anywhere in your page header textbox expressions.