将 Silverlight 应用程序连接到自托管 WCF 服务
我创建了一个在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论