简体   繁体   English

简单的WCF程序,服务方法在客户端程序中不起作用

[英]Easy WCF program, service method not working in client program

I just started writing a WCF service and have come across a strange problem. 我刚刚开始编写WCF服务,遇到了一个奇怪的问题。 I don't know why method from service invoked in client doesn't work properly and new book is not created. 我不知道为什么客户端调用的服务方法无法正常工作,并且无法创建新书。 listBooks() method id ok because it returns list of books but method addBook doesn't create new book in booksList. listBooks()方法ID正常,因为它返回书籍列表,但方法addBook不会在booksList中创建新书籍。 But this situation is only when I use use this method from client because the same method invoked in Service constructor creates a book. 但是这种情况仅发生在我从客户端使用此方法的时候,因为在Service构造函数中调用的同一方法会创建一本书。

Here is the client code: 这是客户端代码:

namespace LibraryClient
{
class Program
{
    static ServiceReference.ServiceClient client;
    static void Main(string[] args)
    {

        client = new ServiceReference.ServiceClient();

        client.addBook(2, "some book", "some author"); //this is not working
        ServiceReference.Book[] books = client.listBooks();

        foreach(var book in books)
        {
            Console.WriteLine("{0}", book.title);
        }

        Console.ReadLine();

    }
}
}

and service code: 和服务代码:

namespace LibraryService
{

public class Service : IService
{

    private static List<Book> booksList;
    private static List<User> usersList;



    public Service()
    {
        booksList = new List<Book>();
        usersList = new List<User>();
        addBook(1, "ANOTHER BOOK", "ANOTHER AUTHOR"); //this is working


    }

    public List<Book> listBooks()
    {

        return booksList;
    }

    public List<User> listUsers() { 
        return usersList;
    }

    public void addBook(int id, string title, string author)
    {
        booksList.Add(new Book(id,title,author));

    }

    }
}

On the server side, each time you call the service a new instance of the service class will be created. 在服务器端,每次调用服务时,都会创建服务类的新实例。 And creating a new instance means that the constructor of this class will be called. 创建新实例意味着将调用此类的构造函数。

And just look what you are doing in the constructor: 只是看看您在构造函数中正在做什么:

booksList = new List<Book>();
usersList = new List<User>();

Basically you are killing everything that might have been stored in those static lists from previous calls. 基本上,您是在杀死以前调用中存储在这些静态列表中的所有内容。 So make sure that you are instantiating those lists only once to ensure that the information stored in them will persist between multiple calls: 因此,请确保仅实例化这些列表一次,以确保存储在其中的信息将在多次调用之间保持不变:

private static readonly List<Book> booksList = new List<Book>();
private static readonly List<User> usersList = new List<User>();

public Service()
{
}

Also bear in mind that the List<T> class is not thread safe. 还请记住List<T>类不是线程安全的。 This means that if you have multiple concurrent client calls to your webservice, the data could be corrupted in this list. 这意味着,如果您有多个并发客户端调用您的Web服务,则此列表中的数据可能会损坏。 You might need to synchronize the access to them to prevent this: 您可能需要同步对它们的访问以防止这种情况:

private static readonly object syncRoot = new object();

public void addBook(int id, string title, string author)
{
    lock (syncRoot)
    {
        booksList.Add(new Book(id, title, author));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM