WCF正在调用一个不存在的服务实现

时间:2015-08-03 13:21:44

标签: c# .net wcf visual-studio-2013

我在一个月前在我的解决方案中创建了一个WCF服务。

我有服务合同和实施的项目:ServiceContract.csproj

服务合同:IUserManagementService

服务实施:UserManagementService

服务客户:UserManagementServiceClient

服务托管项目:ServiceLayer.csproj

我正在使用visual studio pro 2013,我的解决方案针对的是net framework 4.5.2。 解决方案(以及与服务相关的项目)是使用Visual Studio Express 2015创建的,从2周前开始,我使用的是Visual Studio Professional 2013.解决方案的迁移成功。

一周前一切正常。我在上个月编写了大量代码并成功调试,服务完美无缺。

一周前,我在调用我的服务的特定方法(GetUserInfoByUserName)时遇到了问题:未找到UserManagementService.cs + MissingMethodException。

http://s1.postimg.org/ol4olperz/Cattura.png

我试图点击“浏览并找到UserManagementService.cs”,出现一个窗口:“源文件与模块构建时不同。您是否希望调试器仍然使用它(是/否)?” (选择“是”并不能解决问题)。

MissingMethodException是由UnserInfoDTO(一个保留在其他项目中的类)的构造函数调用引起的。

所以我开始排除故障,我发现了以下事实:

1)如果我调用其他服务方法,我不会收到任何错误。但是WCF正在调用这些方法的旧版本。我通过用“throw new Exception”替换它们体内的整个代码来修改所有方法。不抛出异常。该程序遵循旧的指示。 当然,对于方法GetUserInfoByUserName(string userName)也是如此:不抛出异常(Exception)。抛出MissingMethodException,因为该方法仍在执行旧代码,我可以调用UserInfoDTO的构造函数。

2)如果我直接调用UserManagementService,只需在客户端创建它的实例,一切正常。换句话说,只有当我通过服务客户端(UserManagementServiceClient)调用服务时才会遇到问题。

3)似乎正确编译了ServiceContract项目的代码(我有服务合同及其实现,我有问题)。 这个事实非常明显(见第2点),但我想100%肯定,所以我在调试时打开了模块窗口:Debug ---> Windows --->模块,我可以读取所有已加载的DLL。 ServiceContract.dll的重量是16 Kb。 我做了这个实验:我在服务合同和服务实现(UserManagementService)中添加了新代码,并重新构建了解决方案。 我启动了另一个调试会话并查看了加载的dll:ServiceContract.dll的重量为18 Kb,因此已使用新的C#代码更新了已编译的代码。 这意味着我在调试时没有加载旧版本的dll。

4)问题不应该在服务客户端实现中。 客户端服务是使用svcutil生成的。 为了排除故障,我在同一个解决方案(一个简单的控制台应用程序)中创建了一个新的使用者,这次我使用“添加服务引用”生成了服务客户端。 我和这位消费者有同样的问题。

5)我试图更改托管服务的网站的端口:从57915到57916,以排除问题与其他网站具有相同端口的事实无关(57915)。 当然我相应地更新了消费者的配置文件。 这个问题没有解决问题。

6)我试图从项目中删除UserManagementService.cs并实现它。 Unsuccesfull。

7)我尝试从解决方案中删除ServiceContract.proj,创建一个新项目,在新项目中复制已删除项目的代码。 Unsuccesfull。

下面是服务合同的代码(IUserManagementService),项目ServiceContract.csproj

