Using ServiceContract and OperationContract on Existing Interfaces

I have some existing interfaces that are used throughout the system.

Now I want to use one of the interfaces as a service contract.

But the problem is that I need to add attributes [ServiceContract]and [OperationContract]existing interfaces, and they will contaminate the rest of the code.

Any solution to this problem without duplicating interfaces?

Using attributes in a specific implementation? is good practice?

thank

+3
source share
2 answers

You can simply extend the interface with an interface like service-warpper, i.e.:

public interface IMyCode
{
    string GetResult();
}

[ServiceContract]
public interface IMyCodeService : IMyCode
{
    [OperationContract]
    string GetResult();
}

# , , IMyCodeService.GetResult , IMyCode.GetResult, new , :

class Program
{
    static void Main(string[] args)
    {
        MyCodeService service = new MyCodeService();
        IMyCodeService serviceContract = (IMyCodeService)service;
        IMyCode codeContract = (IMyCode)service;

        service.GetResult();
        serviceContract.GetResult();
        codeContract.GetResult();

        Console.ReadKey();
    }
}

public interface IMyCode
{
    void GetResult();
}

public interface IMyCodeService : IMyCode
{
    void GetResult();
}

public class MyCodeService : IMyCodeService
{
    public void GetResult()
    {
        Console.Write("I am here");
    }
}

, .

WCF -, , , .

+7

WCF . , Service . , , , .

, ( -, , , , Global.aspx, WCF)

( , ServiceBehavior), WCF . OperationContract, .

0

All Articles