我有一个.NET 2 Web服务,其中包含一个Web方法,如下所示:
[WebMethod]
public Common.foobar DoSomething(Common.foobar foo)
foobar类在一个通用的.NET 2程序集中定义,该程序集也可用于.NET 3.5应用程序(作为项目引用),该应用程序通过服务引用(不是.NET兼容性Web引用)调用Web服务。 / p>
问题是服务引用命名空间包含自己生成的foobar实现。因此,代理SOAP客户端上可用的自动生成的服务引用方法具有以下签名:
ServiceReference.foobar DoSomething(ServiceReference.foobar foo)
有点谷歌搜索告诉我,这是不可避免的,因为Web服务是基于.NET 2的,因此不支持重用常见类,因为它在WCF中。
所以问题是:有没有人知道将Common.foobar类克隆到WebServiceReference.foobar类的简单而可靠的方法?或者有人知道“hack”,我可以使用公共库中定义的类吗?或者,任何人都可以指出我错过树木的地方,实际上可以使用公共图书馆类
编辑 - 更多信息
.NET 2 webservice类如下所示:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public CommonLibrary.Foobar DoSomething(CommonLibrary.Foobar foo)
{
return new CommonLibrary.Foobar() { Data = "Data in common foobar class", EventCode = 1, MessageId = "mid" };
}
}
调用客户端(.NET 3.5 - 带有.NET服务的服务引用)如下所示:
static void Main(string[] args)
{
// Create the soap client for the .NET 2 service using the auto generated proxy class
var serviceClient = new NET2WebService_ServiceReference.Service1SoapClient();
//Just checking that there is a project reference to CommonLibrary
var commonFoobar_Request = new CommonLibrary.Foobar()
{
Data = "Common foobar data",
EventCode = 1,
MessageId = "Common foobar message id"
};
// This doesn't work as the Service Reference client does not accept the
// common foobar class.
/*
var commonFoobar_Response = serviceClient.DoSomething(commonFoobar_Request);
Console.WriteLine(commonFoobar_Response.Data);
*/
// This is the proxy class to foobar generated by the Service Reference
var serviceRefFoobar_Request = new NET2WebService_ServiceReference.Foobar()
{
Data = "Common foobar data",
EventCode = 1,
MessageId = "Common foobar message id"
};
// This does work as it does uses the autogenerated Foobar class in the service
// reference
var serviceRefFoobar_Response = serviceClient.DoSomething(serviceRefFoobar_Request);
Console.WriteLine(serviceRefFoobar_Response.Data);
}
公共库(也是.NET 2)中的foobar类看起来像这样:
public partial class Foobar
{
private string dataField;
private int eventCodeField;
private string messageIdField;
public string Data
{
get
{
return this.dataField;
}
set
{
this.dataField = value;
}
}
public int EventCode
{
get
{
return this.eventCodeField;
}
set
{
this.eventCodeField = value;
}
}
public string MessageId
{
get
{
return this.messageIdField;
}
set
{
this.messageIdField = value;
}
}
}
并且源自如下的模式:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Foobar">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="Data" type="xs:string"/>
<xs:element name="EventCode" type="xs:int"/>
</xs:sequence>
<xs:attribute name="MessageId" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
最后,这是.NET 3.5客户端上服务引用配置页面的屏幕截图:
您可以从该屏幕截图中看到,服务配置以及客户端项目都知道包含我的Foobar类实现的CommonLibrary。
值得一提的是,上面发布的所有内容都有效,但实际上foobar类比此处发布的样本复杂得多。因此,我渴望找到一个解决方案,我可以在整个框架中使用Common.foobar,而不必翻译Common.Foobar =&gt; ServiceReference.Foobar用于请求,反之亦然。
答案 0 :(得分:1)
我读过的关于ASMX Web服务的所有内容都表明它们不支持在客户端重用类型的功能。仅当服务是WCF服务而不是旧版ASMX服务时,才会存在此功能。
也许是因为WCF使用差异序列化器,但我不确定。
是否可以将ASMX Web服务转换为WCF服务?
答案 1 :(得分:0)
类的重用是客户端的功能,而不是服务器端的功能。如果您的客户端代码引用了相同的程序集,并且在使用“添加服务引用”时启用了共享,则可能会有效。