namespace ERP.ServiceContract
{
    [ServiceContract]
    public interface IUserManagementService
    {
        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetAllUsers", ReplyAction = "http://tempuri.org/IUserManagementService/GetAllUsersResponse")]
        IEnumerable<UserDTO> GetAllUsers();

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetAllUserInfo", ReplyAction = "http://tempuri.org/IUserManagementService/GetAllUserInfoResponse")]
        IEnumerable<UserInfoDTO> GetAllUserInfo();

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/AddUser", ReplyAction = "http://tempuri.org/IUserManagementService/AddUserResponse")]
        void AddUser(UserDTO user);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/ModifyUser", ReplyAction = "http://tempuri.org/IUserManagementService/ModifyUserResponse")]
        void ModifyUser(UserDTO user);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetUserInfoByUserName", ReplyAction = "http://tempuri.org/IUserManagementService/GetUserInfoByUserNameResponse")]
        UserInfoDTO GetUserInfoByUserName(string userName);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/AddUserInfo", ReplyAction = "http://tempuri.org/IUserManagementService/AddUserInfoResponse")]
        void AddUserInfo(UserInfoDTO user_info);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/ModifyUserInfo", ReplyAction = "http://tempuri.org/IUserManagementService/ModifyUserInfoResponse")]
        void ModifyUserInfo(UserInfoDTO user_info);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/VerifyLogin", ReplyAction = "http://tempuri.org/IUserManagementService/VerifyLoginResponse")]
        UserDTO VerifyLogin(string userName, string password);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/AddPerson", ReplyAction = "http://tempuri.org/IUserManagementService/AddPersonResponse")]
        void AddPerson(PersonDTO person);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/Filter", ReplyAction = "http://tempuri.org/IUserManagementService/FilterResponse")]
        IEnumerable<UserDTO> Filter(Func<UserDTO, bool> predicate);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/ValidateUser", ReplyAction = "http://tempuri.org/IUserManagementService/ValidateUserResponse")]
        IEnumerable<ValidationErrorDTO> ValidateUser(UserDTO user);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetUsersTree", ReplyAction = "http://tempuri.org/IUserManagementService/GetUsersTreerResponse")]
        IEnumerable<UserDTO> GetUsersTree(UserDTO currentUser);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/ValidateUserInfo", ReplyAction = "http://tempuri.org/IUserManagementService/ValidateUserInfoResponse")]
        IEnumerable<ValidationErrorDTO> ValidateUserInfo(UserInfoDTO userInfo);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetCityNameByCAP", ReplyAction = "http://tempuri.org/IUserManagementService/GetCityNameByCAPResponse")]
        string GetCityNameByCAP(string CAP);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetAllCitiesNames", ReplyAction = "http://tempuri.org/IUserManagementService/GetAllCitiesNamesResponse")]
        IEnumerable<string> GetAllCitiesNames();

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetAllCitiesCAPs", ReplyAction = "http://tempuri.org/IUserManagementService/GetAllCitiesCAPsResponse")]
        IEnumerable<string> GetAllCitiesCAPs();

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/ModifyPerson", ReplyAction = "http://tempuri.org/IUserManagementService/ModifyPersonResponse")]
        void ModifyPerson(PersonDTO person);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/ValidatePerson", ReplyAction = "http://tempuri.org/IUserManagementService/ValidatePersonResponse")]
        IEnumerable<ValidationErrorDTO> ValidatePerson(PersonDTO person);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetAllPeople", ReplyAction = "http://tempuri.org/IUserManagementService/GetAllPeopleResponse")]
        IEnumerable<PersonDTO> GetAllPeople();

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/PersonWithEmailExists", ReplyAction = "http://tempuri.org/IUserManagementService/PersonWithEmailExistsResponse")]
        bool PersonWithEmailExists(string email);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetPersonAssociatedToUserWithUserName", ReplyAction = "http://tempuri.org/IUserManagementService/GetPersonAssociatedToUserWithUserNameResponse")]
        PersonDTO GetPersonAssociatedToUserWithUserName(string userName);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetAllCities", ReplyAction = "http://tempuri.org/IUserManagementService/GetAllCitiesResponse")]
        IEnumerable<CityDTO> GetAllCities();

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetAllCountriesKeys", ReplyAction = "http://tempuri.org/IUserManagementService/GetAllCountriesKeysResponse")]
        IEnumerable<string> GetAllCountriesKeys();

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/ModifyUserWithUserName", ReplyAction = "http://tempuri.org/IUserManagementService/ModifyUserWithUserNameResponse")]
        void ModifyUserWithUserName(string userName, string password = null, string name = null, string roleKey = null, DateTime? registrationDate = null, bool? active = null, bool? deleted = null, string fk_creator = null);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/ValidateUserData", ReplyAction = "http://tempuri.org/IUserManagementService/ValidateUserDataResponse")]
        IEnumerable<ValidationErrorDTO> ValidateUserData(string userName = null, string password = null, string name = null, string roleKey = null, string fk_creator = null);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetUserRoleOfUserWithUserName", ReplyAction = "http://tempuri.org/IUserManagementService/GetUserRoleOfUserWithUserNameResponse")]
        string GetUserRoleOfUserWithUserName(string userName);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/AddRecord", ReplyAction = "http://tempuri.org/IUserManagementService/AddRecordResponse")]
        void AddRecord(UserInfoDTO userInfo, PersonDTO referencePerson, ContractDTO contract, string cityKey, string countryKey);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/ValidateRecord", ReplyAction = "http://tempuri.org/IUserManagementService/ValidateRecordResponse")]
        IEnumerable<ValidationErrorDTO> ValidateRecord(UserInfoDTO userInfo, PersonDTO referencePerson, ContractDTO contract, string cityKey, string countryKey);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/ValidateContract", ReplyAction = "http://tempuri.org/IUserManagementService/ValidateContractResponse")]
        IEnumerable<ValidationErrorDTO> ValidateContract(ContractDTO contract);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetContract", ReplyAction = "http://tempuri.org/IUserManagementService/GetContractResponse")]
        ContractDTO GetContract(string id);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetContractAssociatedWithUserInfoWithKey", ReplyAction = "http://tempuri.org/IUserManagementService/GetContractAssociatedWithUserInfoWithKeyResponse")]
        ContractDTO GetContractAssociatedWithUserInfoWithKey(string key);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetContractKeyOfContractAssociatedWithUserInfoWithKey", ReplyAction = "http://tempuri.org/IUserManagementService/GetContractKeyOfContractAssociatedWithUserInfoWithKeyResponse")]
        string GetContractKeyOfContractAssociatedWithUserInfoWithKey(string key);

