I have a generic service for performing CRUD operations on a database and I would ideally like to expose it as a WCF service like this:
[ServiceContract]
public interface ICrudService<T> where T : class
{
[OperationContract]
T Add(T arg);
// Read ...
// Update ...
// Delete ...
}
Unfortunately WCF doesn't support generics.
So I'd like to make a WCF service that exposes other services as properties instead. Like this:
[ServiceContract]
public interface IService1
{
[OperationContract]
FooService FooService { get; }
[OperationContract]
BarService BarService { get; }
}
public interface ICrudService<T> where T : class
{
T Add(T arg);
// Read ...
// Update ...
// Delete ...
}
public class FooService : ICrudService<Foo>
{
}
public class BarService : ICrudService<Bar>
{
}
It won't let me use the services as operation contracts. Is there some other way I could achieve this?
that's true:Unfortunately WCF doesn't support generics. but there is a way you can create Dictionary<string, object>
as a input. I'm not sure that's what you want but i had a same problem and i could solve it this way.
[Serializable]
public class WebServiceInputGetDataParams : ISerializable
{
public Dictionary<string, object> Entries
{
get { return entries; }
}
private Dictionary<string, object> entries;
public WebServiceInputGetDataParams()
{
entries = new Dictionary<string, object>();
}
protected WebServiceInputGetDataParams(SerializationInfo info, StreamingContext context)
{
entries = new Dictionary<string, object>();
foreach (var entry in info)
{
entries.Add(entry.Name, entry.Value);
}
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
foreach (var entry in entries)
{
info.AddValue(entry.Key, entry.Value);
}
}
}
its a input class. you can create a method like this:
public void TestMethod(WebServiceInputGetDataParams input)
{
//access the input through dictiobnary
input.Entries
}
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.