C# Web Service 和网站共享库,服务返回不同的“类型";库对象 [英] C# Web Service and Web Site sharing library, service returns different "type" of library object

查看:25
本文介绍了C# Web Service 和网站共享库,服务返回不同的“类型";库对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个解决方案中有一个 Web 服务和一个网站(都是 C#)(目前);我在解决方案中也有一个类库.Web 服务和网站都引用了这个类库.

I have a Web service and a Web site (both C#) in the same solution (For now); I also have a class library in the solution. Both the web service and the web site reference this class library.

Web 服务有一个 WebMethod,它从库中创建一个对象并返回它.该网站调用它并尝试将其放入 Trainer 对象(再次来自同一个库)

The web service has a WebMethod that creates an object from the library and returns it. The website invokes this and attempts to put it into a Trainer object (once again, from the same library)

    ProFitWebService.Service serviceConn = new ProFitWebService.Service();
    ProFitLibrary.Trainer authenticatedTrainer = (ProFitLibrary.Trainer)serviceConn.GetAuthenticatedTrainer(_TrainerLogin.UserName);

但是出现以下情况:无法将 ProFitWebService.Trainer 类型转换为 ProFitLibrary.Trainer"

however the following occurs: "Cannot convert type ProFitWebService.Trainer to ProFitLibrary.Trainer"

这是WebMethod:

Here is the WebMethod:

[WebMethod]
public ProFitLibrary.Trainer GetAuthenticatedTrainer(string email)
{
    ProFitLibrary.Trainer returnTrainer = new ProFitLibrary.Trainer();
    SqlCommand cmd = new SqlCommand("SELECT * FROM Trainers WHERE EmailAddress = '" + email + "'", conn);
    conn.Open();

    SqlDataReader reader;
    reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        returnTrainer.TrainerId = reader.GetInt32(reader.GetOrdinal("TrainerId"));
        returnTrainer.FirstName = reader.GetString(reader.GetOrdinal("FirstName"));
        returnTrainer.LastName = reader.GetString(reader.GetOrdinal("LastName"));
        returnTrainer.PhoneNumber = reader.GetString(reader.GetOrdinal("PhoneNumber"));
        returnTrainer.Address = reader.GetString(reader.GetOrdinal("Address"));
        returnTrainer.City = reader.GetString(reader.GetOrdinal("City"));
        returnTrainer.PostalCode = reader.GetString(reader.GetOrdinal("PostalCode"));
        returnTrainer.EmailAddress = reader.GetString(reader.GetOrdinal("EmailAddress"));
    }

    return returnTrainer;
}

更新:在网站上将 Trainer 对象更改为 ProFitWebService.Trainer 解决了该问题:

Update: Changing the Trainer object to ProFitWebService.Trainer on the Web site fixed the issue:

    ProFitWebService.Service serviceConn = new ProFitWebService.Service();
    ProFitWebService.Trainer authenticatedTrainer = (ProFitWebService.Trainer)serviceConn.GetAuthenticatedTrainer(_TrainerLogin.UserName);

我认为答案很简单,从 Web 服务返回的库对象将始终基于类型/以服务为前缀 - 我不应该同时从网站和服务中引用类库 - 我应该总是创建对象的 WebService 版本 - ProFitWebService.Trainer 等.

I think the answer to this is simply that library objects returned from a Web Service will always be type based/prefixed on the service - and I should not reference the class Library from both the Website and the Service - I should just always create the WebService version of the object - ProFitWebService.Trainer etc.

当您在 Web 服务中使用库时,有人可以确认这是一种标准做法吗?或者如果我让这更难,那么它真的是!

Could someone confirm this as a standard practice when you're using libraries within a web service? or if I'm making this more difficult then it really is!

推荐答案

在创建对 Web 服务的 Web 引用时,您将获得为您生成的代理类.这些代理类看起来像您的库中的类,但它们不是相同的类型.如果要将 Web 服务返回的对象视为共享库中的类型,则需要使用某种方法在这些类型的库版本和代理版本之间进行转换.

When creating the web reference to your web service you will get proxy classes generated for you. These proxy classes look like the classes from your library, but they are not the same types. You will need to have some method translating between the library version and the proxy versions of these types if you want to treat objects returned from the web service as types from your shared library.

这篇关于C# Web Service 和网站共享库,服务返回不同的“类型";库对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