        [OperationContract(Action = "http://tempuri.org/IUserManagementService/GetPersonFullName", ReplyAction = "http://tempuri.org/IUserManagementService/GetPersonFullNameResponse")]
        string GetPersonFullName(string email);
    }
}

下面是旧的服务合同实现(UserManagementService)

namespace ERP.ServiceContract
{
    public class UserManagementService : IUserManagementService
    {
        private IUserManagement _userManager = UserManagement.GetInstanceForDatabase();

        public IEnumerable<UserInfoDTO> GetAllUserInfo()
        {
            return Converter.ConvertEachElementToDTO(_userManager.GetAllUserInfo());
        }

        public IEnumerable<UserDTO> GetAllUsers()
        {
            var users = _userManager.GetAllUsers();

            //foreach(UserBDO user in users)
            //{
            //    user.UserManager = _userManager;
            //    user.InitializeCreatedUsers();
            //}

            return Converter.ConvertEachElementToDTO(users);
        }

        public void AddUser(UserDTO user)
        {
            _userManager.AddUser(user.ToBDO());
        }

        public void AddUserInfo(UserInfoDTO user_info)
        {
            _userManager.AddUserInfo(user_info.ToBDO());
        }

        public IEnumerable<UserDTO> Filter(Func<UserDTO, bool> predicate)
        {
            return GetAllUsers().Where(predicate);
        }

        public UserInfoDTO GetUserInfoByUserName(string userName)
        {
            return _userManager.GetUserInfoByUserName(userName).ToDTO();
        }

        public void ModifyUser(UserDTO user)
        {
            _userManager.ModifyUser(user.ToBDO());
        }

        public void ModifyUserInfo(UserInfoDTO user_info)
        {
            _userManager.ModifyUserInfo(user_info.ToBDO());
        }

        public IEnumerable<ValidationErrorDTO> ValidateUser(UserDTO user)
        {
            return Converter.ConvertEachElementToDTO(_userManager.ValidateUser(user.ToBDO()));
        }

        public UserDTO VerifyLogin(string userName, string password)
        {
            UserBDO result = _userManager.VerifyLogin(userName, password);

            if (result == null)
            {
                return null; 
            }

            return result.ToDTO();
        }

        public IEnumerable<UserDTO> GetUsersTree(UserDTO currentUser)
        {
            throw new Exception();
            return Converter.ConvertEachElementToDTO(_userManager.GetUsersTree(currentUser.ToBDO()));
            return null;
        }

        public IEnumerable<ValidationErrorDTO> ValidateUserInfo(UserInfoDTO info)
        {
            return Converter.ConvertEachElementToDTO(_userManager.ValidateUserInfo(info.ToBDO()));
        }


        public string GetCityNameByCAP(string CAP)
        {
            return _userManager.GetCityNameByCAP(CAP);
        }

        public IEnumerable<string> GetAllCitiesNames()
        {
            return _userManager.GetAllCitiesNames();
        }

