Wcf Showing Generics

I have an application in which common and common types of clients and servers, as well as compatibility, is not one of our problems. I plan to have a single repository for all objects with Internet support, and I was thinking about a common interface for my open service.

something like T GetObject (int id)

but wcf doesn't like it, as it tries to expose its circuit (which I really don't care about)

Is it possible to do such a thing with WCF ?, I can use any type of binding, should not be httpbinding or wsbinding ...

+5
source share
4 answers

, , , . (, , ). :

  • ServiceInterfaces
  • ServiceImplementations ( ServiceInterfaces ModelClasses)
  • ModelClasses
  • Host ( ServiceInterfaces ServiceImplementations)
  • Client ( ServiceInterfaces ModelClasses)

ServiceInterfaces ( .., ):

[ServiceContract]
public interface IMyService<T>
{
    T GetObject(int id);
}

ServiceImplementations , IMyService<T>:

public class MyService<T> : IMyService<T>
{
    T GetObject(int id)
    {
        // Create something of type T and return it. Rather difficult
        // since you only know the type at runtime.
    }
}

Host App.config ( Web.config) (, ):

ServiceHost host = new ServiceHost(typeof(MessageManager.MessageManagerService))
host.Open();

, Client ChannelFactory<TChannel> :

Binding binding = new BasicHttpBinding(); // For the example, could be another binding.
EndpointAddress address = new EndpointAddress("http://localhost:8000/......");
IMyService<string> myService =
    ChannelFactory<IMyService<string>>.CreateChannel(binding, address);
string myObject = myService.GetObject(42);

, , . , ( ServiceInterfaces) ( ModelClasses) . , ModelClasses.

+2

, . , , WCF .

. - , , XML . (, int, string) DataContract - WCF , .

- "" - , , XML-, .

, , , , WCF. SOA (- ) 100% .

+8

, ServiceKnownTypesDiscovery.

:

[ServiceKnownType("GetKnownTypes", typeof(ServiceKnownTypesDiscovery))]
public interface ISomeService
{
    [OperationContract]
    object Request(IRequestBase parameters);
}

GetKnownTypes :

public static class ServiceKnownTypesDiscovery
{
    public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
    {
        var types = new List<Type>();

        foreach (var asmFile in Directory.GetFiles(AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory, "*.dll"))
        {
            Assembly asm = Assembly.LoadFrom(asmFile);
            types.AddRange(asm.GetTypes().Where(p=> Attribute.IsDefined(p,typeof(DataContractAttribute))));
        }

        return types;
    }
}

, [DataContract] ( , ), .

, !

0

, DataContract DataMember. , . , , .

Of course, it only works if you create a client using svcutil(or Visual Studio), and refer to an assembly containing a data contract, and a class with extension methods.

Hope this helps ...

0
source

All Articles