Silverlight 应用程序无法访问其他机器上的 WCF 服务 [英] Silverlight application cannot access WCF services on other machines
问题描述
我有一个运行良好的 Silverlight 应用程序,可以访问在 Silverlight 应用程序本身中托管的 WCF 服务.它使用的端口是 1794.
I have a silverlight application which works perfectly and can access the WCF services which are hosted in silverlight application itself. The port it is using is 1794.
当我部署到其他服务器(开发或测试或暂存)时,应用程序无法访问 WCF 服务.
When I deploy to other servers (dev or test or staging), the application is not able to access WCF services.
这是我的 ServiceReference.ClientConfig 中的一个片段
This is a snippet from my ServiceReference.ClientConfig looks like
<endpoint address="http://localhost:1794/MyWebService.svc"
binding="customBinding" bindingConfiguration="CustomBinding_MyWebService"
contract="ConfigMgmtServiceReference.MyWebService"
name="CustomBinding_MyWebService" />
我的根文件夹也包含 clientaccesspolicy.xml 文件.
My root folder contains the clientaccesspolicy.xml file too.
我怎样才能解决这个问题?
How can I get past this issue?
推荐答案
我怀疑 localhost:1794
会导致这个问题 - 当 Silverlight 应用程序在客户端机器上执行时,本地主机将无法获得它返回到服务器.
I suspect the localhost:1794
would be causing the issue - when the silverlight application executes on a client machine the localhost will not get it back to the server.
我用来消除此类问题的技术是在运行时以编程方式设置端点.我需要的两条信息是服务在我的网络项目中的位置(提前知道),以及为 Silverlight 应用程序提供服务的地址(域)(我可以找到).>
The technique i use to eliminate issues like this is to programmatically set the end points at run time. The two pieces of info i need are the location within my web project of the service (which is known ahead of time), and the address (domain) that the silverlight app has been served from (which i can find out).
private void initEndpoint(ServiceEndpoint endPoint, string serviceName)
{
Uri hostUri = Application.Current.Host.Source;
string wcfBaseUri = string.Format("{0}://{1}:{2}/WebServices/", hostUri.Scheme, hostUri.Host, hostUri.Port);
endPoint.Address = new EndpointAddress(new Uri(wcfBaseUri + serviceName));
}
在这段代码中,文件夹 /WebServices
是我的 Web 服务在我的 Web 应用程序中所在的位置.然后我像这样调用函数:
In this piece of code, the folder /WebServices
is where my web services are located within my web app. I then call the function like this:
LoggingServiceClient loggingService = new LoggingServiceClient();
initEndpoint(loggingService.Endpoint, "LoggingService.svc");
我的实际设置比这稍微复杂一些,因为我也希望能够覆盖它并手动配置端点,但您明白了.通过这样做,我已经能够部署到各种设置,网络服务器在奇怪的端口上运行,并且 Silverlight->webservice 位每次都可以正常工作.
my actual setup is slightly more complex than that, because i also want to be able to override that and manually configure the end points, but you get the point. By doing this, i have been able to deploy to all sorts of setups, with webservers running on odd ports, and the silverlight->webservice bit just works every time.
这篇关于Silverlight 应用程序无法访问其他机器上的 WCF 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!