One of the criticisms of WCF is that it has relatively poor support for known types (for interface programming in WCF)

Source: Internet
Author: User
The reason for this is that if we want to develop services for programming with excuses, you will be unlucky. Whether it is the inherent knowntype feature of WCF or serviceknowntype, you will encounter such difficulties.
If we use knowntype, We have to endlessly declare sub-classes in the parent class. Each time you add a subclass, you must mark it in the parent class. This method clearly violates the object-oriented principle.
If serviceknowntype is used, you have to configure something to indicate our type, but the chances of success are low.
For example: Our excuses:
Namespace Lib
{
Public InterfaceIdata
{
VoidSave ();
}
}

Our service contract
Namespace Lib
{< br> [servicecontract]
Public interface imyservice
{< br> [operationcontract ()]
void Add (idata data);
}
}

Our service implementation class:
Namespace Lib
{
Public   Class Myservice: imyservice
{
Imyservice Member # Region Imyservice Member

Public   Void Add (idata data)
{

}

# Endregion
}
}

If you publish the Service as follows, the client obtains the proxy method VoidAdd (object data );
When we implement Idata When you customize your data class, errors will occur, such:Mydata
Namespace Datalib
{
[Datacontract]
[Serializable]
Public   Class Mydata: idata
{
[Datamember]
Public   String Name;
[Datamember]
Public   Int ID;

Idata Member # Region Idata Member

Public   Void Save ()
{
Console. writeline (This. Name+ "After the task is completed, its ID is:" + This. ID );
}

# Endregion
}
}

When we transmit the mydata type to the server, errors such as serialization errors will be reported. After we configure serviceknowntype, we cannot achieve the expected results, knowntype does not conform to the object-oriented method.
What should I do?
Since WCF does not know how to serialize our mydata class, we can serialize it. Therefore, we need to modify the server side. Code After the modification is completed: Service contract after transformation
Namespace Lib
{< br> [servicecontract]
Public interface imyservice
{< br> [operationcontract ()]
void Add ( byte [] data);
}
}

Myservice
Transformed service implementation class:
Namespace Lib
{
Public   Class Myservice: imyservice
{
Imyservice Member # Region Imyservice Member

Public   Void Add ( Byte [] Data)
{
Using Memorystream MS =   New Memorystream (data ))
{< br> binaryformatter formatter = New binaryformatter ();
idata OBJ = formatter. deserialize (MS) as idata;
obj. save ();
}
}

# Endregion
}
}

Mydata
Mydata after transformation:
Namespace Datalib
{
[Serializable]
Public   Class Mydata: idata
{
Public   String Name {Get; set ;}
Public   Int ID {Get; set ;}

Idata Member # Region Idata Member

Public   Void Save ()
{
Console. writeline (This. Name+ "After the task is completed, its ID is:" + This. ID );
}

# Endregion
}
}

In this way, when the client transmits mydata parameters to the serverByte[] DataThat is, we have to put ourDataSerializedByte[], And then deserialize the server to get our
Mydata, of course, this mydata is of the idata type, so we can call the SAVE () method of idata, so that we can implement it and use programming as an excuse.
Of course, we must copy the DLL generated by the class library where mydata is located to the server without any configuration. Make sure that the DLL and Client versions are the same.
This is my idea for the moment, and I hope it will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.