Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何拥有一个包含多个类的WCF服务?_C#_Wcf_Interface_Multiple Inheritance_Servicecontract - Fatal编程技术网

C# 我如何拥有一个包含多个类的WCF服务?

C# 我如何拥有一个包含多个类的WCF服务?,c#,wcf,interface,multiple-inheritance,servicecontract,C#,Wcf,Interface,Multiple Inheritance,Servicecontract,我想要这样的东西 Service1.svc.cs namespace MyService { public class User : IUser { ExternalLibrary.User externalUser = new ExternalLibrary.User(); public string GetName() { return externalUser.GetName(); }

我想要这样的东西

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);
        }
    }
}
现在,我试图找到一种在WCF接口上实现这些类的方法。我试过这个,但它不起作用:

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);
    }
}

我之所以尝试这种方式,是因为我有太多的外部类,我不想每次调用服务只是为了获取用户名而为所有这些类创建对象。有什么建议吗?

如果你想在一份服务合同下提供,我相信你可以

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);
    }
}
然后声明类
MyService
的两个部分版本,每个部分版本实现适当的接口,例如

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);
        }
    }
}

“它不起作用”是什么意思?它没有编译,或者有运行时错误或者什么?是的@spodger,希望它们在一个服务契约下。我遵循了您的代码,但问题是我不能有两个类(不是接口)。只有一个类实现了这4个方法,只要调用其中一个,我就应该创建两个外部对象。好的,我想我明白你的意思了-我会更新我的答案。这正是我想要的,所以应该有一个选项来接受答案两次:D谢谢,非常好!谢谢