1、Create a error class just like this


namespace TeamService.Projects

{

[DataContract]

public class ProjectServiceError

{

private string operation;

private string errorMessage;


[DataMember]

public String Operation

{

get { return operation; }

set { operation = value; }

}


[DataMember]

public String ErrorMessage

{

get { return errorMessage; }

set { errorMessage = value; }

}

}

}



2、通过FaultContract将其运用到Service Contract中制定的Operation上面




using TeamService.Data;

namespace TeamService

{

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IProjectService" in both code and config file together.

[ServiceContract]

public interface IProjectService

{

[OperationContract]

[FaultContract(typeof(TeamService.Projects.ProjectServiceError))]

int CreateMember(MyMember myMember);




3、在Service Implementation中以抛出Exception的方式植入这个

TeamService.Projects.ProjectServiceError对象



public int CreateMember(MyMember myMember)

{

if (myMember == null)

{

ProjectServiceError err = new ProjectServiceError();

err.Operation = " CreateMember";

err.ErrorMessage = "Input parameter is null.";


throw new FaultException(err.Operation + ":êo" + err.ErrorMessage);

}


后面还有代码,不写了


4、客户端调用



private void button1_Click(object sender, RoutedEventArgs e)

{

TeamClient.ProjectServiceReference.ProjectServiceClient sv = new ProjectServiceClient();


MyMember mb = null;

try

{

if (sv != null)

{

sv.CreateMember(mb);

}


}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

}



5、效果显示: