简体   繁体   中英

How to support different clients with single WCF service

I need to return Employee class as a response to my clientA as follows.

[OperationContract]

    public Employee GetEmployee(String id)
    {
    ..
    ..
    return emp;
    }

    public class Employee 
    {
    public string Name;
    public string phoneNo;
    }

But the problem here is clientB is going to consume my service but the need a SSN of employee. If i add it into Employee class, I will be sending to clientA as well which wont need it. How to overcome this situation. If i have to do anything with custom deserialization, would not it be a problem if i about to handle 1000s of properties?

Which is the better solution? Any wcf architectural help would also be more helpful.

If different clients have different needs, the proper thing is to create different services as well.

You put the business logic in a shared business class (or distributed over multiple shared business classes), and expose a different service per different type of client. That keeps things nice, abstracted and secure, and nobody gets data they don't need or want.

There has been a quite similar discussion on this link . Basically, it refers to conditional hiding the value of a data member.

You could decide if you want to expose a data member based on the client id or credentials (which should be passed as a parameter to the service method call).

[OperationContract]
public Employee GetEmployee(int clientId, String id)
{
    var employee = new Employee();

    //here you might use a mapping table between the clients and the exposed data members
    if (clientId == 1)
    {
        employee.IsSSNSerializable = true;
    }
    return employee;
}

The Employee class will expose the SSN based on the value of the IsSSNSerializable property:

[DataContract]
public class Employee
{
    public bool IsSSNSerializable = false;

    [DataMember]
    public string Name;
    [DataMember]
    public string phoneNo;

    public string SSN;

    [DataMember(Name = "SSN", EmitDefaultValue = false)]
    public string SSNSerializable
    {
        get
        {
            if (!IsSSNSerializable)
                return null;
            return SSN;
        }
        set
        {
            SSN = value;
        }
    }
}

I would suggest you take a read of the versioning strategies of the WCF that might be matches with your scenarios.

for my case, i implemented IExtensibleDataObject on the data contracts and manage in this layer instead of service contracts layer.

the downside would be difficulties to track different versions of contracts, however I'm practicing the semi-strict versioning and works well for me.

我第二个是Roy,但是如果这是客户端A和客户端B之间的唯一区别。公开带有参数IsSSNRequired的GetEmployee方法(可以具有默认的false值)不会有任何问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM