Winforms 在windows应用程序和web服务中使用相同的类库
我的工作是: 类库:Model.dllWinforms 在windows应用程序和web服务中使用相同的类库,winforms,web-services,class,Winforms,Web Services,Class,我的工作是: 类库:Model.dll using System; using System.Collections.Generic; using System.Text; namespace root { public class Customer { private int _Id; public int Id { get { return _Id; } set { _Id =
using System;
using System.Collections.Generic;
using System.Text;
namespace root
{
public class Customer
{
private int _Id;
public int Id
{
get { return _Id; }
set { _Id = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
}
ASP.NET Web服务引用Model.dll,以便我可以在Web方法中使用:
[WebMethod]
public string HelloWorld(root.Customer customer) {
return "Hello World";
}
Windows应用程序,同时参考Model.dll和Web服务(1) 错误:参数“1”:无法从“root.Customer”转换为“root.ws.Customer”
(1) 右键单击->和Web引用->此解决方案中的Web服务->单击Service.asmx-> 输入Web引用名称:ws->单击添加引用
更新:我可以从中更改生成的Reference.cs文件中的行
public string HelloWorld(Customer customer)
到
所以函数将寻找真实的模型而不是代理对象,但不是真实的答案。我不想在每次web参考更新后编辑此文件。
如何强制使用真实模型?我担心在自动生成的ASMX代理中重用类型是不可能的 有三种选择 1) 编写自己的代码生成器,其作用类似于
wsdl.exe
,即构建web服务的代理,但重用指定的类型
(相当困难)
2) 编写自己的代码重写器,将自动生成的代理重写为使用自己的类型。每次建立对web服务的引用后,都会调用这样的重写器
(仍然乏味)
3) 切换到WCF web服务。basicHttpBinding上的WCF服务在语义上等同于ASMX web服务(使用基于http+soap的相同通信协议),但WCF服务元数据包含有关类型的更多信息,因此代理生成器能够“重用引用程序集中的类型”
(建议的方法)如果您离开asmx并开始使用WCF的svc模型,您可以在服务器和客户端之间共享一个库,而无需太多麻烦。如果这是一种选择,那么有很多资源可以帮助您使用WCF online。+1。另见。
public string HelloWorld(Customer customer)
public string HelloWorld(root.Customer customer)