WCF随客户端软件一起发布,客户端自动识别WCF服务地址,不通过配置文件绑定WCF服务,客户端动态获取版本号

WCF发布到IIS,并有相应的SVC宿主文件
客户端软件通过CLICK ONCE发布到WCF的相同目录下
本例实现了:客户端自动发现WCF服务的功能

1 不通过配置文件绑定WCF服务

代码
ICallCenter proxy = null;
BasicHttpBinding binding
= new BasicHttpBinding();EndpointAddress address = new EndpointAddress(AppDeploymentUtils.GetWcfUri());
binding.MaxReceivedMessageSize
= 6553600;
binding.MaxBufferPoolSize
= 52428800;
binding.MaxBufferSize
= 6553600;
binding.ReaderQuotas.MaxDepth
= 3200;
binding.ReaderQuotas.MaxStringContentLength
= 819200;
binding.ReaderQuotas.MaxArrayLength
= 16384;
binding.ReaderQuotas.MaxBytesPerRead
= 409600;
binding.ReaderQuotas.MaxNameTableCharCount
= 16384;
ChannelFactory channelFactory
= new ChannelFactory(binding, address);
proxy
= channelFactory.CreateChannel();

上述代码中AppDeploymentUtils.GetWcfUri()是获取WCF地址的方法
代码详细如下

2 动态获取WCF地址

代码
/// <summary>
/// 获取WCF的服务路径
/// </summary>
/// <returns></returns>
public static string GetWcfUri()
{
if (ApplicationDeployment.IsNetworkDeployed)//是否已连接
{
string url = ApplicationDeployment.CurrentDeployment.UpdateLocation.AbsoluteUri.Replace("Life365.CallCenter.application", "CallCenterWCF.svc");
return url;
}
else
{
return "http://localhost:8080/CallCenterWCF.svc";
}
}

3动态获取ClickOnce的版本号

代码
/// <summary>
/// 获取客户端发布版本号
/// </summary>
/// <returns>当前版本号</returns>
public static string GetVersiion()
{
if (ApplicationDeployment.IsNetworkDeployed)//是否已连接
{
ApplicationDeployment currDeployment
= ApplicationDeployment.CurrentDeployment;
string version = currDeployment.CurrentVersion.Major.ToString();//主版本
version += "."+currDeployment.CurrentVersion.Minor.ToString(); //次版本
version += "." + currDeployment.CurrentVersion.Build.ToString();//内部版本号
version += "." + currDeployment.CurrentVersion.Revision.ToString();//修订号
return version;
}
return string.Empty;
}
原文地址:https://www.cnblogs.com/liulun/p/1640473.html