views:

95

answers:

2

Is it possible to make these relative?

for example: webservice.asmx

rather then

http://servername/webservice.asmx

A: 

You can write your own code to pull out the server path of the current service, and prepend it to the service you want to use.

John Fisher
+2  A: 

You need to write custom code to do this. There are two parts of the custom code: 1) The ASP.Net Silverlight Host site needs to pass the Silverlight Application the service address through an Initial Parameter; 2) The Application Start-Up event needs to process the initial parameters to point the service to the correct address.

Part 1 Code on the ASP.Net Host Site. There are 3 steps here:

A) Add a web.config value for containing the service name

<appSettings>
  <clear/>
  <add key="MyServiceName" value="MyService.svc"/>        
</appSettings>

B) Get the webpage base address in in the page load event, append the service address to it, and save it to a page level variable

    // Page level variable for initial parameters
    public string InitParams { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        var serviceAddress =
            ConfigurationManager.AppSettings["MyServiceName"];

        var baseAddress = Request.Url.GetLeftPart(UriPartial.Authority);

        var fullAddress = string.Format("{0}/{1}", baseAddress, serviceAddress);

        // Pass parameters to SilverLight Application 
        InitParams = string.Format(
            "{0}={1}",
            "ServiceAddress",
            fullAddress);
    }

C) In the page that hosts the silverlight control set the initial to the page level variable as defined in B.

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="ClientBin/MySilverlightProject.xap"/>
    <param name="onError" value="onSilverlightError" />
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="3.0.40624.0" />
    <param name="autoUpgrade" value="true" />
    <param name="InitParams" value="<%=InitParams%>" />
    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&amp;v=3.0.40624.0" style="text-decoration:none">
      <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
    </a>
     </object>

The key here being the

<param name="InitParams" value="<%=InitParams%>" />

Part 2 Hook the App start up event and initialize your service to the address in the App.xaml.cs file.

private void Application_Startup(object sender, StartupEventArgs e)
        {
            string serviceAddress = e.InitParams["ServiceAddress"];
            // INSERT CODE TO INITIALIZE YOUR SERVICE HERE
        }
Gus
Useful answer, very helpful!
masenkablast