Silverlight应用程序无法访问其他计算机上的WCF服务 [英] Silverlight application cannot access WCF services on other machines

查看:78
本文介绍了Silverlight应用程序无法访问其他计算机上的WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

如何解决这个问题?

推荐答案

我怀疑 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.

该技术我用来消除此类问题的方法是,以编程方式在运行时设置终点。我需要的两条信息是服务的Web项目在我的Web项目中的位置(这是提前知道的),以及从中提供了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");

我的实际设置比这稍微复杂一些,因为我也希望能够覆盖它,并且手动配置终点,但是您已经明白了。通过这样做,我已经能够部署到各种设置,并且Web服务器在奇数端口上运行,并且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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