wcf揭露泛型 [英] wcf exposing generics

查看:140
本文介绍了wcf揭露泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端和服务器共享类型的应用程序,互操作性不是我们所关心的问题之一。我打算为所有启用Web的对象设置一个存储库,并且我正在为我的公开服务考虑通用接口。



类似T GetObject(int id)



但是wcf不喜欢它,因为它试图暴露它的模式(我不太在意)



是否有可能做这样的事情与WCF?,我可以使用任何类型的绑定不一定是httpbinding或wsbinding ...

解决方案

我想这是可能的,但我不确定你会想要这个。我会采取以下方法(未经测试,不知道它是否有效)。首先在您的解决方案中创建以下项目结构:


  • ServiceInterfaces

  • ServiceImplementations (引用 ServiceInterfaces ModelClasses
  • ModelClasses

  • 主机(引用 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)
    {
    //创建T类型的东西并返回它。很难
    //因为你只知道运行时的类型。


    主机您在 App.config (或 Web.config )文件中拥有正确的服务配置和下面的代码来承载你的服务(假设它是一个独立的应用程序):

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

    最后在 Client 中使用<一个href =http://msdn.microsoft.com/en-us/library/ms576132.aspx =nofollow noreferrer> ChannelFactory< TChannel> 类来定义一个代理:

     绑定绑定= new BasicHttpBinding(); //例如,可能是另一个绑定。 
    EndpointAddress address = new EndpointAddress(http:// localhost:8000 / ......);
    IMyService< string> myService =
    ChannelFactory< IMyService< string>> .CreateChannel(binding,address);
    string myObject = myService.GetObject(42);

    再一次,我不确定这是否正常。诀窍是在主机和主机之间共享你的服务接口(在 ServiceInterfaces 中)和域模型对象(在 ModelClasses 中)客户端。在我的例子中,我使用一个字符串从服务方法返回,但它可以是 ModelClasses 项目中的任何数据协定类型。


    I have an application where client and server share types, and interoperability is not one of our concerns. I am planning to have a single repository for all web enabled objects, and i was thinking of a generic interface for my exposed service.

    something like T GetObject(int id)

    but wcf doesnt like it since its trying to expose its schema (which i dont really care about)

    is it possible to do such a thing with WCF ?, i can use any type of binding doesnt have to be httpbinding or wsbinding...

    解决方案

    I suppose this is possible, though I'm not sure you'd want this. I'd take the following approach (untested, not sure if it works). First create the following project structure in your solution:

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

    In ServiceInterfaces you have an interface like this (I skipped the namespaces, etc to make the example shorter):

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

    In ServiceImplementations you have a class that implements 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.
        }
    }
    

    In Host you have the correct configuration for your service in an App.config (or Web.config) file and the following code to host your service (given that it is a stand-alone app):

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

    And finally in Client you use a ChannelFactory<TChannel> class to define a proxy:

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

    Again, I'm not sure if this works. The trick is to share your service interfaces (in ServiceInterfaces) and domain model objects (in ModelClasses) between the host and the client. In my example I use a string to return from the service method but it could be any data contract type from the ModelClasses project.

    这篇关于wcf揭露泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