How can I return a custom class from a web service

2019-08-06 10:27发布

问题:

I have a web service that returns an object of a custom class (user):

Web service code

public class User
{
    public string login { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }

}

[WebMethod]
public User GetUserInfo(int userID)
{
    ITDashboardDataContext db = new ITDashboardDataContext();

    User usr = (from u in db.tl_sb_users
                where u.userID == userID
                select new User
                {
                    firstName = u.firstName,
                    lastName = u.lastName,
                    email = GetUserEmail(userID),
                    login = u.login

                }).FirstOrDefault();

    return usr;
}

I want to cast the result as a user object when I call the web service from another application (I've redefined the user class in this app, too):

Calling application code

public class User
{
    public string login { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
}

I'm trying to bring back a user object with this:

RolloutWriter.RolloutWriter rw = new RolloutWriter.RolloutWriter();
rw.Credentials = new NetworkCredential("myuser", "mypassword", "mydomain");

var vu = rw.GetUserInfo(userID);

User u = (from v in vu
         select new User {
             email = vu.email,
             firstName = vu.firstName,
             lastName = vu.lastName,
             login = vu.login
         }).FirstOrDefault();

this doesn't work - it tells me:

Could not find an implementation of the query pattern for source type 'amstaffsite.RolloutWriter.User'.  'Select' not found.

How can I get back a user object?

回答1:

When you add a reference to a webservice, .net reads in the wsdl & creates all the types it needs to use that service.

What you're getting back is the generated type for user. It has exactly the same signature as the type used by the server, but it won't be the same type - that's why you get the type conversion exception. You'll need to create either a factory, or a constructor on your original type, to convert from the generated user to the original user type.

If you were using wcf, you could put the types into a common assembly, shared by both client & server. WCF can figure out that these types are equivalent & re-use them, rather then regenerating the types.



回答2:

Your method GetUserInfo() returns a single user, not a colection. So you can't use LINQ on the result.

In your second section you don't need LINQ:

RolloutWriter.RolloutWriter rw = new RolloutWriter.RolloutWriter();
rw.Credentials = new NetworkCredential("myuser", "mypassword", "mydomain");

User u = rw.GetUserInfo(userID);

//    User u = (from v in vu
//         select new User {
//             email = vu.email,
//             firstName = vu.firstName,
//             lastName = vu.lastName,
//             login = vu.login
//         }).FirstOrDefault();



回答3:

From what I can tell you are trying to return a custom type from a Web Service. If you need to do this, the custom class definition needs to be included into the code calling the web service. Otherwise how will it know what a User is? If you don't want to do this the other thing you can do is return the default string objects as a string array/list/collection as the User class is just a POCO class anyways.

Please see this link

http://www.codeproject.com/Articles/15967/How-to-Return-a-User-Defined-Object-from-Webservic



标签: c# linq class