我们知道,在silverlight中对service(web service,wcf)添加引用后,会产生一个config文件: ServiceReferences.ClientConfig.如下所示:
Code
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_Wcf_GetData" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:1522/CH10/Wcf_GetData.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Wcf_GetData"
contract="WcfGetDataProxy.Wcf_GetData" name="BasicHttpBinding_Wcf_GetData" />
</client>
</system.serviceModel>
</configuration>
可以看到,这个config中配置了service的一些信息:地址,绑定类型等。这样在实例化服务时候,默认会到此config中读取相关节点信息...
但是这样有几个缺点:
1.本地开发与发布版本时候,需要重新配置文档,并编译才可。【地址位置发生了改变】
2.不安全。client端可以在IE临时文件夹中找到silverlight本地备份,xx.xap文件,可以轻松解包,看到此config文件,并获悉service地址信息....
为了避免这些问题,我们可以利用实例化服务时,服务的几个构造函数的重载函数来实现。也就是说我们在实例化时,明确指出service的地址等信息,而不是到默认的config中查找。
可以自定义一个class,对config中的节点进行实例化,并返回特定信息,从而可以删除config文件,起到client端安全的作用。
自定义服务类别
Code
public class ServiceUtil
{
/// <summary>
/// 调用远端Web service
/// </summary>
/// <returns></returns>
public static ISDApp01Xml.SDApp01XmlSoapClient GetDynamicClient()
{
//BasicHttpBinding 定义
BasicHttpBinding binding = new BasicHttpBinding(
Application.Current.Host.Source.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase)
? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;
//Endpoind 定义
EndpointAddress client = new EndpointAddress(GetHostUrl());
//返回web service实例:注意这里利用了服务的构造函数的重载
return new ISDApp01Xml.SDApp01XmlSoapClient(binding,client);
}
/// <summary>
/// 调用本地Web service
/// </summary>
/// <returns></returns>
public static myService.myServiceSoapClient GetmyService()
{
BasicHttpBinding binding = new BasicHttpBinding(
Application.Current.Host.Source.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase)
? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;
//取得web service路径
EndpointAddress client = new EndpointAddress((new Uri(Application.Current.Host.Source, "../myService.asmx")));
return new myService.myServiceSoapClient(binding, client);
}
public static string GetHostUrl()
{
//指定web service路径
return "http://webtest/WebServices/myService.asmx";
}
}
silverlight调用:
Code
public partial class Demo012 : UserControl
{
public Demo012()
{
InitializeComponent();
setBind();
}
private void setBind()
{
myService.myServiceSoapClient client= ServiceUtil.GetDynamicClient();
string sql = "SELECT ID,NAME FROM PDM_BASIC_INFO";
client.ExecuteQueryAsync(sql);
client.ExecuteQueryCompleted += (sender2, e2) => {
if (e2.Error == null)
{
dgshow.ItemsSource = from item in e2.Result.Descendants("row")
select new BasicInfo
{
ID = (int)item.Attribute("ID"),
Name = (string)item.Attribute("NAME")
};
}
};
myService.myServiceSoapClient wc = ServiceUtil.GetmyService();
wc.sayHelloAsync();
wc.sayHelloCompleted += (sender1, e1) =>
{
MessageBox.Show(e1.Result);
};
}
}
public class BasicInfo
{
public int ID { set; get; }
public string Name { set; get; }
}