views:

1308

answers:

3

I am writing a Silverlight app calling WCF service.

Another solution contains following projects for Silverlight:
- Web project for hosting silverlight app
- Silverlight app project
- Silverlight class library project with service reference to WCF

When i run the Silverlight app on my locahost, the Silverlight invokes WCF using localhost and works fine.

Then i published the service and web app and deployed it to another server. The web.config file was modified to point to the deployed endpoint address.

Now , running the Silverlight app looks up the localhost url of the service, eventhough the endpoint in web.config is that of the deployed server.

From where does the silverlight app lookup the svc url? It doesn't seem to read it from the web.config file. But, seems more like the url was embedded into the assembly during build/publish process.

Any thoughts??

Thanks for reading!

+4  A: 

The silverlight app does NOT look at the web.config of the hosting server at all - that is on the server side and not visible to the silverlight app which is running on the client. The Silverlight app looks in its own ServiceReferences.clientconfig file or at the URL that you specify programmatically when you create the local service proxy in code.

So, you have 2 options:
1. Modify ServiceReferences.clientconfig before you build the deployable version of the Silverlight app.
2. Use code to construct your client-side endpoints with URL's.

We use the 2nd option because we like to have a standard programmatic interface that configures our endpoints. We do something like this (but not with the MaxValue's if it is a public facing service, of course):


        public ImportServiceClient CreateImportServiceClient()
        {
            return new ImportServiceClient(GetBinding(), GetServiceEndpoint("ImportService.svc"));
        }

        public ExportServiceClient CreateExportServiceClient()
        {
            return new ExportServiceClient(GetBinding(), GetServiceEndpoint("ExportService.svc"));
        }

        protected override System.ServiceModel.Channels.Binding GetBinding()
        {
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
            binding.MaxBufferSize = int.MaxValue;
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.SendTimeout = TimeSpan.MaxValue;
            binding.ReceiveTimeout = TimeSpan.MaxValue;
            return binding;
        }

        protected EndpointAddress GetServiceEndpoint(string serviceName)
        {
            if (Settings == null)
            {
                throw new Exception("Service settings not set");
            }
            return
                new EndpointAddress(ConcatenateUri(Settings.ServiceUri,serviceName));
        }

        protected EndpointAddress GetServiceEndpoint(string serviceName, Uri address)
        {
            if (Settings == null)
            {
                throw new Exception("Service settings not set");
            }
            return new EndpointAddress(ConcatenateUri(address, serviceName));
        }

The classes like "ImportServiceClient" and "ExportServiceClient" are the generated proxies from creating service references to our WCF services. Settings.ServiceUri is where we store the address of the server that we should be talking to (in our case it is set dynamically via parameters to the silverlight plugin in the page it is hosted in, but you could use whatever scheme you like to manage this address).

But if you prefer to simply tweak ServiceReferences.ClientConfig then you don't need any code like this.

Clay Fowler
@Clay: Thanks dude! it worked.
pencilslate
A: 

The code seems to be good man (actually i have not tried it) but under which class i need to write the code snippet.

I have a published website which is using some hosted WCF services on the based of needed functionalities.

I want to use the same functionality in silverlight application which is putted on some page.

See your code

protected EndpointAddress GetServiceEndpoint(string serviceName, Uri address) { if (Settings == null) { throw new Exception("Service settings not set"); } return new EndpointAddress(ConcatenateUri(address, serviceName)); }

if i am writting this code in web application than i can take read the key of appsettings where i have defined my endpoint address but how do i read my appsettings key on silverlight application. I also need my Binding protocals to be changed on the basis of my used service.

I really struck. My present application is needed to be migrated from 2.0 to 3.5 with silverlight features.

Please help me on it.

RAJUL
A: 

I use the InitParams of silverlight object in the asp page which host the silverlight to pass the WCF service url. You can get the url from web.config in the asp page. It works in my case.

fresky