WCF passes the user object to the client and runs its methods

This is the same design as a technical issue. I'm not sure I'm doing it right ...

I have a WCF API that interacts with a database and passes a Person object (which is defined in a separate .dll). This has both methods and attributes. The object is dispatched from WCF to the calling client.

I want to call Person methods on the client. I understand that they cannot be sent downstream from the API, however, if I refer to the same .dll that WCF uses, should I strip the Person API for the .dll Person and then run the methods?

I hope it’s clear what I’m trying to achieve.

Thanks Sam

+1
source share
3 answers

WCF supports the ability to reuse links that are already included in the project. In this sense, you can create an assembly of contracts (an assembly containing subtle domain models (e.g. Person , etc.), to which you can add your own logic.

You can then add the assembly to both your WCF service and the calling client projects, as well as give WCF the ability to reuse any existing references. Thus, what is pushed away from your service is deserialized to a local copy of Person , but not Person , which is generated as a proxy server, you actually get a full instance by which you can make method calls.

Remember, you are sorting by value in this case. Any changes made to the Person instance are local only for the client, you will need to transfer it back to your WCF service (via serialization) again so that the service can recognize any changes and act accordingly.

+1
source

WCF works through contract data. This is a returned data model, for example, obejct Person with it proerties. Do not worry about the method when transporting an object from a service to a client, therefore, if a data contract is used with dll and you refer to WCF, the proxy class will generate a Person object from this DLL.

If your logic is more complex, I suppose it depends on your situation. Let me describe a few words:

  • If you cannot change the dll sources and want to call the public dll method, it is better to use reflection. Thus, you get the object from WCF, set the Person properties with the values ​​returned by the call method.

  • If you can change the dll sources, you can create a basic interface similar to IPerson, implement this interface with the properties of the object object dll, and return the IPerson object. In this case, you will be able to broadcast.

Read more: Ok, let me provide you more details:

  • As a nesting practice, I would recommend you create a separate dll with interfaces. This should be the data contract interface that will describe your object. Something like the following:

     [DataContract] public interface IPerson { [DataMember] public int Identifier { get; set; } [DataMember] public string First { get; set; } [DataMember] public string Last { get; set; } public string GetSomething(); } 
  • The WCF usage method should return an IPerson type.

  • In a shared DLL, please use the IPerson interface for the Person object.
  • Link to this DLL from WCF and the client.
  • Add the web link for your client to the WCF service.
  • Your method will return an object of type IPerson, so you can use your Person object from a shared library and use all its methods.
+2
source

Since you are referring to the same DLL, and WCF can be strong, you should be able to call methods on the Person response object without casting. Ensure that the DataContract attribute is used when defining the Person class.

In the following example, the Person class will be serialized by WCF with three server-side data members. The client side of WCF will deserialize the response ... creating the Person class. Thus, on the client side, you can call FullName (), and it will work without casting.

  [DataContract] public class Person { [DataMember] public int Identifier { get; set; } [DataMember] public string First { get; set; } [DataMember] public string Last { get; set; } public string FullName() { return First + " " + Last; } } 
+1
source

All Articles