2011-05-18 33 views
1

我有一個Web方法的.NET 2 Web服務,看起來是這樣的:.NET 2 ASMX Web服務 - 通過.NET 3.5調用的服務參考 - 通用類重用

[WebMethod] 
public Common.foobar DoSomething(Common.foobar foo) 

的foobar的階級在一個通用的.NET 2程序集中定義,該程序也可以作爲.NET 3.5應用程序(作爲項目引用)使用,該應用程序通過服務引用(而不是.NET兼容性Web引用)調用Web服務。

問題是服務引用命名空間包含它自己的自動生成的foobar實現。所以自動生成的服務參考方法,代理SOAP客戶機上使用,具有以下特徵:

ServiceReference.foobar DoSomething(ServiceReference.foobar foo) 

一個小Googling告訴我,這是不可避免的作爲Web服務是基於.NET 2,因此重用常見類不受支持,因爲它在WCF中。

所以問題是:有人知道一個簡單和可靠的方法克隆一個Common.foobar類到一個WebServiceReference.foobar類嗎?或者是否有人知道我可以使用公共庫中定義的類的「黑客」?或者有誰能夠指出哪裏我已經錯過了樹木不見森林,這是逸岸可以使用公共庫類

編輯 - 一些更多的信息

的.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客戶端上Service Reference配置頁面的屏幕截圖:

Screen shot of .NET 3.5 Service Config page

您可以從該屏幕截圖中看到,服務配置以及客戶端項目都知道包含我的Foobar類實現的CommonLibrary。

值得一提的是,上面所列出的所有東西都是作品,但實際上foobar類比這裏發佈的樣本複雜得多。因此,我非常希望找到一種解決方案,可以在整個框架中使用Common.foobar,而不必爲請求翻譯Common.Foobar => ServiceReference.Foobar,反之亦然。

+0

請嘗試使用'[DataMember]'裝飾CommonLibrary.Foobar類的屬性,並使用'[DataContract]'裝飾類。 – 2011-05-19 15:23:37

+0

由於DataMember/DataContract是與WCF和.NET 3.5一起引入的,所以無法完成 - 公用庫是.NET 2,因爲它是由.NET 2 Web Service使用的。升級.NET版本是不可行的(事實上,如果這是一種可能性,我將在兩側使用WCF並放棄ASMX) – MrEyes 2011-05-19 18:27:50

+0

爲什麼不將共享類移動到新的程序集中並引用它在服務器和客戶端?這就是我所做的。 – ashes999 2012-02-08 15:19:28

回答

0

類的重用是客戶端的功能,而不是服務器端的功能。如果您的客戶端代碼引用相同的程序集,並且在使用「添加服務引用」時啓用共享,則可能會有效。

+0

「這可能有用」 - 看起來不是 - 引用是服務引用,而不是.NET 2兼容性Web服務引用。 – MrEyes 2011-05-18 20:05:41

+0

@MeEyes:服務引用是它應該工作的地方。 – 2011-05-18 21:44:57

+0

檢查引用程序集中的重用類型,並選擇所有引用的程序集中的重用類型選項。然而,服務引用生成了它自己的.NET 2 ASMX Web服務所需的對象的等價物 – MrEyes 2011-05-18 22:44:27

1

我已閱讀的有關ASMX Web服務的一切都表明,它們不支持在客戶端重用類型的功能。此功能僅在服務是WCF服務而不是傳統ASMX服務時才存在。

也許這是因爲WCF使用不同的序列化程序,但我不確定。

是否可以將ASMX Web服務轉換爲WCF服務?

+0

儘管我希望WCF不可能作爲框架(非常大),但它僅用於支持.NET 2。 – MrEyes 2011-09-08 12:55:07