- [DataContract]
- public class CallException
- {
- public CallException() { }
- public CallException(string message, string detail)
- { Message = message;
- Detail = detail;
- }
- [DataMember]
- public string Message { get; set; }
- [DataMember]
- public string Detail { get; set; }
- }
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- [FaultContract(typeof(CallException))]
- User GetUserByID(int id,string communCode, out CallException callException);
- [OperationContract]
- [FaultContract(typeof(CallException))]
- [ServiceKnownType(typeof(User ))]
- BaseClass.EntityBase GetByIDWithAuthentication(int id, out CallException callException);
- }
- [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
- [Common.MyServiceBehavior()]
- public class Service1Impl : BaseClass.ServiceBase, IService1
- {
- #region IService1 Members
- public User GetUserByID(int id, string communCode, out CallException callException)
- {
- callException = null;
- User user = null;
- BLL.UserBLL userBll = Common.ServiceLocator.LoadService<BLL.UserBLL>();
- user= userBll.GetUserByID(id, out callException);
- return user;
- }
- //[Common.AuthenticationBehavior()]
- public BaseClass.EntityBase GetByIDWithAuthentication(int id, out CallException callException)
- {
- callException = null;
- User user = null;
- BLL.UserBLL userBll = Common.ServiceLocator.LoadService<BLL.UserBLL>();
- user = userBll.GetByID(id, out callException);
- return user;
- }
- #endregion
- }
- public class UserBLL : BaseClass.BLLBase
- {
- public UserBLL(Common.DALHelper dalHelper)
- {
- _dalHelper = dalHelper;
- }
- private Common.DALHelper _dalHelper;
- [Common.ExceptionCallHandler("你没有权限", "", "", "你没有权限啊BLL")]
- public User GetByID(int id, out CallException callException)
- {
- callException = null;
- if (id < 10)
- {
- callException = new CallException()
- {
- Message = "获取用户",
- Detail = "必须大于等于10"
- };
- throw new FaultException<CallException>(callException, "parameter error");
- }
- else
- {
- User user = null;
- int b = 0;
- user = _dalHelper.UserDal.GetByID(id, ref b, out callException);
- return user;
- }
- }
- [Common.ExceptionCallHandler("你没有权限", "", "", "你没有权限啊BLL")]
- public User GetUserByID(int id, out CallException callException)
- {
- User user = null;
- callException = null;
- if (id < 10)
- {
- callException = new CallException()
- {
- Message = "获取用户",
- Detail = "必须大于等于10"
- };
- }
- else
- {
- int b = 0;
- user = _dalHelper.UserDal.GetByID(id, ref b, out callException);
- } return user;
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- using System.ServiceModel.Channels;
- namespace ConsoleConsumer
- {
- class Program
- {
- static void Main(string[] args)
- {
- Service1.Service1Client client = new Service1.Service1Client();
- using (OperationContextScope scope = new OperationContextScope((IContextChannel)client.InnerChannel))
- {
- MessageHeaders messageHeadersElement = OperationContext.Current.OutgoingMessageHeaders;
- messageHeadersElement.Add(MessageHeader.CreateHeader("username", "", "kd"));
- messageHeadersElement.Add(MessageHeader.CreateHeader("password", "", "kd"));
- }
- Console.WriteLine("请输入ID:");
- int id =int.Parse ( Console.ReadLine());
- Service1.CallException callException = null;
- try
- {
- client.GetByIDWithAuthentication(out callException, id);
- Console.WriteLine("成功调用");
- }
- catch (FaultException<Service1.CallException> ex)
- {
- Console.WriteLine("半路接获CallException Error:{0},{1}", ex.Detail.Message, ex.Detail.Detail);
- }
- catch (FaultException<System.IO.EndOfStreamException> ex)
- {
- Console.WriteLine("半路接获EndOfStreamException Error:{0},{1}", ex.Detail.Message, ex.Detail.HelpLink );
- }
- catch (Exception ex)
- {
- Console.WriteLine("最后一关 Error:{0},{1}", ex.Message, ex.HelpLink);
- }
- Console.Read();
- }
- }
- }
- void client_GetUserByIDCompleted(object sender, Service1.GetUserByIDCompletedEventArgs e)
- {
- #region
- if (e.callException != null)
- {
- ChildWindow win = new ChildWindow();
- win.Title = e.callException.Message;
- win.Content = e.callException.Detail;
- win.MinHeight = 50;
- win.MinWidth = 200;
- win.Show();
- }
- else
- {
- ChildWindow win = new ChildWindow();
- win.Title = "ok";
- win.Content = "it is ok";
- win.MinHeight = 50;
- win.MinWidth = 200;
- win.Show();
- }
- #endregion
- }
3 结论
MMSUXIAOHH 2011-12-01