<p>silverlight中调用wcf服务好好一阵的折腾啊。。。。终于搞定了,做个记录顺便分享一下</p>
<p><span style="font-size: large;"><strong>wcf工程中需要注意的地方:</strong></span></p>
<p><strong>1.新建一个crossdomain.xml文件,内容如下</strong></p>
<p><?xml version="1.0" encoding="utf-8" ?><br><cross-domain-policy><br> <allow-access-from domain="*" /><br> <!-- 意为:允许来自任意域名对本web服务站点的任意跨域访问,如要限制跨域访问站点:可将"*"更改为相应域名,多个域名则为多个<allow-access-from ... />节点 --><br></cross-domain-policy></p>
<p><strong>2.修改web.config文件内容</strong></p>
<p> <endpoint address="" binding="<span style="color: #ff0000;">basicHttpBinding</span>" contract="Demo.Wcf.IService1"><br><endpoint address="mex" binding="<span style="color: #ff0000;">basicHttpBinding</span>" contract="IMetadataExchange"/></p>
<p>因为目前silverlight只支持<span style="color: #ff0000;">basicHttpBinding</span></p>
<p><span style="color: #333300;"><strong><span style="font-size: large;">silverlight工程需要注意的地方:</span></strong></span></p>
<p><span style="color: #333300;"><span style="font-size: small;"><strong></strong></span></span></p>
<p><span style="color: #333300;"><span style="font-size: x-small;">注意其address访问地址</span></span></p>
<p><span style="color: #333300;"><span style="font-size: small;"><span style="font-size: x-small;"></span><span style="font-size: x-small;"> <client><br> <endpoint address="</span><a href="http://localhost:4584/Service1.svc"><span style="font-size: x-small;"><span style="color: #ff0000;">http://localhost:4584/Service1.svc</span></span></a><span style="font-size: x-small;">" binding="basicHttpBinding"<br> bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"<br> name="BasicHttpBinding_IService1" /><br> </client></span></span></span></p>
<p><span style="color: #333300;"><span style="font-size: small;"></span></span></p>
<p>实现代码如下:</p>
<p><textarea cols="78" rows="15" name="code" class="c-sharp"> private void Button_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client client = new Demo.Slapp.ServiceReference1.Service1Client();
client.GetDataAsync(9);
client.GetDataCompleted += new EventHandler<Demo.Slapp.ServiceReference1.GetDataCompletedEventArgs>(client_GetDataCompleted);
client.CloseCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_CloseCompleted);
}
void client_GetDataCompleted(object sender, Demo.Slapp.ServiceReference1.GetDataCompletedEventArgs e)
{
if (e.Error == null)
{
this.btnDemo.Content = e.Result;
}
else
{
this.btnDemo.Content = "eror";
}
}</textarea></p>
<p></p>
<p><br></p>
<p><span style="font-size: large;"><strong>wcf工程中需要注意的地方:</strong></span></p>
<p><strong>1.新建一个crossdomain.xml文件,内容如下</strong></p>
<p><?xml version="1.0" encoding="utf-8" ?><br><cross-domain-policy><br> <allow-access-from domain="*" /><br> <!-- 意为:允许来自任意域名对本web服务站点的任意跨域访问,如要限制跨域访问站点:可将"*"更改为相应域名,多个域名则为多个<allow-access-from ... />节点 --><br></cross-domain-policy></p>
<p><strong>2.修改web.config文件内容</strong></p>
<p> <endpoint address="" binding="<span style="color: #ff0000;">basicHttpBinding</span>" contract="Demo.Wcf.IService1"><br><endpoint address="mex" binding="<span style="color: #ff0000;">basicHttpBinding</span>" contract="IMetadataExchange"/></p>
<p>因为目前silverlight只支持<span style="color: #ff0000;">basicHttpBinding</span></p>
<p><span style="color: #333300;"><strong><span style="font-size: large;">silverlight工程需要注意的地方:</span></strong></span></p>
<p><span style="color: #333300;"><span style="font-size: small;"><strong></strong></span></span></p>
<p><span style="color: #333300;"><span style="font-size: x-small;">注意其address访问地址</span></span></p>
<p><span style="color: #333300;"><span style="font-size: small;"><span style="font-size: x-small;"></span><span style="font-size: x-small;"> <client><br> <endpoint address="</span><a href="http://localhost:4584/Service1.svc"><span style="font-size: x-small;"><span style="color: #ff0000;">http://localhost:4584/Service1.svc</span></span></a><span style="font-size: x-small;">" binding="basicHttpBinding"<br> bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"<br> name="BasicHttpBinding_IService1" /><br> </client></span></span></span></p>
<p><span style="color: #333300;"><span style="font-size: small;"></span></span></p>
<p>实现代码如下:</p>
<p><textarea cols="78" rows="15" name="code" class="c-sharp"> private void Button_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client client = new Demo.Slapp.ServiceReference1.Service1Client();
client.GetDataAsync(9);
client.GetDataCompleted += new EventHandler<Demo.Slapp.ServiceReference1.GetDataCompletedEventArgs>(client_GetDataCompleted);
client.CloseCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_CloseCompleted);
}
void client_GetDataCompleted(object sender, Demo.Slapp.ServiceReference1.GetDataCompletedEventArgs e)
{
if (e.Error == null)
{
this.btnDemo.Content = e.Result;
}
else
{
this.btnDemo.Content = "eror";
}
}</textarea></p>
<p></p>
<p><br></p>