WCF /实体框架 - 如何获得良好的DataContract? [英] WCF/Entity Framework - How To Get a Nice DataContract?

查看:287
本文介绍了WCF /实体框架 - 如何获得良好的DataContract?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个干净的方式使用自动生成的EF类与WCF而不是手工制作WCF DataContract的类?
这些类位于LAIT.Entities.dll中

Is there a clean way of using the auto-generated EF classes with WCF instead of having to hand-make classes for WCF DataContracts? The classes are located in the LAIT.Entities.dll

ProductService.vb

ProductService.vb


公共类ProductService
实现IProductService

Public Class ProductService Implements IProductService

Public Function GetWorkOrder(ByVal WorkOrderID As Integer) As

WorkOrder实现IProductService.GetWorkOrder

WorkOrder Implements IProductService.GetWorkOrder

    Using dc As New LAIT.Model.LAITEntities
        Try
            Dim _Order = (From o In dc.WorkOrder
                           Where o.WorkOrderID = WorkOrderID
                          Select o).SingleOrDefault

            Return _Order
        Catch ex As Exception
            Return Nothing
        End Try
    End Using

End Function 

结束课程

IProductService.vb

IProductService.vb


公共接口IProductService

Public Interface IProductService

<OperationContract()>
Function GetWorkOrder(ByVal WorkOrderID As Integer) As

LAIT.Entities.WorkOrder

LAIT.Entities.WorkOrder

结束界面


推荐答案

POCO代理类型不能被Windows Communication Foundation(WCF)直接序列化或反序列化,因为DataContractSerializer序列化引擎只能对已知类型进行序列化和反序列化。代理类型不是已知类型。

The POCO proxy type cannot be directly serialized or deserialized by the Windows Communication Foundation (WCF), because the DataContractSerializer serialization engine can only serialize and deserialize known types. The proxy type is not a known type.

如果您的POCO实体没有任何导航属性,您可以通过WCF服务通过添加您的实体的序列化对象< DataContract>和< DataMember> 属性。

If your POCO entities don't have any "Navigation Properties" you can have serialized objects of your entities through WCF Services by adding <DataContract> and <DataMember> properties in your class.

但对于具有导航属性的实体除了添加 < DataContract>和< DataMember> 您需要在您的WCF服务中进行一些更改,如下所示。在您的WCF服务项目中添加以下calss。

But for Entities with "Navigation Properties" In addition of adding <DataContract> and <DataMember> properties in your class you need to have some changes in your WCF Service as follows. Add the following calss in your WCF Service project.

1。

Imports System.Data.Objects
Imports System.ServiceModel.Description
Imports System.ServiceModel.Channels

Public Class ApplyDataContractResolverAttribute
    Inherits Attribute
    Implements IOperationBehavior

    Public Sub New()
    End Sub

    Public Sub AddBindingParameters(ByVal operationDescription As OperationDescription, ByVal parameters As BindingParameterCollection) Implements IOperationBehavior.AddBindingParameters
    End Sub

    Public Sub ApplyClientBehavior(ByVal operationDescription As OperationDescription, ByVal proxy As System.ServiceModel.Dispatcher.ClientOperation) Implements IOperationBehavior.ApplyClientBehavior
        Dim dataContractSerializerOperationBehavior As DataContractSerializerOperationBehavior = operationDescription.Behaviors.Find(Of DataContractSerializerOperationBehavior)()
        dataContractSerializerOperationBehavior.DataContractResolver = New ProxyDataContractResolver()
    End Sub

    Public Sub ApplyDispatchBehavior(ByVal operationDescription As OperationDescription, ByVal dispatch As System.ServiceModel.Dispatcher.DispatchOperation) Implements IOperationBehavior.ApplyDispatchBehavior
        Dim dataContractSerializerOperationBehavior As DataContractSerializerOperationBehavior = operationDescription.Behaviors.Find(Of DataContractSerializerOperationBehavior)()
        dataContractSerializerOperationBehavior.DataContractResolver = New ProxyDataContractResolver()
    End Sub

    Public Sub Validate(ByVal operationDescription As OperationDescription) Implements IOperationBehavior.Validate
        ' Do validation.
    End Sub
End Class

2.打开服务界面文件。默认情况下,它被称为IService1。

2.Open the service interface file. By default, it is called IService1.

3.使用以下代码更改定义服务接口文件的代码:

3.Replace the code that defines the service interface file with the following code:

C#
[ServiceContract]
public interface IService1
{
    [OperationContract]
    [ApplyDataContractResolver]
    void UpdateOrder(Order updated);

    [OperationContract]
    [ApplyDataContractResolver]
    Order GetOrder(int OrderID);
}

VB
<ServiceContract> _
Public Interface IService1
    <OperationContract> _
    <ApplyDataContractResolver> _
    Sub UpdateOrder(updated As Order)

    <OperationContract> _
    <ApplyDataContractResolver> _
    Function GetOrder(OrderID As Integer) As Order
End Interface

你准备好了。

这篇关于WCF /实体框架 - 如何获得良好的DataContract?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