2007年3月6日 星期二

SchemaImporterExtension 的功用

SchemaImporterExtension ? 這是什麼?

當我們使用 Web Service,在Client App 會產生 proxy class

而在 Web Service ,使用了 DataContract 時,在 Client App 會產生相對應的 proxy class,但卻不一樣。

舉例:

Service Side

[DataContract] class ScheduleEntry { public string EntryTitle; public DateTime Timestamp; }

[ServiceContract] class Scheduler { [OperationContract] public void AddEntry(ScheduleEntry entry) { } }

Client Side Add Service Reference 後,會產生一個 proxy class: ScheduleEntry ,但不是我們在Service Service 所用的。

這樣造成了重複的定義,非常不妥。

此時,應當使用 SchemaImporterExtension ,自行定義 code generator 應該參考我們自己的 assembly

請參考 SchemaImporterExtension Class SchemaImporterExtension Technology Sample

The correct way to do it would be: 1. Create a type that inherits from SchemaImporterExtension 2. Override the ImportSchemaType method 3. Write code to check the name and namespace of the type described in the WSDL document and replace with your own type and return the generated code. 4. Give your type a strong name and put it in the GAC (alternatively put it in the bin folder) 5. Register the extension to the proxy generator in the machine.config 6. Use Add Web Reference or wsdl.exe to generate the proxy.

到了WCF ,方法有所改變

請參考 Importing Schema to Generate Classes

沒有留言:

Share with Facebook