How can I have an WCF service with multiple classes?

475 views Asked by At

I wanna have something like this

Service1.svc.cs

namespace MyService
{
    public class User : IUser
    {
        ExternalLibrary.User externalUser = new ExternalLibrary.User();

        public string GetName()
        {
            return externalUser.GetName();
        }

        public bool SetName(string name)
        {
            return externalUser.SetName(name);
        }
    }

    public class Device : IDevice
    {
        ExternalLibrary.Device externalDevice = new ExternalLibrary.Device();

        public string GetDeviceName()
        {
            return externalDevice.GetDeviceName();
        }

        public bool SetDeviceName(string name)
        {
            return externalDevice.SetDeviceName(name);
        }
    }
}

Now, I am trying to find a way to implement these classes on WCF interface. I tryed this not it doesnt worked:

namespace MyService
{
    [ServiceContract]
    public interface IMyService : IUser, IDevices
    {
        // nothing here
    }

    public interface IUSer
    {
        string GetName();
        bool SetName(string name);
    }

    public interface IDevice
    {
        string GetDeviceName();
        bool SetDeviceName(string name);
    }
}

The reason I am trying this way it's because i have too much external classes and I don't want to create objects for all of them everytime I call the service just to get usernames. Any suggestion?

1

There are 1 answers

4
spodger On BEST ANSWER

If you want them under one Service Contract I believe you can do

namespace MyService
{
    [ServiceContract]
    public interface IMyService : IUSer, IDevice
    {
        [OperationContract]
        string GetName();
        [OperationContract]
        bool SetName(string name);
        [OperationContract]
        string GetDeviceName();
        [OperationContract]
        bool SetDeviceName(string name);
    }

    public interface IUSer
    {
        string GetName();
        bool SetName(string name);
    }

    public interface IDevice
    {
        string GetDeviceName();
        bool SetDeviceName(string name);
    }
}

Then declare two partial versions of the class MyService, which each implements the appropriate interface, e.g.

namespace MyService
{
    public partial class MyService : IUser
    {
        ExternalLibrary.User externalUser = new ExternalLibrary.User();

        public string GetName()
        {
            return externalUser.GetName();
        }

        public bool SetName(string name)
        {
            return externalUser.SetName(name);
        }
    }

    public partial class MyService : IDevice
    {
        ExternalLibrary.Device externalDevice = new ExternalLibrary.Device();

        public string GetDeviceName()
        {
            return externalDevice.GetDeviceName();
        }

        public bool SetDeviceName(string name)
        {
            return externalDevice.SetDeviceName(name);
        }
    }
}