        public IEnumerable<string> GetAllCitiesCAPs()
        {
            return _userManager.GetAllCitiesCAPs();
        }

        public void AddPerson(PersonDTO person)
        {
            _userManager.AddPerson(person.ToBDO());
        }

        public void ModifyPerson(PersonDTO person)
        {
            _userManager.ModifyPerson(person.ToBDO());
        }

        public IEnumerable<ValidationErrorDTO> ValidatePerson(PersonDTO person)
        {
            return Converter.ConvertEachElementToDTO(_userManager.ValidatePerson(person.ToBDO()));
        }

        public IEnumerable<PersonDTO> GetAllPeople()
        {
            return Converter.ConvertEachElementToDTO(_userManager.GetAllPeople());
        }

        public bool PersonWithEmailExists(string email)
        {
            return _userManager.PersonWithEmailExists(email);
        }

        public PersonDTO GetPersonAssociatedToUserWithUserName(string userName)
        {
            PersonBDO result = _userManager.GetPersonAssociatedToUserWithUserName(userName);

            if (_userManager.GetPersonAssociatedToUserWithUserName(userName) == null)
            {
                return null;
            }

            return result.ToDTO();
        }

        public IEnumerable<CityDTO> GetAllCities()
        {
            return Converter.ConvertEachElementToDTO(_userManager.GetAllCities());
        }

        public IEnumerable<string> GetAllCountriesKeys()
        {
            return _userManager.GetAllCountiresKeys();
        }

        public void ModifyUserWithUserName(string userName, string password = null, string name = null, string roleKey = null, DateTime? registrationDate = default(DateTime?), bool? active = default(bool?), bool? deleted = default(bool?), string fk_creator = null)
        {
            _userManager.ModifyUserWithUserName(userName, password, name, roleKey, registrationDate, active, deleted, fk_creator);
        }

        public IEnumerable<ValidationErrorDTO> ValidateUserData(string userName = null, string password = null, string name = null, string roleKey = null, string fk_creator = null)
        {
            return Converter.ConvertEachElementToDTO(_userManager.ValidateUserData(userName, password, name, roleKey, fk_creator));
        }


        public string GetUserRoleOfUserWithUserName(string userName)
        {
            return _userManager.GetUserRoleOfUserWithUserName(userName);
        }

        public void AddRecord(UserInfoDTO userInfo, PersonDTO referencePerson, ContractDTO contract, string cityKey, string countryKey)
        {
            _userManager.AddRecord(userInfo.ToBDO(), referencePerson.ToBDO(), contract.ToBDO(), cityKey, countryKey);
        }

        public IEnumerable<ValidationErrorDTO> ValidateRecord(UserInfoDTO userInfo, PersonDTO referencePerson, ContractDTO contract, string cityKey, string countryKey)
        {
            return Converter.ConvertEachElementToDTO(_userManager.ValidateRecord(userInfo.ToBDO(), referencePerson.ToBDO(), contract.ToBDO(), cityKey, countryKey));
        }


        public IEnumerable<ValidationErrorDTO> ValidateContract(ContractDTO contract)
        {
            return Converter.ConvertEachElementToDTO(_userManager.ValidateContract(contract.ToBDO()));
        }

        public ContractDTO GetContract(string id)
        {
            ContractBDO result = _userManager.GetContract(id);

            if (result != null)
            {
                return result.ToDTO();
            }

            return null;
        }


        public ContractDTO GetContractAssociatedWithUserInfoWithKey(string key)
        {
            ContractBDO result = _userManager.GetContractAssociatedWithUserInfoWithKey(key);

            if (result == null)
            {
                return null;
            }

            return result.ToDTO();
        }


        public string GetContractKeyOfContractAssociatedWithUserInfoWithKey(string key)
        {
            return _userManager.GetContractKeyOfContractAssociatedWithUserInfoWithKey(key);
        }

        public string GetPersonFullName(string email)
        {
            return _userManager.GetPersonFullName(email);
        }
    }
}

下面是用于故障排除的新的实际服务实现,预测ServiceContract.csproj

namespace ERP.ServiceContract
{
    public class UserManagementService : IUserManagementService
    {
        public void GetUsersTree()
        {
            throw new Exception();
        }

        public IEnumerable<DTO.UserDTO> GetAllUsers()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.UserInfoDTO> GetAllUserInfo()
        {
            throw new NotImplementedException();
        }

        public void AddUser(DTO.UserDTO user)
        {
            throw new NotImplementedException();
        }

