WCF通用存储库

时间:2018-01-18 19:10:33

标签: c# wcf

我目前正在使用WCF服务器和客户端(Winform)应用程序。我有很多实体(模型),我将执行CRUD操作。我尝试实现Generic Repository但是获取错误值不能为null参数名称:Key

My Internface

namespace Interface
{
    [ServiceContract]
    public interface IRepository<T> where T : class
    {
        [OperationContract]
        T Add(T t); 
        [OperationContract]
        Task<T> AddAsyn(T t);

        [OperationContract]
        int Count();  

        [OperationContract]
        Task<int> CountAsync();  

        [OperationContract]
        void Delete(T entity);  

       .....

    }
}

实施

public class Repository<T> : IRepository<T> where T : class
    {
        private readonly ApplicationDbContext _context;
        public Repository()
        {
            _context = new ApplicationDbContext();
        }
        public IQueryable<T> GetAll()
        {
            return _context.Set<T>();
        }

        public virtual async Task<ICollection<T>> GetAllAsyn()
        {

            return await _context.Set<T>().ToListAsync();
        }

        public virtual T Get(int id)
        {
            return _context.Set<T>().Find(id);
        }

   ......
}

服务托管

private ServiceHost _service;
private void StartService()
        {

            try
            {
                _service = new ServiceHost(typeof(Service.Repository<>));
                _service.Open();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

App.Config中

<services>
        <service behaviorConfiguration="debug" name="Service.Repository">
          <endpoint name="Repository" address="Repository" binding="netTcpBinding" bindingConfiguration="customTcpBinding" contract="Interface.IRepository" />

          <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
          <host>
            <baseAddresses>

              <add baseAddress="net.tcp://localhost:9898" />
            </baseAddresses>
          </host>
        </service>

获得错误 System.ArgumentNullException:&#39;值不能为null。参数名称:key

0 个答案:

没有答案