私はこの正確な問題に遭遇しました。解決策はそれほど単純ではありませんでした。
私は次のことをしました:
- サービス用に独自のServiceHostFactoryを定義する
- 工場では、サービスコントラクトの説明からメタデータの動作を取得し、組み込みのMetadataExporterをカスタム実装に置き換えます
- カスタムMetaDataExporterで、GetGeneratedMetadata関数をオーバーライドし、サービス用に生成されたXMLスキーマを直接操作します
- サービスによって公開されたタイプのリフレクションを使用して、必要なすべての列挙型を検出し、それらをスキーマに追加できました
- これで、サービスをクライアントに追加してプロキシを構築すると、列挙型が含まれます
- 含めたい列挙型に適用できる独自のカスタムDataEnumAttributeを定義することで、プロセスを汎用化しました
私の考えでは、これはwcfサービスメタデータジェネレーターの欠陥です。タイプで定義された列挙型を追加するように構成可能である必要がありますが、コントラクトプロパティによって参照されることはありません。
完全なコードは投稿するには少し長すぎますが、基本的な考え方は次のとおりです。
Public Class CustomMetaDataFactory
Inherits System.ServiceModel.Activation.ServiceHostFactory
'----------------------------------------------------------------------------
' CreateServiceHost (Overridden)
'----------------------------------------------------------------------------
Protected Overrides Function CreateServiceHost(ByVal serviceType As System.Type, _
ByVal baseAddresses() As System.Uri) _
As System.ServiceModel.ServiceHost
'--------------------------------------------------------------------------
' Local Variables
'--------------------------------------------------------------------------
Dim host As System.ServiceModel.ServiceHost = _
MyBase.CreateServiceHost(serviceType, baseAddresses)
Dim smb As ServiceMetadataBehavior
Dim bFound As Boolean = False
'--------------------------------------------------------------------------
' Attach a meta data behaviour so that the service will publish
' wsdl and xsd descriptions
'--------------------------------------------------------------------------
Dim behavior As IServiceBehavior
For Each behavior In host.Description.Behaviors
If (TypeOf behavior Is ServiceMetadataBehavior) Then
smb = CType(behavior, ServiceMetadataBehavior)
'------------------------------------------------------------------------
' Replace the default MetadataExporter with our custom implementation
'------------------------------------------------------------------------
smb.MetadataExporter = New CustomWsdlExporter
bFound = True
Exit For
End If
Next
If (Not bFound) Then
smb = New ServiceMetadataBehavior
smb.HttpGetEnabled = True
smb.HttpGetUrl = baseAddresses(0)
'--------------------------------------------------------------------------
' Replace the default MetadataExporter with our custom implementation
'--------------------------------------------------------------------------
smb.MetadataExporter = New CustomWsdlExporter
host.Description.Behaviors.Add(smb)
End If
Return host
End Function
End Class
Public Class CustomWsdlExporter
Inherits WsdlExporter
'----------------------------------------------------------------------------
' ExportContract
'----------------------------------------------------------------------------
Public Overrides Sub ExportContract(ByVal contract As System.ServiceModel.Description.ContractDescription)
'--------------------------------------------------------------------------
' Local Variables
'--------------------------------------------------------------------------
Dim op As OperationDescription
' iterate the operations, collecting the return type and parameter types
For Each op In contract.Operations
' Add code here to reflect the operations and the types used by them
Next
MyBase.ExportContract(contract)
End Sub
'----------------------------------------------------------------------------
' GetGeneratedMetadata
'----------------------------------------------------------------------------
Public Overrides Function GetGeneratedMetadata() As System.ServiceModel.Description.MetadataSet
'--------------------------------------------------------------------------
' Local Variables
'--------------------------------------------------------------------------
Dim schemas As XmlSchemaSet
Dim schema As XmlSchema
schemas = MyBase.GeneratedXmlSchemas
If (schemas IsNot Nothing AndAlso schemas.Count > 0) Then
For Each schema In schemas.Schemas
' Add code here to manipulate each XML schema generated for the service
Next
End If
Return MyBase.GetGeneratedMetadata()
End Function
End Class