将 Silverlight 应用程序连接到自托管 WCF 服务

发布于 2024-08-02 12:23:54 字数 2285 浏览 4 评论 0原文

我创建了一个在 192.168.0.199:87 上运行的 WCF 服务。该服务运行没有问题。但是,当我创建一个 Silverlight 应用程序来在 VS 中的开发电脑上使用此服务时,我遇到了跨域问题。我该如何解决这个问题?该服务不是 IIS WCF 服务。我也无法在同一端口上托管 WCF 服务和 Silverlight 应用程序。 Silverlight 正在 192.178.0.199:87 上查找 clientaccesspolicy.xml,在本例中,这是我的自托管 WCF 服务的地址。

任何帮助都会很棒。

这是我的代码我不知道我是否能酿造出好东西。 我的 app.config 文件位于此处。我认为这是一个端点问题,但我不确定。 http://213.46.36.140/app.config.txt

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();
        }      

        private void button1_Click(object sender, EventArgs e)
        {
            _host = new ServiceHost(typeof(WmsStatService));
            _host.Open();
        }
    }

    // Define a service contract.
    [ServiceContract(Namespace = "http://WindowsFormsApplication11")]
    public interface IWmsStat
    {
        [OperationContract]
        string sayHello(string name);
        [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
        Stream GetSilverlightPolicy();
    }

    public class WmsStatService : IWmsStat
    {
        public string sayHello(string name)
        {
            return "hello there " + name + " nice to meet you!";
        }

        Stream StringToStream(string result)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }
        public Stream GetSilverlightPolicy()
        {
            // result cointains the clienaccpolicy.xml content.
            //
string result = @"<?xml version=""1.0"" encoding=""utf-8""?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers=""*"">
                <domain uri=""*""/>
            </allow-from>
            <grant-to>
                <resource path=""/"" include-subpaths=""true""/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>";
            return StringToStream(result);
        }
    }
}

I've created a WCF service that is running on 192.168.0.199:87. The service works without an issue. However, when I create a Silverlight app to consume this service on my dev pc in VS I am getting the cross-domain problem. How am I going to solve this? The service is not an IIS WCF service. I also can't host the WCF service and the Silverlight app on the same port. Silverlight is looking for the clientaccesspolicy.xml on 192.178.0.199:87, in this case, this is the address of my self hosted WCF service.

Any help would be great.

This is my code I don't know if I brew something good.
My app.config file is located here. I think it's an endpoint problem but I am not sure.
http://213.46.36.140/app.config.txt

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();
        }      

        private void button1_Click(object sender, EventArgs e)
        {
            _host = new ServiceHost(typeof(WmsStatService));
            _host.Open();
        }
    }

    // Define a service contract.
    [ServiceContract(Namespace = "http://WindowsFormsApplication11")]
    public interface IWmsStat
    {
        [OperationContract]
        string sayHello(string name);
        [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
        Stream GetSilverlightPolicy();
    }

    public class WmsStatService : IWmsStat
    {
        public string sayHello(string name)
        {
            return "hello there " + name + " nice to meet you!";
        }

        Stream StringToStream(string result)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }
        public Stream GetSilverlightPolicy()
        {
            // result cointains the clienaccpolicy.xml content.
            //
string result = @"<?xml version=""1.0"" encoding=""utf-8""?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers=""*"">
                <domain uri=""*""/>
            </allow-from>
            <grant-to>
                <resource path=""/"" include-subpaths=""true""/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>";
            return StringToStream(result);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文