为wcf项目创建故障异常

时间:2016-10-14 12:25:29

标签: c# entity-framework wcf

我正在尝试为我的btnUpdate书类创建faultexception。我是WCF的新手,我不确定我必须添加异常消息客户端或服务的哪个部分?

这是我的客户部分

protected void BtnUpdateBook(object sender, DirectEventArgs e)
{
    Service1Client client = new Service1Client();
    ConcreteBook book = new ConcreteBook
    {
        BookId = Convert.ToInt32(txtBookId.Text),
        BookName = txtBookName.Text,
        BookDescription = txtBookDescription.Text
    };
    client.UpdateBook(book);
    strBook.DataSource = client.GetAll();
    strBook.DataBind();
    client.Close();
}

这是服务

public class UpdateBook
{
    public void BookUpdate(ConcreteBook book)
    {
        BookEntities db = new BookEntities();
        Book upBook = db.Books.FirstOrDefault(x => x.bookId == book.BookId);
        upBook.bookName = book.BookName;
        upBook.bookDescription = book.BookDescription;
        db.SaveChanges();
    }
}

修改

我在Service

中创建了BSException类
[DataContract] 
public class BSException 
{ 
    private int id;

    [DataMember] 
    public int Id 
    { 
        get { return id; } 
        set { id = value; } 
    } 

    private string exceptionMessage; 

    [DataMember] 
    public string ExceptionMessage 
    { 
        get { return exceptionMessage; } 
        set { exceptionMessage = value; } 
    }
}

我正在尝试在我的服务中的updatebook类中使用这个类

public void BookUpdate(ConcreteBook book) 
{ 
    BookEntities db = new BookEntities(); 
    try 
    { 
        Book upBook = db.Books.FirstOrDefault(x => x.bookId == book.BookId); 
        upBook.bookName = book.BookName; 
        upBook.bookDescription = book.BookDescription; 
        db.SaveChanges();
    }
    catch(Exception) 
    {
        BSException exception = new BSException(); 
        //exception. //problem is here??
    }
}

0 个答案:

没有答案