WCF正在调用一个不存在的服务实现
本文关键字:不存在 一个 服务 实现 调用 WCF | 更新日期: 2023-09-27 18:06:09
一个月前,我在我的解决方案中创建了一个WCF服务。
我有服务合同的项目和实现:serviceconcontract .csproj
服务契约:IUserManagementService
服务实现:UserManagementService
服务客户端:UserManagementServiceClient
服务托管项目:ServiceLayer.csproj
我使用的是visual studio pro 2013,我的解决方案针对。net框架4.5.2。该解决方案(以及与该服务相关的项目)是用Visual Studio Express 2015创建的,自两周前以来,我一直在使用Visual Studio Professional 2013。解决方案的迁移成功。
直到一周前一切都很好。在过去的一个月里,我写了很多代码,并成功地调试了它,服务运行良好。
一周前,我开始有问题,当调用我的服务的特定方法(GetUserInfoByUserName): UserManagementService.cs not found + 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%确定,所以我在调试时打开模块窗口:调试-> Windows ->模块,在那里我可以读取所有加载的dll。serviceconcontract .dll的权重为16kb。我做了这个实验:我在服务契约和服务实现(UserManagementService)中添加了新代码,并重新构建了解决方案。我启动了另一个调试会话,并查看了加载的dll: serviceconcontract .dll的权重为18 Kb,因此编译后的代码已更新为新的c#代码。这意味着我在调试时不会加载旧版本的dll。
4)问题不应该出现在服务客户端实现中。客户端服务是用svcutil生成的。为了进行故障排除,我在相同的解决方案(一个简单的控制台应用程序)中创建了一个新的使用者,这次我生成了带有"添加服务引用"的服务客户机。我和这位消费者也有同样的问题。
5)我试图改变服务所在网站的端口:从57915到57916,以排除问题与具有相同端口(57915)的其他网站无关的事实。当然,我相应地更新了消费者的配置文件。此操作未解决问题。
6)我试图从项目中删除UserManagementService.cs并重新分配它。Unsuccesfull .
7)我试图删除服务合同。从解决方案中删除项目,创建新项目,在新项目中复制已删除项目的代码。Unsuccesfull .
在服务契约(IUserManagementService)的代码下面,项目serviceconcontract .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);
}
}
}
下面是用于故障排除的新的实际服务实现,项目serviceconcontract .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, project 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();
}
}
}
我解决了这个问题。
在我的解决方案中有一个旧的dll导致了这个问题。
如果你正在阅读这个问题,因为你有一个类似的问题,检查有没有旧的dll(或一般的可执行代码)在解决方案的所有项目(bin和obj文件夹)