Return object of WCF type from referenced assembly

I looked at some similar posts on this topic and did not quite find what I was looking for, so I will explain what I am doing and the problem I'm working with.

I have an MVC3 application and a class library that stores my model data. For example, the project of my domain model has the CLUser class:

public class CLUser { public int ID { get; set; } [Display(Name = "User Name")] [StringLength(50, ErrorMessage = Util.ERRORMESSAGE_STRING_LENGTH_50)] public string UserName { get; set; } public string Password { get; set; } public DateTime PasswordExpiration { get; set; } public bool LockedOut { get; set; } public string LockedOutReason { get; set; } [Display(Name = "Security Question")] public string SecurityQuestion { get; set; } [Display(Name = "Security Answer")] public string SecurityAnswer { get; set; } [Display(Name = "Current Status:")] public int Standing { get; set; } public int MerchantID { get; set; } public int PartnerID { get; set; } public DateTime CreatedDtTm { get; set; } public DateTime UpdatedDtTm { get; set; } public List<CLPermission> UserPerms { get; set; } } 

Then I have a WCF service that references the same project that has the CLUser model. So, in my WCF, I have a job contract called

 [OperationContract] GetUser(string userName, string password); 

and thus displayed in the MVC project.

So here is the problem. I want to set the CLUser object equal to my WCF service call for GetUser. Which looks like this:

 ServiceClient MyService = new ServiceClient(); CLUser Usr = MyService.GetUser(userName, password); 

However, when I do, I get this error:

It is not possible to implicitly convert an Auth.CLUser type to Auth.CLUser [C: \ .... \ Auth.DLL]

If WCF and the MVC application reference the same DLL, can they use the same objects?

+4
source share
3 answers

If you load the same assembly from different places, then the types cannot be considered equal depending on the loading context used. Identity is sometimes more than a strong name.

+2
source

Since @JohnSaunders avoided commenting on the question, make sure you reference the same version of the general assembly. If the model is different, WCF messaging deserialization will not work.

+1
source

It looks like you are using the generated ServiceClient proxy, which would create a structurally identical but different CLUser type (the type will live in a different namespace) and try to use its return value as a reference to the common CLUser assembly CLUser . If you have a ServiceReference in your ServiceClient project, this leads to this problem. You can use something like Automapper to map content from the generated proxy type to your preferred generic type, or use ChannelFactory to create a client connection that will then return the CLUser type of the generic assembly. Personally, I prefer the ChannelFactory approach.

0
source

Source: https://habr.com/ru/post/1395131/


All Articles