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 WCFWhen 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!
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.
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.
精彩评论