        public void ModifyUser(DTO.UserDTO user)
        {
            throw new NotImplementedException();
        }

        public void AddUserInfo(DTO.UserInfoDTO user_info)
        {
            throw new NotImplementedException();
        }

        public void ModifyUserInfo(DTO.UserInfoDTO user_info)
        {
            throw new NotImplementedException();
        }


        public void AddPerson(DTO.PersonDTO person)
        {
            throw new NotImplementedException();
        }

        public DTO.UserDTO VerifyLogin(string userName, string password)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.UserDTO> Filter(Func<DTO.UserDTO, bool> predicate)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.ValidationErrorDTO> ValidateUser(DTO.UserDTO user)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.UserDTO> GetUsersTree(DTO.UserDTO currentUser)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.ValidationErrorDTO> ValidateUserInfo(DTO.UserInfoDTO userInfo)
        {
            throw new NotImplementedException();
        }

        public string GetCityNameByCAP(string CAP)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<string> GetAllCitiesNames()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<string> GetAllCitiesCAPs()
        {
            throw new NotImplementedException();
        }

        public void ModifyPerson(DTO.PersonDTO person)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.ValidationErrorDTO> ValidatePerson(DTO.PersonDTO person)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.PersonDTO> GetAllPeople()
        {
            throw new NotImplementedException();
        }

        public bool PersonWithEmailExists(string email)
        {
            throw new NotImplementedException();
        }

        public DTO.PersonDTO GetPersonAssociatedToUserWithUserName(string userName)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.CityDTO> GetAllCities()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<string> GetAllCountriesKeys()
        {
            throw new NotImplementedException();
        }

        public void ModifyUserWithUserName(string userName, string password = null, string name = null, string roleKey = null, DateTime? registrationDate = null, bool? active = null, bool? deleted = null, string fk_creator = null)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.ValidationErrorDTO> ValidateUserData(string userName = null, string password = null, string name = null, string roleKey = null, string fk_creator = null)
        {
            throw new NotImplementedException();
        }

        public string GetUserRoleOfUserWithUserName(string userName)
        {
            throw new NotImplementedException();
        }


        public void AddRecord(DTO.UserInfoDTO userInfo, DTO.PersonDTO referencePerson, DTO.ContractDTO contract, string cityKey, string countryKey)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.ValidationErrorDTO> ValidateRecord(DTO.UserInfoDTO userInfo, DTO.PersonDTO referencePerson, DTO.ContractDTO contract, string cityKey, string countryKey)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<DTO.ValidationErrorDTO> ValidateContract(DTO.ContractDTO contract)
        {
            throw new NotImplementedException();
        }

        public DTO.ContractDTO GetContract(string id)
        {
            throw new NotImplementedException();
        }

        public DTO.ContractDTO GetContractAssociatedWithUserInfoWithKey(string key)
        {
            throw new NotImplementedException();
        }

        public string GetContractKeyOfContractAssociatedWithUserInfoWithKey(string key)
        {
            throw new NotImplementedException();
        }

        public string GetPersonFullName(string email)
        {
            throw new NotImplementedException();
        }

        public DTO.UserInfoDTO GetUserInfoByUserName(string userName)
        {
            throw new NotImplementedException();
        }
    }
}

UserManagementService.svc,项目ServiceLayer.csproj

<%@ ServiceHost Language="C#" Debug="true" Service="ERP.ServiceContract.UserManagementService"  %>

程序演示,项目consumer.csproj

namespace Consumer
{
    class Program
    {
        static void Main(string[] args)
        {
            //Works fine
            UserManagementService service = new UserManagementService();
            service.GetUserInfoByUserName("AGIE");

            //Works fine
            service.GetAllCities();

            ServiceReference.UserManagementServiceClient proxy = new ServiceReference.UserManagementServiceClient();

            //Error UserManagementService.cs not found + MissingMethodException. NotImplementedException is not thrown
            proxy.GetUserInfoByUserName("AGIE");

            //Error UserManagementService.cs not found + MissingMethodException does not occur, but the NotImplementedException of the method is not thrown
            proxy.GetAllCities();
        } 
    }
}

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。

我的解决方案中有一个旧的DLL导致了这个问题。

如果您因为遇到类似问题而正在阅读此问题,请检查解决方案的所有项目(bin和obj文件夹)中是否存在旧的dll(或一般的可执行代码)。