我怎样才能拥有具有多个 类 的 WCF 服务?
How can I have an WCF service with multiple classes?
我想要这样的东西
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);
}
}
然后声明 class 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);
}
}
}
我想要这样的东西
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);
}
}
然后声明 class 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);
}
}
}