wcf 自定义权限验证调用完整例子源码
static void Main(string[] args){ Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN"); ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorService"); NetworkCredential credential = channelFactory.Credentials.Windows.ClientCredential; credential.UserName = "Foo"; credential.Password = "Password"; ICalculator calculator = channelFactory.CreateChannel(); Invoke(proxy => proxy.Add(1, 2), calculator, "Add"); Invoke(proxy => proxy.Subtract(1, 2), calculator, "Subtract"); Invoke(proxy => proxy.Multiply(1, 2), calculator, "Multiply"); Invoke(proxy => proxy.Divide(1, 2), calculator, "Divide"); Console.WriteLine(); channelFactory = new ChannelFactory<ICalculator>("calculatorService"); credential = channelFactory.Credentials.Windows.ClientCredential; credential.UserName = "Bar"; credential.Password = "Password"; calculator = channelFactory.CreateChannel(); Invoke(proxy => proxy.Add(1, 2), calculator, "Add"); Invoke(proxy => proxy.Subtract(1, 2), calculator, "Subtract"); Invoke(proxy => proxy.Multiply(1, 2), calculator, "Multiply"); Invoke(proxy => proxy.Divide(1, 2), calculator, "Divide"); Console.Read();}static void Invoke(Action<ICalculator> action, ICalculator proxy, string operation){ try { action(proxy); Console.WriteLine("服务操作\"{0}\"调用成功...", operation); } catch (Exception ex) { Console.WriteLine("服务操作\"{0}\"调用失败...", operation); }}
评论