How to expose objects through WCF?

后端 未结 4 593
北恋
北恋 2021-01-21 05:32

My project is split up into a typical 3 layer structure for a Silverlight app. That is:

  • A base layer, which is a class library that contains all my business object
相关标签:
4条回答
  • 2021-01-21 06:22

    It seems that you are expecting internal classes used in the WCF service which you have marked as DataContracts to be exported as .NET objects to your client code (the Silverlight UI). WCF does not support this capability. A class marked as a DataContract is just that, a data structure with no methods. If you need a good resource for undestanding WCF, try Learning WCF: A Hands-on Guide by Michele Bustamente.

    @John Fisher does sketch out a way of exposing .NET objects to both the client & service but this may not be an option for Silverlight. Here is a blog entry explaining how to access REST-based services from Silverlight.

    0 讨论(0)
  • 2021-01-21 06:23

    Sixto's answer is correct. With WCF you can share the BO DLL between the client and the service and re-use those objects (just configure the WCF service reference this way), but this won't work in your case. The issue is that your BO project is a standard .NET project and not a silverlight project.

    Typically I structure my projects a little differently than yours:

    • Common Objects - these would be the data structures marked up with WCF attributes and would also contain any common interfaces such as my data layer interface.
    • Data Access - any data access objects. This references only the common objects and returns those from data calls.
    • Business Logic (optional) - If there are other web services to interface with stick that in its own layer
    • Service - The actual WCF service, references Common directly and other projects through interfaces
    • Silverlight - references the Service

    If you moved your business logic out to its project and just referenced your common "dumb" objects, then you could probably create two logic projects, one silverlight and one standard, and just have the actual files in one and create links to those files in the other. That way you would get the same logic across both projects and it would exist in both Silverlight and standard .NET.

    0 讨论(0)
  • 2021-01-21 06:29

    I did something similar on a small project by creating a "communication layer" dll, which contained only the objects being passed between client and server. A variation of this (share a dll between all the projects needing these objects) should work for you. Watch out for versioning issues, though. Unless you have full control of both ends, an upgrade to one end could cause headaches, if you're not careful.

    0 讨论(0)
  • 2021-01-21 06:36

    Mark up your objects that are being used by the service with [DataContract] attributes and then put [DataMember] on the public properties. This will solve your problem.

    0 讨论(0)
提交回复
热议问题