通过svcutil.exe 命令可以生成wcf服务的代理类,然后再配置webconfig调用代理类能达到调用服务的效果。再就是添加web引用也可以调用WCF服务。
但如果服务太多就必须逐一在webconfig配置服务地址,所以研究了下动态配置WCF服务的主机名:
1、编写WCF服务,并通过svcutil.exe 命令生成代理类
2、拷贝代理类在项目
3、编写调用类代码
public class InvokeContext
{
#region CallService
private const string wshttpbinding = "wshttpbinding";
//public static T CreateWCFServiceByURL<T>(string url)
//{
// return CreateWCFServiceByURL<T>(url, "wsHttpBinding", "WSHttpBinding_IDeskBoardProvider");
//}
public static T CreateWCFServiceByURL<T>(string url, string bing,string wsName)
{
if (string.IsNullOrEmpty(url)) throw new NotSupportedException("this url isn`t Null or Empty!");
EndpointAddress address = new EndpointAddress(url);
Binding binding = CreateBinding(bing,wsName);
ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
return factory.CreateChannel();
}
#endregion
#region Create Protocol
/// <summary>
/// Create Protocol
/// </summary>
/// <param name="binding">Protocol Name</param>
/// <returns></returns>
public static Binding CreateBinding(string binding, string wsName)
{
Binding bindinginstance = null;
if (binding.ToLower() == wshttpbinding)
{
WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
//ws.Name = "WSHttpBinding_IDeskBoardProvider";
ws.Name = wsName;
ws.CloseTimeout =new TimeSpan(00, 01, 00);
ws.OpenTimeout = new TimeSpan(00,01,00);
ws.ReceiveTimeout = new TimeSpan(00,10,00);
ws.SendTimeout = new TimeSpan(00,01,00);
ws.BypassProxyOnLocal = false;
ws.TransactionFlow = false;
ws.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
ws.MaxBufferPoolSize = 524288;
ws.MaxReceivedMessageSize = 65536;
ws.MessageEncoding = WSMessageEncoding.Text;
ws.TextEncoding = Encoding.UTF8;
ws.UseDefaultWebProxy = true;
ws.AllowCookies = false;
ws.ReaderQuotas.MaxDepth = 32;
ws.ReaderQuotas.MaxStringContentLength = 8192;
ws.ReaderQuotas.MaxArrayLength = 16384;
ws.ReaderQuotas.MaxBytesPerRead = 4096;
ws.ReaderQuotas.MaxNameTableCharCount = 16384;
ws.ReliableSession.Ordered = true;
ws.ReliableSession.InactivityTimeout = new TimeSpan(00,10,00);
ws.ReliableSession.Enabled = false;
ws.Security.Mode = SecurityMode.Message;
ws.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
ws.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
ws.Security.Transport.Realm = "";
ws.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
ws.Security.Message.NegotiateServiceCredential = true;
ws.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
ws.Security.Message.EstablishSecurityContext = true;
bindinginstance = ws;
}
return bindinginstance;
}
#endregion
}
4、页面调用服务
Default.aspx.cs:
public partial class _Default : System.Web.UI.Page
{
private string url = "http://localhost:888/DeskTest.svc";
private string wsHttpBinding = "wsHttpBinding";
private string wshttpInterface = "WSHttpBinding_IDeskBoardProvider";
protected void Page_Load(object sender, EventArgs e)
{
IDeskBoard inface = InvokeContext.CreateWCFServiceByURL<IDeskBoard>(url, wsHttpBinding, wshttpInterface);
List<T_SYS_DESKBOARD> listDeskBoard = inface.GetDeskBoards().ToList();
Repeater1.DataSource = listDeskBoard;
Repeater1.DataBind();
}
}